npm.io
4.10.0 • Published 19h ago

redzip

Licence
MIT
Version
4.10.0
Deps
0
Vulns
0
Weekly
1.2K
Stars
6

Redzip License NPM version Build Status Coverage Status

Work with zip, tar, tar.gz, tar.bz2, rar archives and FAT images (.img, .fat) as it is regular files and directories.

Supported path patterns: /home/user/hello.zip/readme.md and /home/user/disk.fat/file.txt, /home/user/hello.tar/file.txt /home/user/hello.tar.gz/file.txt, /home/user/hello.tar.bz2/file.txt and /home/user/hello.rar/file.txt.

Install

npm i redzip

API

read(path[, options])
  • path - string
  • options - object can contain:
    • sort - sort by: name, size, date
    • order - "asc" or "desc" for ascending and descending order (default: "asc")
    • type - when "raw" returns not formatted result
    • root - root directory to cut from result
Example
import * as redzip from 'redzip';

const sort = 'size';
const order = 'desc';
const type = 'raw';
const dirPath = '/home/coderaiser/hello.zip/hello/';
const path = '/home/coderaiser/hello.zip/hello.txt';

const dirStream = await redzip.read(dirPath, {
    type,
    sort,
    order,
});

console.log(dirStream.type);
// outputs
'directory';

console.log(dirStream.contentLength);

// outputs
280;

dirStream.pipe(process.stdout);

// outputs
({
    path: '/',
    files: [{
        name: 'redzip.js',
        size: 4735,
        date: 1_377_248_899_000,
        owner: 0,
        mode: 0,
    }, {
        name: 'readify.js',
        size: 3735,
        date: 1_377_248_899_000,
        owner: 0,
        mode: 0,
    }],
});

dirStream.files;
// returns
[{
    name: 'redzip.js',
    size: 4735,
    date: 1_377_248_899_000,
    owner: 0,
    mode: 0,
}, {
    name: 'readify.js',
    size: 3735,
    date: 1_377_248_899_000,
    owner: 0,
    mode: 0,
}];

dirStream.path;
// returns
'/';

const fileStream = await redzip.read(path);
console.log(fileStream.type);
// outputs
'file';

fileStream.pipe(process.stdout);
// outputs
'hello';

console.log(fileStream.contentLength);

// outputs
6;
readSize(path[, options])
  • path - string
  • options - object can contain:
    • type - when "raw" returns not formatted result
const fileStream = readSize('/hello/world.zip/readme.md');

fileStream.pipe(process.stdout);
// outputs
'10kb';

fileStream.contentLength; // 4
readStat(path[, options])
  • path - string
const stat = readStat('/hello/world.zip/readme.md');

// returns
({
    date: '2021-01-18T22:00:00.000Z',
    mode: 1_107_099_648,
    name: 'readme.md',
    owner: 0,
    size: 0,
    type: 'file',
});
write(path[, data], options)

When you need to save string, or Buffer use Readable.from.

  • path - string
  • data - stream
  • options - object can contain:
    • unzip - unzip file content before writing
    • mode - set permissions mode (optional)
    • date - date of modication
    • limit - max size in bytes of the content buffered in memory, 100MB by default; throws EFBIG when exceeded (used by .fat and .tar.bz2 paths)
Example
import {Readable} from 'node:stream';
import {createGzip} from 'node:zlib';
import {write} from 'redzip';
import pullout from 'pullout';

const dirPath = '/home/coderaiser/hello.zip/hello/';
await write(dirPath);
// returns
'save: ok("hello")';

const path = '/home/coderaiser/hello.zip/hello.txt';
const writeStream = await write(path, Readable.from('hello'));

await pullout(writeStream);
// returns
'save: ok("hello")';

await pullout(await read(path));
// returns
'hello';

const zipStream = Readable
    .from('hello')
    .pipe(createGzip());

await write(path, zipStream, {
    unzip: true,
});
const readStream = await read(path);
await pullout(readStream);
// returns
'hello';
remove(path)
  • path - string
Example
import {remove} from 'redzip';
import pullout from 'pullout';

const dirPath = '/home/coderaiser/hello.zip/hello/';
await remove(dirPath);

const path = '/home/coderaiser/hello.zip/hello.txt';
await remove(path);
list(path)
  • path - string
Example
import {list} from 'redzip';

const dirPath = '/home/coderaiser/hello.zip/hello/';
await list(dirPath);
// returns
[
    '/Users/coderaiser/hello.zip/hello/world.txt',
];

FAT images

A path containing a .fat file works the same way a path containing a .zip file does, for every function above: read, readSize, readStat, write, remove, list. Detection is by the .fat extension in the path, the same way .zip is detected today.

Example
import {Readable} from 'node:stream';
import {
    read,
    write,
    remove,
} from 'redzip';
import pullout from 'pullout';

const path = '/home/coderaiser/disk.fat/hello.txt';

await write(path, Readable.from('hello'));
await pullout(await read(path));
// returns
'hello';

await remove(path);

A .fat image holds a real FAT12/FAT16/FAT32 filesystem (the kind used on floppy disks, SD cards, and USB sticks formatted "FAT"), read and written through fatfs-wasm. Unlike the zip backend, which streams entries one at a time, every write or remove against a .fat path reads the whole image into memory, changes it, and writes the whole image back — fine for a floppy-sized image, slow for a multi-gigabyte one.

tar, tar.gz and tar.bz2 archives

A path containing a .tar, .tar.gz or .tar.bz2 file works the same way a path containing a .zip file does, for every function above: read, readSize, readStat, write, remove, list. Detection is by the .tar, .tar.gz or .tar.bz2 extension in the path.

Example
import {Readable} from 'node:stream';
import {
    read,
    write,
    remove,
} from 'redzip';
import pullout from 'pullout';

const tarPath = '/home/coderaiser/hello.tar/hello.txt';
const tarGzPath = '/home/coderaiser/hello.tar.gz/hello.txt';
const tarBz2Path = '/home/coderaiser/hello.tar.bz2/hello.txt';

await write(tarPath, Readable.from('hello'));
await pullout(await read(tarPath));
// returns
'hello';

await write(tarGzPath, Readable.from('hello'));
await remove(tarGzPath);

await write(tarBz2Path, Readable.from('hello'));
await pullout(await read(tarBz2Path));
// returns
'hello';

The archive has to exist already. Tar has no index and no in-place changes, so reading one entry streams the archive from the start, and every write or remove rebuilds the whole archive. Read through tar-stream.

Reading .tar.bz2 streams through unbzip2-stream. No maintained package compresses bzip2 as a stream, so write and remove on a .tar.bz2 buffer the rebuilt, uncompressed tar in memory and compress it once with bzip2-wasm. The buffer is limited to 100MB by default: past it the call throws an error with code: 'EFBIG'. Pass {limit} in write options to change it.

.fat writes are limited the same way: the content passed to write is buffered in memory before it is written to the image, so it can not be larger than options.limit (100MB by default, error code: 'EFBIG').

rar archives

A path containing a .rar file works the same way a path containing a .zip file does, for every function above: read, readSize, readStat, write, remove, list. Unlike read-only RAR tools, write and remove are supported too.

Example
import {Readable} from 'node:stream';
import {
    read,
    write,
    remove,
} from 'redzip';
import pullout from 'pullout';

const path = '/home/coderaiser/hello.rar/hello.txt';

await write(path, Readable.from('hello'));
await pullout(await read(path));
// returns
'hello';

await remove(path);

The archive has to exist already. mode is synthesized (0o644 for files, 0o755 for directories), and every write or remove rebuilds the whole archive in memory. Read and written through @bitplane/rars.

  • readify - read directory content with file attributes: size, date, owner, mode
  • readbox - read file or directory from dropbox

License

MIT

Keywords