Comprehensive functionality for reading and inspecting existing ZIP archives, accessing file metadata, and retrieving individual entry content both synchronously and asynchronously.
Create AdmZip instance from existing archive file, buffer data, or with custom options.
/**
* Create AdmZip instance from existing archive
* @param {string|Buffer|object} input - File path, buffer data, or options object
* @param {object} [options] - Configuration options
*/
function AdmZip(input, options);Usage Examples:
const AdmZip = require("adm-zip");
// From file path
const zip = new AdmZip("./archive.zip");
// From buffer
const buffer = fs.readFileSync("./archive.zip");
const zip = new AdmZip(buffer);
// With custom file system (Electron support)
const OriginalFs = require("original-fs");
const zip = new AdmZip("./archive.zip", { fs: OriginalFs });
// With options object
const zip = new AdmZip({
input: "./archive.zip",
noSort: true,
readEntries: true
});Retrieve array of all entries in the archive with optional password support.
/**
* Get all entries in the archive
* @param {string} [password] - Password for encrypted archives
* @returns {ZipEntry[]} Array of ZipEntry objects
*/
getEntries(password);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Get all entries
const entries = zip.getEntries();
entries.forEach(entry => {
console.log(entry.entryName);
console.log(`Size: ${entry.header.size}`);
console.log(`Compressed: ${entry.header.compressedSize}`);
console.log(`Directory: ${entry.isDirectory}`);
});
// Password-protected archives
const protectedEntries = zip.getEntries("password123");Retrieve specific entry by name or path within the archive.
/**
* Get specific entry by name
* @param {string} name - Entry name/path within archive
* @returns {ZipEntry|null} ZipEntry object or null if not found
*/
getEntry(name);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Get specific file
const entry = zip.getEntry("data/config.json");
if (entry) {
console.log("Found:", entry.entryName);
console.log("Size:", entry.header.size);
}
// Check if entry exists
const logoEntry = zip.getEntry("images/logo.png");
if (logoEntry) {
// Process the logo file
const logoData = logoEntry.getData();
}Extract and read entry content as Buffer with optional password support.
/**
* Read entry content as Buffer
* @param {ZipEntry|string} entry - ZipEntry object or entry name
* @param {Buffer|string} [pass] - Password for encrypted entries
* @returns {Buffer|null} Entry content as Buffer or null if error
*/
readFile(entry, pass);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Read by entry name
const content = zip.readFile("data.txt");
if (content) {
console.log(content.toString("utf8"));
}
// Read by ZipEntry object
const entry = zip.getEntry("config.json");
const jsonData = zip.readFile(entry);
if (jsonData) {
const config = JSON.parse(jsonData.toString("utf8"));
}
// Password-protected file
const protectedData = zip.readFile("secret.txt", "password123");Extract entry content and convert to text string with encoding support.
/**
* Read entry content as text string
* @param {ZipEntry|string} entry - ZipEntry object or entry name
* @param {string} [encoding] - Text encoding (default: "utf8")
* @returns {string} Entry content as string
*/
readAsText(entry, encoding);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Read as UTF-8 text
const textContent = zip.readAsText("readme.txt");
console.log(textContent);
// Read with specific encoding
const latinText = zip.readAsText("document.txt", "latin1");
// Read JSON file
const jsonText = zip.readAsText("data.json");
const data = JSON.parse(jsonText);Read entry content asynchronously with callback support.
/**
* Read entry content asynchronously
* @param {ZipEntry|string} entry - ZipEntry object or entry name
* @param {function} callback - Callback function (data, error)
*/
readFileAsync(entry, callback);
/**
* Read entry content as text asynchronously
* @param {ZipEntry|string} entry - ZipEntry object or entry name
* @param {function} callback - Callback function (text, error)
* @param {string} [encoding] - Text encoding (default: "utf8")
*/
readAsTextAsync(entry, callback, encoding);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Async file reading
zip.readFileAsync("large-file.dat", function(data, err) {
if (err) {
console.error("Error reading file:", err);
return;
}
console.log("File size:", data.length);
// Process the data
});
// Async text reading
zip.readAsTextAsync("log.txt", function(text, err) {
if (err) {
console.error("Error:", err);
return;
}
console.log("Log content:", text);
}, "utf8");Get general information about the archive structure and contents.
/**
* Get total number of entries in archive
* @returns {number} Total entry count
*/
getEntryCount();
/**
* Get number of child entries for a directory
* @param {ZipEntry|string} entry - Directory entry
* @returns {number} Number of child entries
*/
childCount(entry);
/**
* Iterate over all entries with callback
* @param {function} callback - Callback function for each entry
*/
forEach(callback);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Get total entry count
console.log("Total entries:", zip.getEntryCount());
// Count children in directory
const folderEntry = zip.getEntry("documents/");
if (folderEntry) {
console.log("Files in documents:", zip.childCount(folderEntry));
}
// Iterate over all entries
zip.forEach(function(entry) {
console.log(`${entry.entryName} - ${entry.header.size} bytes`);
});Test archive integrity and validate entry data.
/**
* Test archive integrity
* @param {string} [pass] - Password for encrypted archives
* @returns {boolean} True if archive is valid
*/
test(pass);Usage Examples:
const zip = new AdmZip("./archive.zip");
// Test archive integrity
if (zip.test()) {
console.log("Archive is valid");
} else {
console.log("Archive is corrupted");
}
// Test password-protected archive
if (zip.test("password123")) {
console.log("Archive and password are valid");
}