jxxcarlson/elm-tar/2.2.1

NOTE: the latest version is 4.0.0

Tar

Use

   encodeFiles : List ( FileRecord, Data ) -> Encode.Encoder

to tar an arbitrary set of files which may contain either text or binary data. To tar a set of text files, you can use

   encodeTextFiles : List (FileRecord, String) -> Encode.Encoder

Here is a complete example:

  testArchive : Bytes
  testArchive =
      encodeTextFiles
          [ ( { defaultFileRecord | filename = "one.txt" }, "One" )
          , ( { defaultFileRecord | filename = "two.txt" }, "Two" )
          ]
          |> encode

To untar an archive, imitate this example:

   extractArchive testArchive

For more details, see the README. See also the demo app ./examples/Main.elm

type Data
= StringData String
| BinaryData Bytes

Use StringData String for text data, BinaryData Bytes for binary data, e.g., StringData "This is a test" or BinaryData someBytes

type alias FileRecord =
{ filename : String
, mode : Mode
, ownerID : Int
, groupID : Int
, fileSize : Int
, lastModificationTime : Int
, linkIndicator : Link
, linkedFileName : String
, userName : String
, groupName : String
, fileNamePrefix : String
}

A FileRecord contains the information needed for tar to construct the header for the assoicated file in the tar archive. You may use defaultFileRecord as a starting point, modifying only what is needed.

createArchive : List ( FileRecord, Data ) -> Bytes
 Example:

> data1 = ( { defaultFileRecord | filename = "one.txt" }, StringData "One" )
> data2 = ( { defaultFileRecord | filename = "two.txt" }, StringData "Two" )
> createArchive [data1, data2]

> createArchive [data1, data2]
> <3072 bytes> : Bytes.Bytes

extractArchive : Bytes -> List ( FileData, Data )

Try

import Tar exposing(..) extractArchive testArchive

to test this function

testArchive : Bytes

A small tar archive for testing purposes

encodeFiles : List ( FileRecord, Data ) -> Encoder
  Example


  import Tar exposing(defaultFileRecord)


  fileRecord_ =
      defaultFileRecord


  fileRecord1 =
      { fileRecord_ | filename = "a.txt" }


  content1 =
      "One two three\n"


  fileRecord2 =
      { fileRecord_ | filename = "c.binary" }


  content2 =
      Hex.toBytes "616263646566" |> Maybe.withDefault (encode (Bytes.Encode.unsignedInt8 0))


  Tar.encodeFiles
      [ ( fileRecord1, StringData content1 )
      , ( fileRecord2, BinaryData content2 )
      ]
      |> Bytes.Encode.encode


  Note: `Hex` is found in `jxxcarlson/hex`
encodeTextFile : FileRecord -> String -> Encoder
 Example:

> encodeTextFile defaultFileRecord "Test!" |> encode
> <1024 bytes> : Bytes.Bytes

encodeTextFiles : List ( FileRecord, String ) -> Encoder

Example

encodeFiles [(defaultFileRecord, "This is a test"), (defaultFileRecord, "Lah di dah do day!")] |> Bytes.Encode.encode == <2594 bytes> : Bytes

defaultFileRecord : FileRecord

defaultFileRecord is a dummy FileRecord that you modify to suit your needs. It contains a lot of boilerplates

Example

fileRecord = { defaultFileRecord | filename = "Test.txt" }

See the definition of FileRecord to see what other fields you may want to modify, or see /examples/Main.elm.