Detailed options and capabilities for each built-in archive format: ZIP, TAR, and JSON.
ZIP format provides compressed archive support with configurable compression levels and various ZIP-specific options.
/**
* ZIP format-specific options
*/
interface ZipOptions {
/** ZIP archive comment */
comment?: string;
/** Use local time instead of UTC (default: false) */
forceLocalTime?: boolean;
/** Force ZIP64 headers for large archives (default: false) */
forceZip64?: boolean;
/** Prepend slash to entry paths (default: false) */
namePrependSlash?: boolean;
/** Use STORE compression - no compression (default: false) */
store?: boolean;
/** Options passed to zlib for compression */
zlib?: {
/** Compression level 0-9 (0=no compression, 9=max compression) */
level?: number;
/** Chunk size for compression */
chunkSize?: number;
/** Window bits for compression */
windowBits?: number;
/** Memory level for compression */
memLevel?: number;
/** Compression strategy */
strategy?: number;
};
}/**
* ZIP entry-specific options (extends base EntryData)
*/
interface ZipEntryData extends EntryData {
/** Override global store setting for this entry */
store?: boolean;
/** Override global namePrependSlash setting for this entry */
namePrependSlash?: boolean;
}const archiver = require('archiver');
// Maximum compression ZIP archive
const zipMax = archiver('zip', {
zlib: { level: 9 } // Maximum compression
});
// Fast compression with custom options
const zipFast = archiver('zip', {
zlib: {
level: 1, // Fast compression
chunkSize: 1024,
windowBits: 15,
memLevel: 8
},
comment: 'Generated by my application',
forceLocalTime: true
});
// No compression (store only)
const zipStore = archiver('zip', {
store: true // No compression, fastest
});
// ZIP64 for large archives
const zipLarge = archiver('zip', {
forceZip64: true, // Support archives > 4GB
zlib: { level: 6 }
});
// Adding entries with ZIP-specific options
zipMax.append('content', {
name: 'file.txt',
store: false // Compress this entry even if global store is true
});
zipMax.append('binary data', {
name: 'data.bin',
store: true, // Don't compress this entry
namePrependSlash: true // Force slash prefix
});TAR (Tape Archive) format provides uncompressed archive support with optional gzip compression.
/**
* TAR format-specific options
*/
interface TarOptions {
/** Compress TAR using gzip (default: false) */
gzip?: boolean;
/** Options passed to zlib.createGzip() when gzip is true */
gzipOptions?: {
/** Compression level 0-9 */
level?: number;
/** Chunk size for compression */
chunkSize?: number;
/** Window bits for compression */
windowBits?: number;
/** Memory level for compression */
memLevel?: number;
/** Compression strategy */
strategy?: number;
};
}/**
* TAR entry options (extends base EntryData)
*/
interface TarEntryData extends EntryData {
/** Entry type: 'file', 'directory', 'symlink' */
type?: 'file' | 'directory' | 'symlink';
/** For symlink entries, the target path */
linkname?: string;
}const archiver = require('archiver');
// Plain TAR archive (no compression)
const tar = archiver('tar');
// TAR with gzip compression
const tarGz = archiver('tar', {
gzip: true,
gzipOptions: {
level: 6, // Balanced compression
chunkSize: 1024
}
});
// TAR with maximum gzip compression
const tarGzMax = archiver('tar', {
gzip: true,
gzipOptions: {
level: 9 // Maximum compression
}
});
// Adding entries to TAR (same as other formats)
tarGz
.append('Hello TAR', { name: 'hello.txt' })
.file('/path/to/file', { name: 'archived-file.txt' })
.directory('/path/to/dir', 'archived-dir');JSON format provides metadata-only archiving, outputting entry information as structured JSON data.
/**
* JSON format options (uses base Transform options only)
*/
interface JsonOptions {
// No format-specific options beyond base Transform stream options
highWaterMark?: number;
objectMode?: boolean;
}/**
* JSON entry data with automatically calculated fields
*/
interface JsonEntryData extends EntryData {
/** Entry type: 'file', 'directory', 'symlink' */
type?: 'file' | 'directory' | 'symlink';
/** CRC32 checksum of entry content (automatically calculated) */
crc32?: number;
/** Size of entry content in bytes (automatically calculated) */
size?: number;
/** For symlink entries, the target path */
linkname?: string;
}The JSON format outputs an array of entry metadata objects:
[
{
"name": "file.txt",
"date": "2023-01-01T12:00:00.000Z",
"mode": 33188,
"crc32": 123456789,
"size": 11,
"type": "file"
},
{
"name": "directory/",
"date": "2023-01-01T12:00:00.000Z",
"mode": 16877,
"type": "directory"
}
]const archiver = require('archiver');
const fs = require('fs');
// Create JSON metadata archive
const jsonArchive = archiver('json');
const output = fs.createWriteStream('metadata.json');
jsonArchive.pipe(output);
// Add various content types
jsonArchive
.append('Hello World', { name: 'hello.txt' })
.file('/path/to/document.pdf', { name: 'document.pdf' })
.directory('/path/to/images', 'images')
.symlink('link.txt', 'target.txt');
jsonArchive.finalize().then(() => {
console.log('JSON metadata archive created');
// Output file contains JSON array with entry metadata
});
// Reading the JSON output
jsonArchive.on('data', (chunk) => {
// JSON data is streamed as it's generated
console.log('JSON chunk:', chunk.toString());
});| Feature | ZIP | TAR | JSON |
|---|---|---|---|
| Compression | ✓ (zlib) | ✓ (gzip) | ✗ |
| Directories | ✓ | ✓ | ✓ (metadata) |
| Symlinks | ✓ | ✓ | ✓ (metadata) |
| Large Files | ✓ (ZIP64) | ✓ | ✓ (metadata) |
| Standards | ZIP 2.0+ | POSIX.1-1988 | JSON |
| Use Cases | General archives | Unix/Linux archives | Metadata analysis |
| File Content | ✓ | ✓ | ✗ (metadata only) |
Format-specific errors that may be thrown:
DIRECTORYNOTSUPPORTED - Format doesn't support directory entriesSYMLINKNOTSUPPORTED - Format doesn't support symlink entriesENTRYNOTSUPPORTED - Entry type not supported by formatstore: true option provides fastest processing with no compression