Work with files and directories in the archive.
Extract the content of an Entry as a String.
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:
File.Download.bytes to download them as a file.Http.bytesBody to send them to an HTTP server.elm/bytes package to decode these bytes into any data structure.Extracting content from an entry might fail if:
The data is compressed through an unsupported method. See Compression Methods for more information.
The extracted data does not match the integrity checksum.
The entry has no data of the expected type.
The DEFLATE data is corrupted.
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.
Get the uncompressed size of an entry.
This is the number of bytes that you will get if you extract this entry.
Get the compressed size of an entry as stored in the archive.
Get the last time an entry was modified.
Zip time stamps are relative to the time zone they were created in. However, the time zone is not stored in the archive. This means you need to know the zone to get a meaningful time stamp.
Determine if an entry is a directory.
Get the comment of an entry.
Get the CRC32 checksum of an entry's uncompressed data.
You don't need to check the integrity of the data, the extract content functions do it for you.
However, you might still find this checksum useful for other purposes, like quickly determining whether two files are identical.
Create archive entries.
Metadata needed to create an entry.
Note: lastModified requires a Time.Zone to be provided because ZIP time stamps are not stored in a universal zone (like UTC). Read more above.
When you create a file Entry you can choose to store the data as-is or compress it.
Keep in mind that:
Hopefully that helps you decide whether you need compression or not.
Create an entry for a file without compressing it.
import Bytes.Encode as Encode
helloTxt =
Encode.string "Hello, World!"
|> Encode.encode
|> store
{ path = "hello.txt"
, lastModified = ( zone, now )
, comment = Nothing
}Files inside directories are created by passing the absolute path:
store
{ path = "versions/v1.txt"
, lastModified = ( zone, now )
, comment = Nothing
}Create a directory entry.
You do not need to explicitly create directories. Extracting programs automatically create directories in the path to a file.
Use this if you need to add directory metadata or if you want a directory to exist even if it doesn't contain any files.
Deflate compression is provided by
folkertdev/elm-flate.
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 handle them by using the method number
and raw bytes from the UnsupportedCompression case.
case toBytes entry of
Err (UnsupportedCompression 6 rawBytes) ->
Ok <| decodeImplode rawBytes
result ->
resultYou can read more about compression methods and their corresponding numbers in section 4.4.5 of the specification.
Represents a file or a directory in a
Ziparchive.You can use this to extract the content and read the metadata.
See
Entry.pathto learn more about the way these entries are stored.