Skip to content

ADM ZIP

cthackers edited this page Mar 1, 2012 · 16 revisions

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");

[ADM-ZIP] constructor(String 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();

[Array] getEntries()

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.

[Buffer] 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  
});

[String] readAsTextAsync(Object entry, Function callback, String encoding = 'utf8')

Asynchronous readAsText

[void] deleteFile(Object entry, Boolean writeZip = false)

[void] addZipComment(String comment, Boolean writeZip = false)

[String] getZipComment( )

[void] addZipEntryComment(Object entry, String comment, Boolean writeZip = false)

[void] getZipEntryComment(Object entry)

[void] updateFile(Object entry, Buffer content, Boolean writeZip = false)

[void] addLocalFile(String localPath, Boolean writeZip = false)

[void] addLocalFolder(String localPath, Boolean writeZip = false)

[void] addFile(String entryName, Buffer content, String comment = '', Boolean writeZip = false)

[void] extractEntryTo(Object entry, String targetPath, Boolean maintainEntryPath = true, Boolean overwrite = false)

Etracts 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);

[void] extractAllTo(String targetPath, Boolean overwrite = false)

Extracts the entire archive to location specifed 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)

[Buffer] toBuffer()

Clone this wiki locally