0.2.2 • Published 5 months ago

adv-zlib v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Features

  • šŸ”„šŸ”„šŸ”„ Optimized for large ZIP files: Handle big size ZIP files with considerable speed and low memory usage.
  • šŸ”„šŸ”„šŸ”„ Elegant way to handle nested ZIP files: Providing semantic APIs to work with deeply nested ZIP files like /a.zip/b.zip/c.txt.
  • šŸ”„šŸ”„ ESM-first with CJS compatibility: Fully embraces ESM, while also providing .cjs builds for CommonJS projects.
  • šŸ”„ Non-blocking: All I/O operations are implemented with Streams to maximize performance and scalability.
  • šŸ”„ Modern async/await APIs: Simplified APIs using async/await, no callbacks required.

Installation

npm install adv-zlib

Once the package installed, you can import the library using import or require:

// With import
import AdvZlib from 'adv-zlib';

// With require
const AdvZlib = require('adv-zlib');

Usage with Real-World Examples

Example 1: Extracting Specific Files from a Large ZIP Without Full Decompression

import AdvZlib from 'adv-zlib';
const advZlib = new AdvZlib();

const zipFilePath = '/path/to/bigsize.zip';
const targetFile = 'foo.txt';

// 1. Check if target file exist
if (await advZlib.exists(path.join(zipFilePath, targetFile))) {
  // 2. Read target file
  const content = (await advZlib.read(zipFilePath, targetFile)).toString();

  // 3. Do something with the content
  // ...
}

šŸ’” Why use adv-zlib?

Unlike traditional extraction methods, adv-zlib reads only the required file, significantly improving performance by avoiding full decompression.

Example 2:Handling Nested ZIP Files with a Clean and efficient API

import AdvZlib from 'adv-zlib';
const advZlib = new AdvZlib();

const zipFilePath = '/path/to/bigsize.zip';
const targetFiles = [
  'nest1.zip/nest2.zip/foo.txt',
  'nest1.zip/nest2.zip/bar.txt'
];

for (const targetFile of targetFiles) {
  // 1. Check if target file exist
  if (await advZlib.exists(path.join(zipFilePath, targetFile))) {
    // 2. Read target file
    const content = (await advZlib.read(zipFilePath, targetFile)).toString();

    // 3. Do something with the content
    // ...
  }
}

šŸ’” Optimized Nested ZIP Handling

adv-zlib caches decompressed buffers of nested ZIPs (e.g., nest1.zip and nest2.zip in this example). Once a nested ZIP is decompressed, future operations on the same ZIP reuse the cached data, significantly reducing processing time.

APIs

exists(): Check if a file exists in a ZIP

  • exists(path: string): Promise<boolean>

read(): Read content from an specific file within a ZIP file

  • read(path: string): Promise<Buffer>
  • read(path: string, filter: (entry: ZipEntry) => boolean): Promise<Buffer>

extract(): Extract a file or a folder from a ZIP file

  • extract(path: string): Promise<void>
  • extract(path: string, filter: (entry: ZipEntry) => boolean): Promise<void>

cleanup(): Clean up the caches

  • cleanup(): Promise<void>

Cache Mechanism

  • To avoid reanalyzing the same ZIP file multiple times, adv-zlib caches up to 10 CentralDir instances per ZIP file. Each instance consumes very little memory, so there is no need to worry about memory leaks.
  • To enhance performance when handling nested ZIP files, adv-zlib caches decompressed buffers of nested ZIP files in a designated folder (default: node_modules/.cache/adv-zlib/).
  • Remember to call cleanup() to clear the caches once all ZIP files have been processed.
  • Please raise an issue if you find any bugs or have suggestions for improvements.

Upcoming

  • Compression APIs: passible based on archiver to provide several sementic APIs for zip compression.
  • Encryption APIs: would provide APIs like isEncrypted() and add new arguments password to existed APIs to support encrypted zip files.

Report Issue

Feel free to raise an issue if you find any bugs or have suggestions for improvements. I will reponse/fix them as soon as possible.

License

MIT

0.2.1

5 months ago

0.2.2

5 months ago

0.1.12

6 months ago

0.1.13

6 months ago

0.2.0

6 months ago

0.1.11

6 months ago

0.1.10

6 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago