View Source Unzip (unzip v0.12.0)

Module to get files out of a zip. Works with local and remote files

Overview

Unzip tries to solve problem of accessing files from a zip which is not local (Aws S3, sftp etc). It does this by simply separating file system and zip implementation. Anything which implements Unzip.FileAccess can be used to get zip contents. Unzip relies on the ability to seek and read of the file, This is due to the nature of zip file. Files from the zip are read on demand.

Usage

# Unzip.LocalFile implements Unzip.FileAccess
zip_file = Unzip.LocalFile.open("foo/bar.zip")

# `new` reads list of files by reading central directory found at the end of the zip
{:ok, unzip} = Unzip.new(zip_file)

# Alternatively if you have the zip file in memory as binary you can
# directly pass it to `Unzip.new(binary)` to unzip
#
# {:ok, unzip} = Unzip.new(<<binary>>)

# returns list of files along with metadata
file_entries = Unzip.list_entries(unzip)

# returns decompressed file stream
stream = Unzip.file_stream!(unzip, "baz.png")

# if you want to read the whole file as binary
#
# file_content = Enum.into(stream, <<>>, &IO.iodata_to_binary/1)

Supports STORED and DEFLATE compression methods. Does not support zip64 specification yet

Summary

Types

t()

Struct holding zip related metadata, returned by the new/1.

Functions

Returns decompressed file as a stream of iodata. file_path must be the complete file path within the zip. The file is read in chunks, decompressed concurrently in a streaming fashion.

Returns list of files metadata. This does not make pread call as metadata is already by new/1.

Reads zip metadata from the passed zip file.

Types

@type stream_options() :: {:chunk_size, pos_integer()}
@type t() :: %Unzip{cd_list: map(), zip: struct()}

Struct holding zip related metadata, returned by the new/1.

Public fields:

  • zip - Zip struct passed to new/1

Remaining fields are private and must not be accessed directly.

Functions

Link to this function

file_stream!(unzip, file_path, opts \\ [])

View Source
@spec file_stream!(t(), String.t(), [stream_options()]) :: Enumerable.t()

Returns decompressed file as a stream of iodata. file_path must be the complete file path within the zip. The file is read in chunks, decompressed concurrently in a streaming fashion.

Options

  • chunk_size - Chunks are read from the source of the size specified by chunk_size. This is not the size of the chunk returned by file_stream! since the chunk size varies after decompressing the. Useful when reading from the source is expensive and you want optimize by increasing the chunk size. Defaults to 65_000
@spec list_entries(t()) :: [Unzip.Entry.t()]

Returns list of files metadata. This does not make pread call as metadata is already by new/1.

See Unzip.Entry for metadata fields

@spec new(Unzip.FileAccess.t()) :: {:ok, t()} | {:error, term()}

Reads zip metadata from the passed zip file.

zip must implement Unzip.FileAccess protocol.

Fetches the list of files present in the zip and other metadata by reading central directory found at the end of the zip.

Returns Unzip struct which contains passed zip struct and zip metadata.