agu-z/elm-zip/1.0.1

NOTE: the latest version is 3.0.1

Zip.Entry

Work with files and directories in the archive.

type alias Entry =
Entry

Represents a file or a directory in a Zip archive.

You can use this to extract the content and read the metadata.

Check out Entry.path to learn more about the way these entries are stored.

Extract Content

toString : Entry -> Result ExtractError String

Extract the content of an Entry as a String.

toBytes : Entry -> Result ExtractError Bytes

Extract the content of an Entry as Bytes.

Bytes can represent an image, a PDF, a ZIP within a ZIP, anything you can imagine.

Examples of what you can do with Bytes:

= UnsupportedCompression Int
| IntegrityError
| DecodeError
| InflateError

Extracting content from an entry might fail if:

  1. The data is compressed through an unsupported method. See extractWith for more information.

  2. The extracted data does not match the integrity checksum.

  3. The entry has no data of the expected type.

  4. The DEFLATE data is corrupted.

Read Metadata

path : Entry -> String

Get the absolute path of an entry.

path dir == "versions/"


path v1 == "versions/v1.txt"


path v2 == "versions/v2.txt"

Even though Zip archives are aware of directories, they do not store entries in a tree format. Instead, each entry simply indicates its absolute path in the archive.

Different applications have different needs and they may or may not care about the tree structure.

Some applications might expect a certain structure and can simply use Zip.byPath to get the relevant entries.

Other applications might want to explore the archive, and can use Zip.ls to get a list of the entries and go from there.

extractedSize : Entry -> Int

Get the uncompressed size of an entry.

This is the number of bytes that you will get if you extract this entry.

compressedSize : Entry -> Int

Get the compressed size of an entry as stored in the archive.

lastModified : Zone -> Entry -> Posix

Get the last time an entry was modified.

Zip time stamps are relative to the time zone they were created in.

Unfortunately, this information is not stored anywhere, making them only meaningful if you know it.

isDirectory : Entry -> Bool

Determine whether an entry is a directory.

comment : Entry -> String

Get the comment of an entry.

Did you know files can have comments? I didn't.

checksum : Entry -> Int

Get the CRC32 checksum of an entry's uncompressed data.

You do not need to confirm the integrity of data manually. The extract content functions perform this check automatically for you.

However, you might still find this checksum useful for other purposes, like quickly determining whether two files are identical.

Other Compression Methods

This library uses the folkertdev/elm-flate package to support Deflate compression. Most archives you'll find in the wild will use this method.

If you're expecting to work with archives using other methods, you can use the following function to handle them.

( { method : Int
, rawBytes : Bytes
}
-> Maybe Bytes
)
-> Entry
-> Result ExtractError Bytes

Extract archives with other compression methods.

handleCompression { method, rawBytes } =
    case method of
        6 ->
            -- Use raw bytes to extract the data.
            decodeImplode rawBytes


        _ ->
            -- We don't know how to handle other methods.
            Nothing


extracted =
    entry |> extractWith handleCompression

Note: This callback is only called if the method is not 0 (Stored) nor 8 (Deflated). You do not need to handle these yourself.

You can read more about compression methods and their corresponding numbers in section 4.4.5 of the specification.