agu-z/elm-zip/1.0.0

NOTE: the latest version is 3.0.1

Zip

Work with Zip archives.

type Zip

Represents a Zip archive.

An archive is comprised of entries which represent (optionally) compressed files and directories.

Reading

fromBytes : Bytes -> Maybe Zip

Read a Zip from Bytes.

If you have an uploaded File of an archive, you can use File.toBytes to read it:

import File exposing (File)
import Task exposing (Task)
import Zip exposing (Zip)


type Msg
    = GotZip (Maybe Zip)


readArchive : File -> Cmd Msg
readArchive file =
    file
        |> File.toBytes
        |> Task.map Zip.fromBytes
        |> Task.perform GotZip

You can also get Bytes from anywhere like an HTTP request, or even from within another archive.

Accessing Content

Once you have a Zip, you can use it to access its files and directories.

Use the Zip.Entry module to do read their content and metadata.

ls : Zip -> List Entry

List all entries in the archive.

allEntries =
    Zip.ls zip

Files and directories get their own entries and they are not stored in a tree format.

If you only care about one of them, you can use the Zip.Entry.isDirectory function to filter them:

allFiles =
    List.filter (not << Entry.isDirectory) zip
byPath : String -> Zip -> Maybe Entry

Get an entry by its absolute path.

Zip.byPath "versions/v1.txt"

Nothing is returned if no entry matches the path exactly.

Directory entries are typically stored in the archive with a slash at the end:

Zip.byPath "versions" == Nothing


Zip.byPath "versions/" == Just Entry