zip(3erl) | Erlang Module Definition | zip(3erl) |
zip - Utility for reading and creating 'zip' archives.
This module archives and extracts files to and from a zip archive. The zip format is specified by the "ZIP Appnote.txt" file, available on the PKWARE web site www.pkware.com.
The zip module supports zip archive versions up to 6.1. However, password-protection and Zip64 are not supported.
By convention, the name of a zip file is to end with .zip. To abide to the convention, add .zip to the filename.
zip_comment() = #zip_comment{comment = string()}
The record zip_comment only contains the archive comment for a zip archive.
zip_file() =
#zip_file{name = string(),
info = file:file_info(),
comment = string(),
offset = integer() >= 0,
comp_size = integer() >= 0}
The record zip_file contains the following fields:
filename() = file:filename()
The name of a zip file.
extension() = string()
extension_spec() =
all |
[extension()] |
{add, [extension()]} |
{del, [extension()]}
create_option() =
memory |
cooked |
verbose |
{comment, string()} |
{cwd, file:filename()} |
{compress, extension_spec()} |
{uncompress, extension_spec()}
These options are described in create/3.
handle()
As returned by zip_open/2.
foldl(Fun, Acc0, Archive) -> {ok, Acc1} | {error, Reason}
Types:
Calls Fun(FileInArchive, GetInfo, GetBin, AccIn) on successive files in the Archive, starting with AccIn == Acc0.
FileInArchive is the name that the file has in the archive.
GetInfo is a fun that returns information about the file.
GetBin returns the file contents.
Both GetInfo and GetBin must be called within the Fun. Their behavior is undefined if they are called outside the context of Fun.
The Fun must return a new accumulator, which is passed to the next call. foldl/3 returns the final accumulator value. Acc0 is returned if the archive is empty. It is not necessary to iterate over all files in the archive. The iteration can be ended prematurely in a controlled manner by throwing an exception.
Example:
> Name = "dummy.zip". "dummy.zip" > {ok, {Name, Bin}} = zip:create(Name, [{"foo", <<"FOO">>}, {"bar", <<"BAR">>}], [memory]). {ok,{"dummy.zip",
<<80,75,3,4,20,0,0,0,0,0,74,152,97,60,171,39,212,26,3,0,
0,0,3,0,0,...>>}} > {ok, FileSpec} = zip:foldl(fun(N, I, B, Acc) -> [{N, B(), I()} | Acc] end, [], {Name, Bin}). {ok,[{"bar",<<"BAR">>,
{file_info,3,regular,read_write,
{{2010,3,1},{19,2,10}},
{{2010,3,1},{19,2,10}},
{{2010,3,1},{19,2,10}},
54,1,0,0,0,0,0}},
{"foo",<<"FOO">>,
{file_info,3,regular,read_write,
{{2010,3,1},{19,2,10}},
{{2010,3,1},{19,2,10}},
{{2010,3,1},{19,2,10}},
54,1,0,0,0,0,0}}]} > {ok, {Name, Bin}} = zip:create(Name, lists:reverse(FileSpec), [memory]). {ok,{"dummy.zip",
<<80,75,3,4,20,0,0,0,0,0,74,152,97,60,171,39,212,26,3,0,
0,0,3,0,0,...>>}} > catch zip:foldl(fun("foo", _, B, _) -> throw(B()); (_,_,_,Acc) -> Acc end, [], {Name, Bin}). <<"FOO">>
list_dir(Archive) -> RetValue
list_dir(Archive, Options) -> RetValue
table(Archive) -> RetValue
table(Archive, Options) -> RetValue
Types:
list_dir/1 retrieves all filenames in the zip archive Archive.
list_dir/2 provides options.
table/1 and table/2 are provided as synonyms to resemble the erl_tar module.
The result value is the tuple {ok, List}, where List contains the zip archive comment as the first element.
One option is available:
t(Archive) -> ok
Types:
Prints all filenames in the zip archive Archive to the Erlang shell. (Similar to tar t.)
tt(Archive) -> ok
Types:
Prints filenames and information about all files in the zip archive Archive to the Erlang shell. (Similar to tar tv.)
unzip(Archive) -> RetValue
unzip(Archive, Options) -> RetValue
extract(Archive) -> RetValue
extract(Archive, Options) -> RetValue
Types:
unzip/1 extracts all files from a zip archive.
unzip/2 provides options to extract some files, and more.
extract/1 and extract/2 are provided as synonyms to resemble module erl_tar.
If argument Archive is specified as a binary, the contents of the binary is assumed to be a zip archive, otherwise a filename.
Options:
zip(Name, FileList) -> RetValue
zip(Name, FileList, Options) -> RetValue
create(Name, FileList) -> RetValue
create(Name, FileList, Options) -> RetValue
Types:
Creates a zip archive containing the files specified in FileList.
create/2 and create/3 are provided as synonyms to resemble module erl_tar.
FileList is a list of files, with paths relative to the current directory, which are stored with this path in the archive. Files can also be specified with data in binaries to create an archive directly from data.
Files are compressed using the DEFLATE compression, as described in the "Appnote.txt" file. However, files are stored without compression if they are already compressed. zip/2 and zip/3 check the file extension to determine if the file is to be stored without compression. Files with the following extensions are not compressed: .Z, .zip, .zoo, .arc, .lzh, .arj.
It is possible to override the default behavior and control what types of files that are to be compressed by using options {compress, What} and {uncompress, What}. It is also possible to use many compress and uncompress options.
To trigger file compression, its extension must match with the compress condition and must not match the uncompress condition. For example, if compress is set to ["gif", "jpg"] and uncompress is set to ["jpg"], only files with extension "gif" are compressed.
Options:
zip_close(ZipHandle) -> ok | {error, einval}
Types:
Closes a zip archive, previously opened with zip_open/1,2. All resources are closed, and the handle is not to be used after closing.
zip_get(ZipHandle) -> {ok, [Result]} | {error, Reason}
zip_get(FileName, ZipHandle) -> {ok, Result} | {error, Reason}
Types:
Extracts one or all files from an open archive.
The files are unzipped to memory or to file, depending on the options specified to function zip_open/1,2 when opening the archive.
zip_list_dir(ZipHandle) -> {ok, Result} | {error, Reason}
Types:
Returns the file list of an open zip archive. The first returned element is the zip archive comment.
zip_open(Archive) -> {ok, ZipHandle} | {error, Reason}
zip_open(Archive, Options) -> {ok, ZipHandle} | {error, Reason}
Types:
Opens a zip archive, and reads and saves its directory. This means that later reading files from the archive is faster than unzipping files one at a time with unzip/1,2.
The archive must be closed with zip_close/1.
The ZipHandle is closed if the process that originally opened the archive dies.
stdlib 3.7.1 | Ericsson AB |