Compress Commons Archive Utilities

7.0.0 · active · verified Sun Apr 19

compress-commons is a Node.js library that provides a common, low-level interface for defining and working with various archive formats, particularly ZIP, within a Node.js environment. Inspired by the established Java library Apache Commons Compress, it aims to offer foundational building blocks and structures (like file headers and entry definitions) for handling archive data. The current stable version is 7.0.0, which transitioned to being an ESM-only package and requires Node.js v18 or newer. While its release cadence is regular, it does not follow a strict schedule. This library differentiates itself by focusing on the underlying components and specifications of archive formats, rather than providing a high-level API for creating or extracting archives directly, making it suitable for developers who need granular control over archive manipulation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic ZIP archive entry using `Headers` and `Entry` classes. It creates a mock file and then defines its corresponding archive header and entry object. It's a low-level definition, not a complete archiving process.

import { Entry, Headers } from 'compress-commons/zip';
import { pipeline } from 'stream/promises';
import { createReadStream, createWriteStream, promises as fsPromises } from 'fs';
import path from 'path';

async function createSimpleZipEntryDefinition() {
  const filePath = path.join(process.cwd(), 'temp_file.txt');
  const outputZipPath = path.join(process.cwd(), 'output.zip');
  const fileContent = 'This is a test file for compress-commons quickstart.';

  // Create a dummy file to simulate content
  await fsPromises.writeFile(filePath, fileContent);

  // Define the header for the ZIP entry
  const header = new Headers({
    path: 'my-entry.txt', // Name of the file inside the archive
    comment: 'A sample entry.',
    size: Buffer.byteLength(fileContent), // Uncompressed size
    crc32: 0, // Placeholder, actual CRC32 calculated by archiver
  });

  // Create an Entry object. In a real scenario, this would be fed into an archiver stream.
  const entry = new Entry(header, {
    type: 'file',
    source: createReadStream(filePath), // Source can be a stream or Buffer
  });

  console.log(`Defined ZIP entry: ${entry.header.path}`);
  console.log(`Entry size: ${entry.header.size} bytes`);

  // This library provides common interfaces, not a full archiver.
  // To actually create a .zip file, you'd typically use a library like 'archiver'
  // that consumes these 'Entry' objects.
  // For demonstration, we'll just show the entry definition.
  // A full archiver would look something like this (conceptual):
  // import archiver from 'archiver';
  // const archive = archiver('zip', { zlib: { level: 9 } });
  // archive.pipe(createWriteStream(outputZipPath));
  // archive.append(entry.source, { name: entry.header.path });
  // await archive.finalize();
  // console.log(`
Conceptually, 'my-entry.txt' would be added to a ZIP archive.`);
  
  await fsPromises.unlink(filePath);
}

createSimpleZipEntryDefinition().catch(console.error);

view raw JSON →