jxxcarlson/elm-tar/4.0.0

Tar

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

createArchive : List ( Metadata, Data ) -> Bytes

Example:

data1 : ( Metadata, Data )
data1 =
    ( { defaultMetadata | filename = "one.txt" }
    , StringData "One"
    )


data2 : ( Metadata, Data )
data2 =
    ( { defaultMetadata | filename = "two.txt" }
    , StringData "Two"
    )


createArchive [data1, data2]
extractArchive : Bytes -> List ( Metadata, Data )

Decode an archive into its constituent files.

type Data
= StringData String
| BinaryData Bytes

Use StringData String for text data, BinaryData Bytes for binary data:

import Bytes.Encode as Encode


StringData "This is a test"


BinaryData (Encode.encode (Encode.string "foo"))

Metadata

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

Information used in the tar header. You may use defaultMetadata as a starting point, modifying only what is needed.

Fields:

  • filename: Name of the file
  • mode: Unix file permissions, e.g. 644
  • ownerID: owner identifier
  • groupID: group identifier
  • fileSize: size of the encoded file in bytes
  • lastModificationTime: last modification time as a posix value
  • linkIndicator: unused
  • userName: user name
  • groupName: group name
  • fileNamePrefix: can be thought of as the name of the folder/directory in which the files to be processed live
defaultMetadata : Metadata

Defined as

defaultMetadata : Metadata
defaultMetadata =
    { filename = "test.txt"
    , mode = defaultMode
    , ownerID = 501
    , groupID = 123
    , fileSize = 20
    , lastModificationTime = 1542665285
    , linkIndicator = NormalFile
    , linkedFileName = "bar.txt"
    , userName = "anonymous"
    , groupName = "staff"
    , fileNamePrefix = "abc"
    }

Example usage:

myMetadata =
    { defaultMetadata | filename = "Test.txt" }
type alias Mode =
{ owner : FilePermission
, group : FilePermission
, other : FilePermission
, setUserID : Bool
, setGroupID : Bool
}

Default mode

defaultMode : Mode
defaultMode =
    { owner = { read = True, write = True, execute = True }
    , group = { read = True, write = True, execute = False }
    , other = { read = True, write = False, execute = False }
    , setUserID = False
    , setGroupID = False
    }

Encoders

Convenient for integration with other Bytes.Encode.Encoders.

encodeFiles : List ( Metadata, Data ) -> Encoder

Encoder for a list of files

import Bytes
import Bytes.Encode as Encode
import Tar exposing (defaultMetadata)


metaData1 : Tar.Metadata
metaData1 =
    { defaultMetadata | filename = "a.txt" }


content1 : String
content1 =
    "One two three\n"


metaData2 : Tar.Metadata
metaData2
    { defaultMetadata | filename = "c.binary" }


content2 : Bytes.Bytes
content2 =
    "1345"
      |> Encode.string
      |> Encode.encode


result : Bytes
result =
    [ ( metaData1, Tar.StringData content1 )
    , ( metaData2, Tar.BinaryData content2 )
    ]
    |> Tar.encodeFiles
    |> Bytes.Encode.encode
encodeTextFile : Metadata -> String -> Encoder
encodeTextFiles : List ( Metadata, String ) -> Encoder