zip.js

Filesystem API

The script zip-fs.js helps to manipulate zip files easily. It provides a virtual filesystem API implementation residing in RAM for zip files.

Contents

  1. Installation
  2. Zip filesystem
  3. Zip entry
  4. Zip file entry
  5. Zip directory entry

Installation

Zip filesystem

Zip filesystem constructor

zip.fs.FS()

A zip.fs.FS object stores an entire filesystem. The root property stores a ZipDirectoryEntry object representing the root directory of the zip. The FS class inherits from all the methods of the class ZipDirectoryEntry in order to modify data in the root directory without accessing to it explicitely.

Attributes

root (ZipDirectoryEntry)
The ZipDirectoryEntry object referencing the root directory of the filesystem

Methods

FS#getById(entryId)

Get a ZipEntry object given a unique entry id

Parameters:

entryId (number)
The id of the entry

Returns:

ZipEntry
The ZipEntry object

FS#find(fullname)

Get a ZipEntry object given a unique entry full filename

Parameters:

fullname (string)
The entry full filename

Returns:

ZipEntry
The ZipEntry object

FS#remove(zipEntry)

Remove a ZipEntry object from the virtual filesystem

Parameters:

zipEntry (ZipEntry)
The entry to remove

FS#move(entry, destination)

Move a ZipEntry object into the ZipDirectoryEntry destination object

Parameters:

entry (ZipEntry)
The ZipEntry to move
destination (ZipDirectoryEntry)
The ZipDirectoryEntry destination object

Zip entry

ZipEntry constructor

ZipEntry()

Each ZipEntry object represents a node in the zip directory tree. A file in the zip is a ZipFileEntry with a ZipEntry object as prototype. A directory in the zip is a ZipDirectoryEntry with a ZipEntry object as prototype.

Attributes

name (string)
The entry filename
directory (boolean)
true if the entry is a ZipDirectoryEntry object
children (Array of ZipEntry)
The entry children, empty if the entry is a ZipFileEntry object
id (number)
The id of the entry

Methods

ZipEntry#getFullname()

Get the full name of the entry

Returns:

string
The full name

ZipEntry#getRelativeName(ancestor)

Get the relative name of the entry to the ancestor

Parameters:

ancestor (ZipDirectoryEntry)
the ancestor

Returns:

string
The relative name

ZipEntry#isDescendantOf(ancestor)

Test if the entry is a descendant of ancestor directory entry

Parameters:

ancestor (ZipDirectoryEntry)
The ancestor

Returns:

boolean
true if the entry is a descendant of ancestor directory entry

Zip file entry

ZipFileEntry constructor

ZipFileEntry()

Methods

ZipFileEntry#getText([options])

Get file entry content as a string

Parameters:

options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of should be verified (false by default)
password (string)
password used to decrypt the file (undefined by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the decompression

Returns:

Promise
A promise with the unzipped content as resolved value

ZipFileEntry#getBlob(mimeType [, options])

Get file entry content as a Blob object

Parameters:

mimeType (string)
A MIME type
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of should be verified (false by default)
password (string)
password used to decrypt the file (undefined by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the decompression

Returns:

Promise
A promise with the unzipped content as resolved value

ZipFileEntry#getData64URI(mimeType [, options])

Get file entry content as a data URI string

Parameters:

mimeType (string)
A MIME type
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of should be verified (false by default)
password (string)
password used to decrypt the file (undefined by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the decompression
ZipFileEntry#getUint8Array([options])

Get file entry content as a UInt8Array object

Parameters:

options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of should be verified (false by default)
password (string)
password used to decrypt the file (undefined by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the decompression

Returns:

Promise
A promise with the UInt8Array object as resolved value

ZipFileEntry#replaceBlob(blob)

Replace the file entry content with a blob object

Parameters:

blob (Blob)
the blob object

ZipFileEntry#replaceText(text)

Replace the file entry content with a string

Parameters:

text (string)
the string
ZipFileEntry#replaceData64URI(text)

Replace the file entry content with a data URI

Parameters:

data URI (string)
the data URI
ZipFileEntry#replaceUint8Array(array)

Replace the file entry content with a UInt8Array object

Parameters:

array (UInt8Array)
the UInt8Array object

Zip directory entry

ZipDirectoryEntry constructor

ZipDirectoryEntry()

Methods

ZipDirectoryEntry#addDirectory(name)

Add a directory into the directory entry

Parameters:

name (string)
The directory name

Returns:

ZipDirectoryEntry
The new ZipDirectoryEntry object

ZipDirectoryEntry#addBlob(name, blob)

Add a Blob object file into the directory entry

Parameters:

name (string)
The file name
blob (Blob)
The Blob content

Returns:

ZipFileEntry
The new ZipFileEntry object

ZipDirectoryEntry#addText(name, text)

Add a text object into the directory entry

Parameters:

name (string)
The file name
text (string)
The text content

Returns:

ZipFileEntry
The new ZipFileEntry object

ZipDirectoryEntry#addData64URI(name, dataURI)

Add a data URI object into the directory entry

Parameters:

name (string)
The file name
text (string)
The data URI content

Returns:

ZipFileEntry
The new ZipFileEntry object
ZipDirectoryEntry#addUint8Array(name, array)

Add a UInt8Array object into the directory entry

Parameters:

name (string)
The file name
array (UInt8Array)
The UInt8Array objact

Returns:

ZipFileEntry
The new ZipFileEntry object

ZipDirectoryEntry#addHttpContent(name, URL, [, options])

Add a content retrieved from a URL into the directory entry

Parameters:

name (string)
The file name
URL (string)
The content URL
options (Object)
Optional properties:
useRangeHeader (boolean)
boolean value specifying if the Range request header should be used (false by default)
preventHeadRequest (boolean)
boolean value specifying if a HEAD request can be sent in order the get the content length (false by default)
forceRangeRequests (boolean)
boolean value specifying if the Range request header should be sent even if the server does not send responses with the header Accept-Ranges: bytes (false by default)
useXHR (boolean)
boolean value specifying if the implementation should rely on the XMLHttpRequest API instead of the fetch API to send requests (false by default)
If useXHR is not set to true, you can additionnally pass any option the fetch API accepts.

Returns:

ZipFileEntry
The new ZipFileEntry object

ZipDirectoryEntry#addFileSystemEntry(fileSystemEntry)

Add a FileSystemEntry object. If the entry is a FileSystemFileEntry object then the FileSystemFileEntry#file() function is called in order to get its content. If the entry is a FileSystemDirectoryEntry object then it will ba added as a directory and all its children will be added recursively.

Parameters:

entry (FileSystemEntry)
The FileSystemEntry object to add

Returns:

Promise
A promise with the new ZipEntry object as resolved value

ZipDirectoryEntry#importBlob(blob [, options])

Import a zipped content stored in a Blob object into the directory entry

Parameters:

blob (Blob)
The Blob object
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of the files in the imported zip should be verified (false by default)
password (string)
password used to decrypt the all the files (empty by default)
filenameEncoding (string)
encoding of filenames stored in another encoding than UTF-8 (cp437 by default)
commentEncoding (string)
encoding of comments stored in another encoding than UTF-8 (cp437 by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the import

Returns:

Promise
A promise with undefined as resolved value

ZipDirectoryEntry#importData64URI(dataURI [, options])

Import a zipped content stored in a data URI string into the directory entry

Parameters:

dataURI (string)
The data URI
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of the files in the imported zip should be verified (false by default)
password (string)
password used to decrypt the all the files (empty by default)
filenameEncoding (string)
encoding of filenames stored in another encoding than UTF-8 (cp437 by default)
commentEncoding (string)
encoding of comments stored in another encoding than UTF-8 (cp437 by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the import

Returns:

Promise
A promise with undefined as resolved value

ZipDirectoryEntry#importUint8Array(array [, options])

Import a zipped content stored in a UInt8Array object into the directory entry

Parameters:

array (UInt8Array)
The UInt8Array object
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of the files in the imported zip should be verified (false by default)
password (string)
password used to decrypt the all the files (empty by default)
filenameEncoding (string)
encoding of filenames stored in another encoding than UTF-8 (cp437 by default)
commentEncoding (string)
encoding of comments stored in another encoding than UTF-8 (cp437 by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the import

Returns:

Promise
A promise with undefined as resolved value

ZipDirectoryEntry#importHttpContent(URL [, options])

Import a zipped content retrieved from a URL into the directory entry

Parameters:

URL (string)
The URL
options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value
checkSignature (boolean)
boolean value specifying if the signature of the files in the imported zip should be verified (false by default)
password (string)
password used to decrypt the all the files (empty by default)
filenameEncoding (string)
encoding of filenames stored in another encoding than UTF-8 (cp437 by default)
commentEncoding (string)
encoding of comments stored in another encoding than UTF-8 (cp437 by default)
useRangeHeader (boolean)
boolean value specifying if the Range request header should be used (false by default)
preventHeadRequest (boolean)
boolean value specifying if a HEAD request can be sent in order the get the content length (false by default)
forceRangeRequests (boolean)
boolean value specifying if the Range request header should be sent even if the server does not send responses with the header Accept-Ranges: bytes (false by default)
useXHR (boolean)
boolean value specifying if the implementation should rely on the XMLHttpRequest API instead of the fetch API to send requests (false by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the import
If useXHR is not set to true, you can additionnally pass any option the fetch API accepts.

Returns:

Promise
A promise with undefined as resolved value

ZipDirectoryEntry#exportBlob([options])

Export the zipped content in a Blob object from the directory entry

Parameters:

options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value (undefined by default)
zip64 (boolean)
force all the files to be written in Zip64 (false by default)
level (number)
compression level applied on all files (5 by default)
version (number)
zip version of all files (undefined by default)
password (string)
password used to encrypt all the files (undefined by default)
encryptionStrength (number)
strength of the encryption algorithm: 1 for AES-128, 2 for AES-192, 3 for AES-256 (3 by default)
zipCrypto (boolean)
use ZipCrypto instead of AES (false by default)
relativePath (boolean)
create filenames relative to the directory entry (false by default)
dataDescriptor (boolean)
boolean value specifying the data descriptor record should be included (true by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the exprt

Returns:

Promise
A promise with the zipped content as resolved value

ZipDirectoryEntry#exportData64URI([options])

Export the zipped content in a data URI from the directory entry

Parameters:

options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value (undefined by default)
zip64 (boolean)
force all the files to be written in Zip64 (false by default)
level (number)
compression level applied on all files (5 by default)
version (number)
zip version of all files (undefined by default)
password (string)
password used to encrypt all the files (undefined by default)
encryptionStrength (number)
strength of the encryption algorithm: 1 for AES-128, 2 for AES-192, 3 for AES-256 (3 by default)
zipCrypto (boolean)
use ZipCrypto instead of AES (false by default)
relativePath (boolean)
create filenames relative to the directory entry (false by default)
dataDescriptor (boolean)
boolean value specifying the data descriptor record should be included (true by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the export

Returns:

Promise
A promise with the zipped content as resolved value

ZipDirectoryEntry#exportUint8Array([options])

Export the zipped content in a UInt8Array object from the directory entry

Parameters:

options (Object)
Optional properties:
onprogress (Function)
function tracking the task progress and having as parameters an index (number) value and a max (number) value (undefined by default)
zip64 (boolean)
force all the files to be written in Zip64 (false by default)
level (number)
compression level applied on all files (5 by default)
version (number)
zip version of all files (undefined by default)
password (string)
password used to encrypt all the files (undefined by default)
encryptionStrength (number)
strength of the encryption algorithm: 1 for AES-128, 2 for AES-192, 3 for AES-256 (3 by default)
zipCrypto (boolean)
use ZipCrypto instead of AES (false by default)
relativePath (boolean)
create filenames relative to the directory entry (false by default)
dataDescriptor (boolean)
boolean value specifying the data descriptor record should be included (true by default)
signal (AbortSignal)
signal object created with an AbortController instance and used to cancel the export

Returns:

Promise
A promise with the zipped content as resolved value

ZipDirectoryEntry#getChildByName(name)

Get a ZipEntry child object given its filename

Parameters:

name (string)
The entry name

Returns:

ZipEntry
The ZipEntry object