Skip to content

ADM ZIP

The Brain edited this page Nov 19, 2020 · 16 revisions
  • adm-zip.js
    • constructor(filePath)
    • getEntries(): Array
    • getEntry(name): ZipObject
    • readFile(entry): Buffer
    • readFileAsync(entry, callback)
    • readAsText(entry, encoding = 'utf8')
    • readAsTextAsync(entry, callback, encoding = 'utf8')
    • deleteFile(entry)
    • addZipComment(comment)
    • getZipComment(): String
    • addZipEntryComment(entry, comment)
    • getZipEntryComment(entry): String
    • updateFile(entry, content)
    • addLocalFile(localPath, zipPath)
    • addLocalFolder(localPath, zipPath)
    • addFile(entryName, content, comment, attr)
    • extractEntryTo(entry, targetPath, maintainEntryPath, overwrite = false, outFileName)
    • extractAllTo(targetPath, overwrite = false)
    • writeZip(targetFileName)
    • toBuffer(onSuccess, onFail, onItemStart, onItemEnd): Buffer

Introduction

ADM-ZIP is the main and the only class that should be used. It provides almost all methods required for you to use with a zip archive.

var Zip = require("adm-zip");

constructor(filePath)

The class constructor can have one or no arguments specified. If a argument of type String is provided, the class will try to read the file at filePath and parse it as a zip.

If no argument is specified, then a new in memory zip will be created.

Examples:

// loads and parses existing zip file local_file.zip
var zip = new Zip("local_file.zip"); 
// creates new in memory zip
var zip = new Zip();

getEntries(): Array

Returns an array of ZipEntry objects representing the files and folders inside the archive.

<ZipObject> getEntry(String name)

Returns a ZipEntry object representing the file or folder specified by name.

<Buffer> readFile(Object entry)

Extracts the given entry from the archive and returns the content as a Buffer object.

The entry argument can be either a ZipEntry object or String with the entry name.

<void> readFileAsync(Object entry, Function callback)

Asynchronous readFile.

<String> readAsText(Object entry, String encoding = 'utf8')

Extracts the given entry from the archive and returns the content as plain text in the given encoding. If no encoding is specified, utf8 will be used.

The entry argument can be either a ZipEntry object or String with the entry name.

Examples:

// loads and parses existing zip file local_file.zip
var zip = new Zip("local_file.zip"); 
// get all entries and iterate them
zip.getEntries().forEach(function(entry) {
    var entryName = entry.entryName;
    var decompressedData = zip.readFile(entry); // decompressed buffer of the entry
    console.log(zip.readAsText(entry)); // outputs the decompressed content of the entry  
});

<void> readAsTextAsync(Object entry, Function callback, String encoding = 'utf8')

Asynchronous readAsText.

<void> deleteFile(Object entry)

Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory.

The entry argument can be either a ZipEntry object or String with the entry name.

<void> addZipComment(String comment)

Adds a comment to the zip. The zip must be rewritten after adding the comment.

<String> getZipComment( )

Returns the zip comment.

<void> addZipEntryComment(Object entry, String comment)

Adds a comment to a specified entry. The zip must be rewritten after adding the comment and the comment cannot exceed 65535 characters in length.

The entry argument can be either a ZipEntry object or String with the entry name.

<void> getZipEntryComment(Object entry)

Returns the comment of the specified entry.

The entry argument can be either a ZipEntry object or String with the entry name.

<void> updateFile(Object entry, Buffer content)

Updates the content of an existing entry inside the archive. The zip must be rewritten after updating the content.

The entry argument can be either a ZipEntry object or String with the entry name.

<void> addLocalFile(String localPath, String zipPath)

Adds a file from the disk to the archive.

<void> addLocalFolder(String localPath, String zipPath)

Adds a local directory and all its nested files and directories to the archive.

<void> addFile(String entryName, Buffer content, String comment, Number attr)

Allows you to programmatically create a entry (file or directory) in the zip file.

If you want to create a directory the entryName must end in / and a null buffer should be provided.

comment and attributes are optional.

<void> extractEntryTo(Object entry, String targetPath, Boolean maintainEntryPath = true, Boolean overwrite = false, String outFileName)

Extracts the given entry to the given targetPath. If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted.

The entry argument can be either a ZipEntry object or String with the entry name. If maintainEntryPath is TRUE and the entry is inside a folder, the entry folder will be created in targetPath as well. If the files to be extracted already exist at the target path, the files may be overwritten if specified so by the overwrite argument.

// will extract the file myfile.txt from the archive to /home/user/folder/subfolder/myfile.txt
zip.extractEntryTo("folder/subfolder/myfile.txt", "/home/user/", true, true);

// will extract the file myfile.txt from the archive to /home/user/myfile.txt
zip.extractEntryTo("folder/subfolder/myfile.txt", "/home/user/", false, true);

// will extract the file newname.txt from the archive to /home/user/myfile.txt
zip.extractEntryTo("folder/subfolder/myfile.txt", "/home/user/", false, true, "newname.txt");

<void> extractAllTo(String targetPath, Boolean overwrite = false)

Extracts the entire archive to location specified by targetPath.

If the files to be extracted already exist at the target path, the files may be overwritten if specified so by the overwrite argument.

<void> writeZip(String targetFileName)

Writes the newly created zip file to disk at the specified location or if a zip was opened and no targetFileName is provided, it will overwrite the opened zip.

<Buffer> toBuffer(Function onSuccess, Function onFail, Function onItemStart, Function onItemEnd)

Returns the content of the entire zip file as a Buffer object. In async mode:

onSuccess(Buffer buffer);
onFail(String error);
onItemStart(String fileName);
onItemEnd(String fileName);