Core functionality for creating and managing archive instances with built-in format support and custom format registration.
Creates a new Archiver instance with the specified format and options.
/**
* Creates a new Archiver instance
* @param {string} format - Archive format ('zip', 'tar', 'json', defaults to 'zip')
* @param {Object} options - Format-specific options
* @returns {Archiver} Archiver instance that is a Transform stream
*/
function archiver(format, options);Usage Examples:
const archiver = require('archiver');
// Create ZIP archive with compression
const zipArchive = archiver('zip', {
zlib: { level: 9 } // Maximum compression
});
// Create TAR archive with gzip compression
const tarArchive = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1,
chunkSize: 1024,
windowBits: 15,
memLevel: 8
}
});
// Create JSON metadata archive
const jsonArchive = archiver('json');
// Default to ZIP format
const defaultArchive = archiver();Alternative method for creating Archiver instances, functionally identical to the main factory function.
/**
* Creates a new Archiver instance (alternative method)
* @param {string} format - Archive format
* @param {Object} options - Format-specific options
* @returns {Archiver} Archiver instance
*/
archiver.create(format, options);Usage Examples:
// Equivalent to archiver('zip', options)
const archive = archiver.create('zip', {
zlib: { level: 5 }
});Registers a custom archive format plugin for use with archiver.
/**
* Registers a custom archive format
* @param {string} format - Format name to register
* @param {Function} module - Format module constructor function
* @throws {Error} If format already registered or module invalid
*/
archiver.registerFormat(format, module);Requirements for Format Modules:
append(data, source, callback) methodfinalize() methodUsage Examples:
// Custom format implementation
function CustomFormat(options) {
this.options = options || {};
}
CustomFormat.prototype.append = function(data, source, callback) {
// Implementation for appending entries
callback();
};
CustomFormat.prototype.finalize = function() {
// Implementation for finalizing archive
};
// Register the custom format
archiver.registerFormat('custom', CustomFormat);
// Now you can use the custom format
const customArchive = archiver('custom', { customOption: true });Checks if a format is already registered and available for use.
/**
* Checks if a format is already registered
* @param {string} format - Format name to check
* @returns {boolean} True if format is registered, false otherwise
*/
archiver.isRegisteredFormat(format);Usage Examples:
// Check built-in formats
console.log(archiver.isRegisteredFormat('zip')); // true
console.log(archiver.isRegisteredFormat('tar')); // true
console.log(archiver.isRegisteredFormat('json')); // true
console.log(archiver.isRegisteredFormat('custom')); // false (unless registered)
// Check before registering to avoid errors
if (!archiver.isRegisteredFormat('myformat')) {
archiver.registerFormat('myformat', MyFormatModule);
}Archiver comes with three built-in formats that are automatically registered:
'zip''tar''json'Format-related errors that may be thrown:
'create(format): format not registered' - Attempting to create archive with unregistered format'register(format): format already registered' - Attempting to register a format that already exists'register(format): format module invalid' - Format module is not a function'register(format): format module missing methods' - Format module missing required append or finalize methods