Zips up directories into buffers or saves zipped files to disk
npx @tessl/cli install tessl/npm-zip-dir@2.0.0Zip Dir is a Node.js library that recursively zips directories into buffers while preserving directory structure. It provides both synchronous and asynchronous operation modes through callbacks and promises, with support for custom file filtering, progress tracking, and flexible output options.
npm install zip-dirconst zipdir = require('zip-dir');ES6 modules:
import zipdir from 'zip-dir';const zipdir = require('zip-dir');
// Promise-based usage
const buffer = await zipdir('/path/to/directory');
// Callback-based usage
zipdir('/path/to/directory', function(err, buffer) {
if (err) throw err;
console.log('Zip created successfully!');
});
// Save directly to disk
zipdir('/path/to/directory', { saveTo: './output.zip' }, function(err, buffer) {
if (err) throw err;
console.log('Zip saved to disk!');
});Zip Dir is built around several key components that work together to provide efficient and reliable directory compression:
Creates a compressed zip archive from a directory, recursively including all files and subdirectories while preserving the directory structure.
/**
* Zips up a directory recursively preserving directory structure
* @param {string} rootDir - Path to directory to be zipped (resolved internally)
* @param {ZipOptions|function} [options] - Configuration options or callback function
* @param {function} [callback] - Callback function with signature (error, buffer)
* @returns {Promise<Buffer>|undefined} Promise resolving to zip buffer when no callback provided, undefined when callback used
*/
function zipdir(rootDir, options, callback);Parameter Handling:
options.saveTo is provided, the callback/promise waits for both zip creation and file write completionOptions:
interface ZipOptions {
/** Path to save the zip file to disk (uses binary encoding) */
saveTo?: string;
/**
* Function to filter files/directories during traversal
* @param fullPath - Absolute path to the file or directory
* @param stats - Node.js fs.Stats object with file metadata
* @returns true to include item in zip, false to exclude
* @note Must return true for directories to allow traversal into subdirectories
*/
filter?(fullPath: string, stats: fs.Stats): boolean;
/**
* Function called for each item added to the zip
* @param path - For files: path relative to root directory (e.g., "subdir/file.txt")
* - For directories: full absolute path of the directory
*/
each?(path: string): void;
}Usage Examples:
const zipdir = require('zip-dir');
// Filter out temporary files and keep only JSON files
zipdir('/path/to/directory', {
filter: (path, stat) => {
// Allow directories to be traversed
if (stat.isDirectory()) return true;
// Only include JSON files
return path.endsWith('.json');
}
}, function(err, buffer) {
console.log('Filtered zip created');
});
// Track progress with each callback
zipdir('/path/to/directory', {
each: (path) => console.log('Added:', path),
saveTo: './archive.zip'
}, function(err, buffer) {
console.log('Archive created with progress tracking');
});
// Promise-based with options
try {
const buffer = await zipdir('/path/to/directory', {
filter: (path, stat) => !path.includes('.git'),
each: (path) => console.log('Processing:', path)
});
console.log('Zip buffer size:', buffer.length);
} catch (error) {
console.error('Error creating zip:', error);
}The function handles various error conditions and follows Node.js error-first callback conventions. Errors stop processing immediately and are propagated through the callback/promise chain.
Common Error Scenarios:
saveTo optionError Handling Examples:
// Callback error handling
zipdir('/nonexistent/path', function(err, buffer) {
if (err) {
console.error('Failed to create zip:', err.message);
return;
}
console.log('Success!');
});
// Promise error handling
try {
const buffer = await zipdir('/path/to/directory');
} catch (error) {
console.error('Zip creation failed:', error.message);
}/**
* Node.js fs.Stats object containing file/directory metadata
*/
interface fs.Stats {
isFile(): boolean;
isDirectory(): boolean;
size: number;
mtime: Date;
// ... other fs.Stats properties
}type: 'nodebuffer' outputpath.resolve() to handle relative paths and trailing slashes consistentlyfs.readdir(), fs.stat(), and fs.readFile() for directory traversal and file readingtrue for directories to allow traversal into subdirectorieseach callback receives path relative to root directory (e.g., "subdir/file.txt")each callback receives full absolute path of the directory being processedsaveTo option is used, writes with { encoding: 'binary' } to preserve zip file integrity