This repository contains two libs to handle ZIP archives, zip-archive.js
to read them, and zip-builder.js
to write them. Note that this repository is managed as described in https://xkcd.com/1597/.
This library originates from a project where I needed both raw deflate compression and ZIP archives. I used pako
for the compression and so decided to write my own ZIP library that internally uses pako
.
The two libraries are independent from each other, both require pako (unless your ZIP archives are just containers with uncompressed contents). Simply include both pako
and this library via <script>
tags into your project. Then you can use the globals ZipArchive
and ZipBuilder
.
First of all you need the ZIP file as ArrayBuffer
. If you are using XHR, set responseType = 'arraybuffer'
; if you are using fetch
, then call the arrayBuffer()
method on the response. If you are working with files, then use the readAsArrayBuffer()
method of the FileReader
.
Then call the ZipArchive
constructor with the arraybuffer as argument. Note that this will throw an error if the ZIP file is broken. Otherwise you get a ZipArchive
object with the following methods:
Get the comment of the ZIP archive. This assumes UTF-8 encoding by default, set the optional argument to true
if that’s not the case.
Get the list of all entries of the ZIP archive.
Get the metadata of an entry of the ZIP archive. You can specify the entry either by using its name, or its index. The metadata object contains following entries:
name
versionCreated
version
bitflag
compressionMethod
lastMod
(asDate
)crc32
compressedSize
uncompressedSize
disk
internalAttr
externalAttr
extra
(as array)comment
Refer to the ZIP standard for the meaning.
Get the compressed data of an entry of the ZIP archive (as arraybuffer).
Get the uncompressed data of an entry of the ZIP archive, either as arraybuffer or as string (if the second optional argument is set to true
, assumes UTF-8 encoding).
Call the ZipBuilder
constructor with an optional argument for default data (see below for addFile
). The resulting object has the following methods:
This adds a file the the ZIP archive. The data object can contain the following entries:
name
: the name of the filecomment
: the file commentextra
: extra data (as array)date
: the timestamp (asDate
)content
: the content (as array)isText
: whether the content is text (asBoolean
)isDir
: whether the content is a directory (asBoolean
)version
: the versionattr
: the numeric attributescompressionLevel
: the compression levelcompressionRate
: the maximal compression rate (if the compressed data is larger it will be stored without compression)
Missing options fall back to the default you specified in the constructor, or to sensible defaults.
Closes the ZIP archive with an optional comment. Note that the comment will be encoded as UTF-8. If you are not sure whether your reader will accept this, use only ASCII letters (or no comment at all).
The current number of entries in the ZIP archive.
The current byte size of the ZIP archive (assuming you don’t add a comment).
Returns the ZIP archive as Blob
. This will automatically close the archive if not done yet.