Module basic module

module zip

The zip module is a module for file compression and decompression. It provides operations such as compressing, decompressing, finding and enumerating file lists in zip files.

Through the zip module, we can package multiple files into a zip file, and we can also decompress the zip file to restore the original file.

Here are some examples:

1. Compressed files:

1 2 3 4 5 6
var zip = require('zip'); var zipfile = zip.open('/path/to/dest.zip', 'w'); zipfile.write('/path/to/src1', 'src1'); zipfile.write('/path/to/src2', 'src2'); zipfile.close();

2. Unzip the file:

1 2 3 4 5 6 7 8 9 10
var zip = require('zip'); var zipfile = zip.open('/path/to/src.zip', 'r'); var filenames = zipfile.namelist(); for (var i = 0; i < filenames.length; ++i) { var filename = filenames[i]; var data = zipfile.read(filename); console.log(filename + ': ' + data.length + ' bytes'); } zipfile.close();

static function

isZipFile

Determine whether the file is in zip format

1
static Boolean zip.isZipFile(String filename) async;

Call parameters:

  • filename: String, file name

Return results:

  • Boolean, returning true means the file is a zip file

open

Open a zip file

1 2 3
static ZipFile zip.open(String path, String mod = "r", String codec = "utf8") async;

Call parameters:

  • path: String, file path
  • mod: String, open file mode, "r" represents reading, "w" represents creating, "a" represents appending to the zip file
  • codec: String, set the zip file encoding method, the default is "utf8"

Return results:


Open a zip file

1 2 3
static ZipFile zip.open(Buffer data, String mod = "r", String codec = "utf8") async;

Call parameters:

  • data:Buffer, zip file data
  • mod: String, open file mode, "r" represents reading, "w" represents creating, "a" represents appending to the zip file
  • codec: String, set the zip file encoding method, the default is "utf8"

Return results:


Open a zip file

1 2 3
static ZipFile zip.open(SeekableStream strm, String mod = "r", String codec = "utf8") async;

Call parameters:

  • strm:SeekableStream, zip file stream
  • mod: String, open file mode, "r" represents reading, "w" represents creating, "a" represents appending to the zip file
  • codec: String, set the zip file encoding method, the default is "utf8"

Return results: