wilton.js  v202103141
 All Namespaces Functions
Functions
zip Namespace Reference

wilton/zip
Read and write ZIP files More...

Functions

Array listFileEntries (String path, Function|Undefined callback)
 List all the entries names from a ZIP file.
Object readFile (String path, Object|Undefined options, Function|Undefined callback)
 Read the entire contents of a ZIP file.
String readFileEntry (String path, String entry, Object|Undefined options, Function|Undefined callback)
 Read the contents of a specified entry from a ZIP file.
Undefined unzipFile (String path, String destPath, Function|Undefined callback)
 Unzip ZIP file into FS.
Undefined unzipFileEntries (String path, Object entries, Function|Undefined callback)
 Unzip the specified ZIP entries into FS.
Undefined writeFile (String path, Object entries, Object|Undefined options, Function|Undefined callback)
 Write the specified set of entries into ZIP file.

Detailed Description

This module allows to read and write files in ZIP format.

Usage example:

// create ZIP file
zip.writeFile("test.zip", {
foo: "bar",
"bazdir1/bazdir2/bazfile": "boo"
});
// list entries in ZIP file
zip.listFileEntries("test.zip");
// read single ZIP entry from file
zip.readFileEntry("test.zip", "foo");
// read all contens of ZIP file
zip.readFile("test.zip");

Function Documentation

Array zip::listFileEntries ( String  path,
Function|Undefined  callback 
)

Reads a central directory record from a ZIP file and returns a list of ZIP entry names.

Parameters
pathString path to file
callbackFunction|Undefined callback to receive result or error
Returns
Array list of ZIP entry names
Object zip::readFile ( String  path,
Object|Undefined  options,
Function|Undefined  callback 
)

Reads the entire contents of a ZIP file into "zip_entry_name" -> "zip_entry_contents" mapping. "zip_entry_contents" may be optionally encoded into hexadecimal (useful for binary data) if hex: true option is specified.

Parameters
pathString path to file
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Object "zip_entry_name" -> "zip_entry_contents" mapping

Options

  • hex Boolean whether data read from ZIP entries needs to be converted to HEX format before returning it to caller; false by default
String zip::readFileEntry ( String  path,
String  entry,
Object|Undefined  options,
Function|Undefined  callback 
)

Reads the contents of a specified entry from a ZIP file. Resulting string may be optionally encoded into hexadecimal (useful for binary data) if hex: true option is specified.

Parameters
pathString path to file
entryString entry name inside the ZIP file
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
String entry contents

Options

  • hex Boolean whether data read from specified ZIP entry needs to be converted to HEX format before returning it to caller; false by default
Undefined zip::unzipFile ( String  path,
String  destPath,
Function|Undefined  callback 
)

Unzips all entries from the specified ZIP file into filesystem.

Parameters
pathString path to file
destPathString path to the directory that will correspond to the root entry of the ZIP file
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined zip::unzipFileEntries ( String  path,
Object  entries,
Function|Undefined  callback 
)

Takes the mapping of src_path_in_zip -> dest_path_in_fs and decompresses the specified entries to specifieed filesystems paths.

Parameters
pathString path to file
entriesObject src_path_in_zip -> dest_path_in_fs
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined zip::writeFile ( String  path,
Object  entries,
Object|Undefined  options,
Function|Undefined  callback 
)

Writes the specified "zip_entry_name" -> "zip_entry_contents" mapping into ZIP file. "zip_entry_contents" may be optionally decoded from hexadecimal (useful for binary data) if hex: true option is specified.

Entries are sorted by keys in alphabetical order.

Parameters
pathString path to file
entriesObject "zip_entry_name" -> "zip_entry_contents" mapping
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • hex Boolean|Undefined whether ZIP entry data needs to be converted to to HEX format before wrting it to ZIP file; false by default
  • fsPaths Boolean|Undefined whether ZIP entry data represents paths in filesystem that should be read and compressed into ZIP file; false by default