Code Monkey home page Code Monkey logo

eunzip's Introduction

eunzip

Simple unzip library with Zip64 support

Overview

Eunzip allows opening, verifying and decompressing of Zip files > 2GB and Zip64 archives, which are not supported by the standard Erlang zip module.

Supports STORE and DEFLATE compression methods.

Basic usage

% Open a Zip file and read its structure
{ok, UnzipState} = eunzip:open("foo/bar.zip")

% Get all file and directory entries from a Zip archive
{ok, Entries} = eunzip:entries(UnzipState)

% Get specific file or directory entry from a Zip archive
{ok, Entry} = eunzip:entry(UnzipState, "README.md")

% Verify an archived file checksum
ok = eunzip:verify(UnzipState, "README.md")

% Decompress file from a Zip archive into as a target filename
ok = eunzip:decompress(UnzipState, "README.md", "unpacked/README.md")

% Close a previously opened Zip file
ok = eunzip:close(UnzipState)

Note: closing a previously opened Zip file is mandatory.

File streaming

Archived file streaming is a technique allowing to process decompressed file by chunks and perform arbitrary actions.

The example below calculates MD5 checksum on-the-fly.

stream_md5(ZipFile, ArchivedFile) ->
    {ok, UnzipState} = eunzip:open("foo/bar.zip"),
    {ok, StreamState} = eunzip:stream_init(UnzipState, "hugefile.mkv"),
    {ok, Md5} = stream_fun(StreamState, crypto:hash_init(md5)),
    ok = eunzip:close(UnzipState),
    Md5.

stream_fun(StreamState, Md5State) ->
    % Read 1 MB chunks of archived data per call
    case eunzip:stream_read_chunk(1024 * 1024, StreamState) of
        {ok, Data} ->
            Md5State1 = crypto:hash_update(Md5State, Data),
            {ok, crypto:hash_final(Md5State1)};
        {more, Data, StreamState1} ->
            stream_fun(StreamState1, crypto:hash_update(Md5State, Data));
        {error, Reason} ->
            {error, Reason}
    end.

Note: stream is closed automatically if all of the file data is read or an error occured.

If you don't want to read until the end of the file, you should call eunzip:stream_end/1 function:

ok = eunzip:stream_end(StreamState)

Credits

Eunzip is based on the Unzip Elixir library by Akash Hiremath.

eunzip's People

Contributors

joaohf avatar pmyarchon avatar puzza007 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

eunzip's Issues

Some dialyzer problems around opaque terms.

Hi,

eunzip is a nice library.

While testing it I've found the following dialyzer errors:

Erlang 24.3.4.13

/home/joaohf/opensource/eunzip/src/eunzip.erl:78:25: The pattern
          {'ok', CD} can never match the type
          {'error', atom()}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:33:9: The pattern
          {'error', Reason} can never match the type
          {'ok',
           #file_buffer{file :: {'file_descriptor', atom(), _},
                        size :: non_neg_integer(),
                        limit :: non_neg_integer(),
                        buffer :: <<>>,
                        buffer_size :: non_neg_integer(),
                        buffer_position :: non_neg_integer(),
                        direction :: 'backward'}}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:154:25: The variable Acc1 can never match since previous clauses completely covered the type
          {'error', 'overlapped_zip_entries'}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:167:34: The call eunzip_range:is_overlap
         (RangeTree :: gb_trees:tree(_, _),
          HeaderOffset :: non_neg_integer(),
          CompressedSize :: non_neg_integer()) contains an opaque term as 1st argument when terms of different types are expected in these positions

/home/joaohf/opensource/eunzip/src/eunzip_range.erl:28:2: Invalid type specification for function eunzip_range:is_overlap/3. The success typing is
          ({_, 'nil' | {_, _, _, _}}, _, _) -> boolean()

Erlang 26.0.2

/home/joaohf/opensource/eunzip/src/eunzip.erl:78:25: The pattern
          {'ok', CD} can never match the type
          {'error', atom()}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:33:9: The pattern
          {'error', Reason} can never match the type
          {'ok',
           #file_buffer{file :: {'file_descriptor', atom(), _},
                        size :: non_neg_integer(),
                        limit :: non_neg_integer(),
                        buffer :: <<>>,
                        buffer_size :: non_neg_integer(),
                        buffer_position :: non_neg_integer(),
                        direction :: 'backward'}}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:154:25: The variable Acc1 can never match since previous clauses completely covered the type
          {'error', 'overlapped_zip_entries'}

/home/joaohf/opensource/eunzip/src/eunzip_central_dir.erl:167:34: The call eunzip_range:is_overlap
         (RangeTree :: gb_trees:tree(_, _),
          HeaderOffset :: non_neg_integer(),
          CompressedSize :: non_neg_integer()) contains an opaque term as 1st argument when terms of different types are expected in these positions

/home/joaohf/opensource/eunzip/src/eunzip_range.erl:28:2: Invalid type specification for function eunzip_range:is_overlap/3.
 The success typing is eunzip_range:is_overlap
          ({_, 'nil' | {_, _, _, _}}, _, _) -> boolean()
 But the spec is eunzip_range:is_overlap
          (Tree, Offset, Length) -> Result
             when
                 Tree :: gb_trees:tree(),
                 Offset :: non_neg_integer(),
                 Length :: non_neg_integer(),
                 Result :: boolean()
 They do not overlap in the 1st argument

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.