Skip to content

infinity.zip

Provides ZIP compression and extraction functionality for archives in files and streams.

Module: infinity.zip

Classes:
Namespaces:
Interfaces:

Example:

infinity.loadModule('infinity.zip');
infinity.loadModule('infinity.fileSystem');

infinity.file.writeString('test.txt', '123');
infinity.file.writeString('test2.txt', '321');

let myArchive = new infinity.zip.file();
myArchive.open('archive.zip', infinity.zip.file.mode.write);

myArchive.add('test.txt');
myArchive.add('test2.txt', 'test3.txt');

myArchive.close();
myArchive.open('archive.zip', infinity.zip.file.mode.read);

myArchive.extract('test3.txt', './');
console.debug(infinity.file.readString('test3.txt'));
//321

myArchive.free();

Classes

file

The class for compression and extraction operations on ZIP-archive files in INFINITY.JS. Files added with add() are compressed with the Deflate method.


Properties


comment

Gets or sets the comment of the ZIP-archive. A changed comment is written when an archive opened for writing is closed.

Type: string


fileCount (readonly)

Gets the quantity of files contained inside the ZIP-archive.

Type: number


fileNames (readonly)

Gets the list of the names of the files inside the ZIP-archive (as stored in the archive, with / as path delimiter). The index of a name in this list can be passed to extract().

Type: infinity.zip.file.stringArray


Methods


constructor()

Lets you instantiate an INFINITY.JS ZIP-file object instance for operations on files. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.zip');
let myArchive = new infinity.zip.file();

add()

Adds a file to the ZIP-archive. The archive must have been opened with the mode write or readWrite, otherwise an error is thrown.

Signature:

add( fileName: string, archiveFileName?: string ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be added.

  • archiveFileName: string, optional

    The name of the file inside the archive (may contain subdirectories separated by /). Default: the filename of fileName without path.

Example:

myArchive.add('dataToCompress.txt');

close()

Closes the ZIP-archive. For an archive opened with the mode write or readWrite, the archive directory is written to the file.

Signature:

close(): void

Example:

myArchive.close();

extract()

Extracts the file indicated by the given index number out of the ZIP-archive and writes it to the specified directory. An existing file is overwritten. Throws an error if the name of the file inside the archive points outside of the target directory (e.g. ../file.txt or an absolute path; since version 3.11).

Signature:

extract( index: number, path?: string, createSubDirs?: boolean ): void

Parameters:

  • index: number

    The zero-based index of the file contained in the archive (see fileNames).

  • path: string, optional

    Relative (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the directory, into which the file should be extracted. Default: '' (the folder with the used INFINITY.JS executable file).

  • createSubDirs: boolean, optional

    Indicates, whether to create subdirectories according to the directory structure inside the ZIP-archive. If false, the file is written directly into path. Default: true.

Example:

myArchive.extract(3, './', true);

extract()

Extracts the file indicated by the given filename out of the ZIP-archive and writes it to the specified directory. An existing file is overwritten. Throws an error if the archive doesn't contain a file with this name, or if the name points outside of the target directory (e.g. ../file.txt or an absolute path; since version 3.11).

Signature:

extract( fileName: string, path?: string, createSubDirs?: boolean ): void

Parameters:

  • fileName: string

    The name of the file contained in the archive (as listed in fileNames).

  • path: string, optional

    Relative (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the directory, into which the file should be extracted. Default: '' (the folder with the used INFINITY.JS executable file).

  • createSubDirs: boolean, optional

    Indicates, whether to create subdirectories according to the directory structure inside the ZIP-archive. If false, the file is written directly into path. Default: true.

Example:

myArchive.extract('toBeExtracted.txt', './', true);

free()

Frees up memory occupied by the INFINITY.JS ZIP-file object instance. The object must not be used afterwards: all other methods and properties then throw an error.

Signature:

free(): void

Example:

myArchive.free();

open()

Opens or creates a ZIP-archive inside an INFINITY.JS ZIP-file object instance. A previously opened archive is closed first. With the mode write, a new archive is created (an existing file is overwritten). Throws an error if the file cannot be opened or is not a valid ZIP-archive, or if the mode is closed.

Signature:

open( fileName: string, mode: infinity.zip.file.mode ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to zip-archive.

  • mode: infinity.zip.file.mode

    Specifies the type of access to the zip-archive according to infinity.zip.file.mode. For archive creation the write-access is required.

Example:

infinity.loadModule('infinity.zip');
let myArchive = new infinity.zip.file();
myArchive.open('archive.zip', infinity.zip.file.mode.readWrite);

stream

A read-only stream that provides a ZIP-archive built on the fly from the files added with add(). The files are stored without compression (ZIP64 format, UTF-8 filenames); their contents are only read from disk when the stream is read. The archive layout is generated on the first access to size, position or the stream data, so all files must be added (and the comment set) before that: afterwards add() and setting the comment throw an error (before version 3.11 they were silently ignored).

Example:

infinity.loadModule('infinity.zip');
infinity.loadModule('infinity.stream');

let zipStream = new infinity.zip.stream();
let fileStream = new infinity.stream('archive.zip', infinity.fileMode.create);

try {
  zipStream.add('test.txt');
  zipStream.add('test2.txt', 'data/test2.txt');
  fileStream.copyFrom(zipStream);
} finally {
  fileStream.free();
  zipStream.free();
}

Properties


comment

Type: string

Gets or sets the comment for the ZIP-archive (at most 65535 bytes).


position

Type: number

Gets or sets the current read position within the generated ZIP-archive.


size

Type: number

Gets the size of the generated ZIP-archive in bytes. Setting the size throws an error, because the stream is read-only.


fileCount (readonly)

Type: number

Gets the quantity of files contained inside the ZIP-archive.


fileList (readonly)

Type: infinity.zip.stream.fileArray

Gets the list of files added to the ZIP-archive.


Methods


constructor()

Lets you instantiate an empty INFINITY.JS ZIP stream object instance. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.zip');
let myStream = new infinity.zip.stream();

add()

Adds a file to the ZIP-archive. The file is read once to determine its size and CRC32 checksum; an error is thrown if it cannot be opened. The file must still exist and must not change until the stream has been read.

Signature:

add( localFileName: string, archiveFileName?: string, created?: number, modified?: number, lastAccess?: number, comment?: string ): void

Parameters:

  • localFileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be added.

  • archiveFileName: string, optional

    The name of the file inside the archive (may contain subdirectories separated by /). Default: the filename of localFileName without path.

  • created: number, optional

    Sets the file creation timestamp (Unix timestamp in milliseconds). Default: 0 (1970-01-01).

  • modified: number, optional

    Sets the timestamp of the last file modification (Unix timestamp in milliseconds). Default: 0 (1970-01-01).

  • lastAccess: number, optional

    Sets the timestamp of the last access to the file (Unix timestamp in milliseconds). Default: 0 (1970-01-01).

  • comment: string, optional

    Can contain a comment about the file inside the ZIP-archive.

Example:

myStream.add('report.pdf', 'reports/report.pdf', Date.now(), Date.now(), Date.now());

copyFrom()

Copies the specified number of bytes from the given stream into the current stream (like infinity.stream.copyFrom()). Because a ZIP stream is read-only, this throws an error unless there is nothing to copy. To read the generated archive, copy it into another stream instead, e.g. with fileStream.copyFrom(zipStream).

Signature:

copyFrom( stream: infinity.zip.stream, count?: number ): number

Parameters:

  • stream: infinity.zip.stream

    Stream object to copy from.

  • count: number, optional

    The number of bytes to copy. Default: 0 (complete stream).

Return type: number

Example:

myStream.copyFrom(stream);

free()

Frees up memory occupied by the INFINITY.JS ZIP stream object instance. The object must not be used afterwards: all other methods and properties then throw an error.

Signature:

free(): void

Example:

myStream.free();

Namespaces

infinity.zip.file

Interfaces:
Enums:

Interfaces

infinity.zip.file.stringArray

Extends: Array<string>

An array of strings.


Enums

infinity.zip.file.mode

The mode enums specify the mode in which a ZIP-archive is being opened.

Values:

  • closed: 0

    The archive is closed. Not allowed for open().

  • read: 1

    Specifies read only access to an existing archive.

  • readWrite: 2

    Specifies read and write access to an existing archive (files can be extracted and added).

  • write: 3

    Creates a new archive (an existing file is overwritten) for adding files.

Example:

infinity.loadModule('infinity.zip');
let myArchive = new infinity.zip.file();
myArchive.open('archive.zip', infinity.zip.file.mode.write);

infinity.zip.stream


Interfaces

infinity.zip.stream.fileArray

Extends: Array<{localFileName: string, archiveFileName: string, created: number, modified: number, lastAccess: number, comment: string, crc32: number, size: number}>

An array containing information about the files added to the archive as file objects

Properties:

  • localFileName

    Type: string. The path of the local file as passed to add().

  • archiveFileName

    Type: string. The name of the file inside the archive.

  • created

    Type: number. The file creation timestamp (Unix timestamp in milliseconds).

  • modified

    Type: number. The timestamp of the last file modification (Unix timestamp in milliseconds).

  • lastAccess

    Type: number. The timestamp of the last access to the file (Unix timestamp in milliseconds).

  • comment

    Type: string. A comment about the file inside the ZIP-archive.

  • crc32

    Type: number. The CRC32 checksum of the file.

  • size

    Type: number. The file size in bytes.