Installations
npm install cmss-compressing
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 4.0.0
Node Version
11.10.1
NPM Version
6.14.4
Score
70.1
Supply Chain
97.6
Quality
72.6
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
junhun-code
Download Statistics
Total Downloads
836
Last Day
1
Last Week
4
Last Month
12
Last Year
109
GitHub Statistics
1 Stars
3 Commits
2 Watching
1 Branches
1 Contributors
Bundle Size
357.37 kB
Minified
182.78 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.1
Package Id
cmss-compressing@1.0.1
Unpacked Size
50.24 kB
Size
11.50 kB
File Count
24
NPM Version
6.14.4
Node Version
11.10.1
Total Downloads
Cumulative downloads
Total Downloads
836
Last day
0%
1
Compared to previous day
Last week
-20%
4
Compared to previous week
Last month
300%
12
Compared to previous month
Last year
-51.6%
109
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
cmss-compressing
The missing compressing and uncompressing lib for node.
Currently supported:
- tar
- gzip
- tgz
- zip
Install
1npm install cmss-compressing
Usage
Compress a single file
Use gzip as an example, tar, tgz and zip is same as gzip.
promise style
1const compressing = require('cmss-compressing');
2
3// compress a file
4compressing.gzip.compressFile('file/path/to/compress', 'path/to/destination.gz')
5.then(compressDone)
6.catch(handleError);
7
8// compress a file buffer
9compressing.gzip.compressFile(buffer, 'path/to/destination.gz')
10.then(compressDone)
11.catch(handleError);
12
13// compress a stream
14compressing.gzip.compressFile(stream, 'path/to/destination.gz')
15.then(compressDone)
16.catch(handleError);
stream style
1const compressing = require('cmss-compressing'); 2 3new compressing.gzip.FileStream({ source: 'file/path/to/compress' }) 4 .on('error', handleError) 5 .pipe(fs.createWriteStream('path/to/destination.gz')) 6 .on('error', handleError); 7 8// It's a transform stream, so you can pipe to it 9fs.createReadStream('file/path/to/compress') 10 .on('error', handleError) 11 .pipe(new compressing.gzip.FileStream()) 12 .on('error', handleError) 13 .pipe(fs.createWriteStream('path/to/destination.gz')) 14 .on('error', handleError); 15 16// You should take care of stream errors in caution, use pump to handle error in one place 17const pump = require('pump';) 18const sourceStream = fs.createReadStream('file/path/to/compress') 19const gzipStream = new compressing.gzip.FileStream(); 20const destStream = fs.createWriteStream('path/to/destination.gz'); 21pump(sourceStream, gzipStream, destStream, handleError);
Compress a dir
Use tar as an example, tgz and zip is same as gzip.
Gzip only support compressing a single file. if you want to compress a dir with gzip, then you may need tgz instead.
promise style
1const compressing = require('cmss-compressing'); 2compressing.tar.compressDir('dir/path/to/compress', 'path/to/destination.tar') 3.then(compressDone) 4.catch(handleError);
stream style
1const compressing = require('cmss-compressing');
2
3const tarStream = new compressing.tar.Stream();
4tarStream.addEntry('dir/path/to/compress');
5
6tarStream
7 .on('error', handleError)
8 .pipe(fs.createWriteStream('path/to/destination.tar'))
9 .on('error', handleError);
10
11// You should take care of stream errors in caution, use pump to handle error in one place
12const tarStream = new compressing.tar.Stream();
13tarStream.addEntry('dir/path/to/compress');
14const destStream = fs.createWriteStream('path/to/destination.tar');
15pump(tarStream, destStream, handleError);
Stream is very powerful, you can compress multiple entries in it;
1const tarStream = new compressing.tar.Stream();
2// dir
3tarStream.addEntry('dir/path/to/compress');
4
5// file
6tarStream.addEntry('file/path/to/compress');
7
8// buffer
9tarStream.addEntry(buffer);
10
11// stream
12tarStream.addEntry(stream);
13
14const destStream = fs.createWriteStream('path/to/destination.tar');
15pipe(tarStream, destStream, handleError);
Uncompress a file
promise style
1const compressing = require('cmss-compressing'); 2 3// uncompress a file 4compressing.tgz.uncompress('file/path/to/uncompress.tgz', 'path/to/destination/dir') 5.then(uncompressDone) 6.catch(handleError); 7 8// uncompress a file buffer 9compressing.tgz.uncompress(buffer, 'path/to/destination/dir') 10.then(uncompressDone) 11.catch(handleError); 12 13// uncompress a stream 14compressing.tgz.uncompress(stream, 'path/to/destination/dir') 15.then(uncompressDone) 16.catch(handleError);
Note: tar, tgz and zip have the same uncompressing API as above: destination should be a path of a directory, while that of gzip is slightly different: destination must be a file or filestream.
And working with urllib is super easy. Let's download a tgz file and uncompress to a directory:
1const urllib = require('urllib'); 2const targetDir = require('os').tmpdir(); 3const compressing = require('cmss-compressing'); 4 5urllib.request('http://registry.npmjs.org/pedding/-/pedding-1.1.0.tgz', { 6 streaming: true, 7 followRedirect: true, 8}) 9.then(result => compressing.tgz.uncompress(result.res, targetDir)) 10.then(() => console.log('uncompress done')) 11.catch(console.error);
stream style
1const compressing = require('cmss-compressing'); 2const mkdirp = require('mkdirp'); 3 4function onEntry(header, stream, next) => { 5 stream.on('end', next); 6 7 // header.type => file | directory 8 // header.name => path name 9 10 if (header.type === 'file') { 11 stream.pipe(fs.createWriteStream(path.join(destDir, header.name))); 12 } else { // directory 13 mkdirp(path.join(destDir, header.name), err => { 14 if (err) return handleError(err); 15 stream.resume(); 16 }); 17 } 18} 19 20new compressing.tgz.UncompressStream({ source: 'file/path/to/uncompress.tgz' }) 21 .on('error', handleError) 22 .on('finish', handleFinish) // uncompressing is done 23 .on('entry', onEntry); 24 25// It's a writable stream, so you can pipe to it 26fs.createReadStream('file/path/to/uncompress') 27 .on('error', handleError) 28 .pipe(new compressing.tgz.UncompressStream()) 29 .on('error', handleError) 30 .on('finish', handleFinish) // uncompressing is done 31 .on('entry', onEntry);
Note: tar, tgz and zip have the same uncompressing streaming API as above: it's a writable stream, and entries will be emitted while uncompressing one after one another, while that of gzip is slightly different: gzip.UncompressStream is a transform stream, so no entry
event will be emitted and you can just pipe to another stream
This constrants is brought by Gzip algorithm itself, it only support compressing one file and uncompress one file.
1new compressing.gzip.UncompressStream({ source: 'file/path/to/uncompress.gz' })
2 .on('error', handleError)
3 .pipe(fs.createWriteStream('path/to/dest/file'))
4 .on('error', handleError);
API
compressFile
Use this API to compress a single file. This is a convenient method, which wraps FileStream API below, but you can handle error in one place.
- gzip.compressFile(source, dest, opts)
- tar.compressFile(source, dest, opts)
- tgz.compressFile(source, dest, opts)
- zip.compressFile(source, dest, opts)
Params
- source {String|Buffer|Stream} - source to be compressed, could be a file path, buffer, or a readable stream
- dest {String|Stream} - compressing destination, could be a file path(eg.
/path/to/xx.tgz
), or a writable stream. - opts {Object} - usually you don't need it
Returns a promise object.
compressDir
Use this API to compress a dir. This is a convenient method, which wraps Stream API below, but you can handle error in one place.
Note: gzip do not have a compressDir method, you may need tgz instead.
- tar.compressDir(source, dest, opts)
- tgz.compressDir(source, dest, opts)
- zip.compressDir(source, dest, opts)
Params
- source {String|Buffer|Stream} - source to be compressed
- dest {String|Stream} - compressing destination, could be a file path(eg.
/path/to/xx.tgz
), or a writable stream. - opts {Object} - usually you don't need it
uncompress
Use this API to uncompress a file. This is a convenient method, which wraps UncompressStream API below, but you can handle error in one place. RECOMMANDED.
- tar.uncompress(source, dest, opts)
- tgz.uncompress(source, dest, opts)
- zip.uncompress(source, dest, opts)
- gzip.uncompress(source, dest, opts)
Params
- source {String|Buffer|Stream} - source to be uncompressed
- dest {String|Stream} - uncompressing destination. When uncompressing tar, tgz and zip, it should be a directory path (eg.
/path/to/xx
). When uncompressing gzip, it should be a file path or a writable stream. - opts {Object} - usually you don't need it
-
opts.zipFileNameEncoding {String} - Only work on zip format, default is 'utf8'. Major non-UTF8 encodings by languages:
- Korean: cp949, euc-kr
- Japanese: sjis (shift_jis), cp932, euc-jp
- Chinese: gbk, gb18030, gb2312, cp936, hkscs, big5, cp950
-
FileStream
The transform stream to compress a single file.
Note: If you are not very familiar with streams, just use compressFile() API, error can be handled in one place.
- new gzip.FileStream(opts)
- new tar.FileStream(opts)
- new tgz.FileStream(opts)
- new zip.FileStream(opts)
Common params:
- opts.source {String|Buffer|Stream} - source to be compressed, could be a file path, buffer, or a readable stream.
Gzip params:
- opts.zlib - {Object} gzip.FileStream uses zlib to compress, pass this param to control the behavior of zlib.
Tar params:
- opts.relativePath {String} - Adds a file from source into the compressed result file as opts.relativePath. Uncompression programs would extract the file from the compressed file as relativePath. If opts.source is a file path, opts.relativePath is optional, otherwise it's required.
- opts.size {Number} - Tar compression requires the size of file in advance. When opts.source is a stream, the size of it cannot be calculated unless load all content of the stream into memory(the default behavior, but loading all data into memory could be a very bad idea). Pass opts.size to avoid loading all data into memory, or a warning will be shown.
- opts.suppressSizeWarning {Boolean} - Pass true to suppress the size warning mentioned.
Tgz params:
tgz.FileStream is a combination of tar.FileStream and gzip.FileStream, so the params are the combination of params of tar and gzip.
Zip params:
- opts.relativePath {String} - Adds a file from source into the compressed result file as opts.relativePath. Uncompression programs would extract the file from the compressed file as relativePath. If opts.source is a file path, opts.relativePath is optional, otherwise it's required.
- opts.yazl {Object} - zip.FileStream compression uses yazl, pass this param to control the behavior of yazl.
Stream
The readable stream to compress anything as you need.
Note: If you are not very familiar with streams, just use compressFile() and compressDir() API, error can be handled in one place.
Gzip only support compressing a single file. So gzip.Stream is not available.
Constructor
- new tar.Stream()
- new tgz.Stream()
- new zip.Stream()
No options in all constructors.
Instance methods
- addEntry(entry, opts)
Params
- entry {String|Buffer|Stream} - entry to compress, cound be a file path, a dir path, a buffer, or a stream.
- opts.relativePath {String} - uncompression programs would extract the file from the compressed file as opts.relativePath. If entry is a file path or a dir path, opts.relativePath is optional, otherwise it's required.
- opts.ignoreBase {Boolean} - when entry is a dir path, and opts.ignoreBase is set to true, the compression will contain files relative to the path passed, and not with the path included.
UncompressStream
The writable stream to uncompress anything as you need.
Note: If you are not very familiar with streams, just use uncompress()
API, error can be handled in one place.
Gzip only support compressing and uncompressing one single file. So gzip.UncompressStream is a transform stream which is different from others.
Constructor
- new gzip.UncompressStream(opts)
- new tar.UncompressStream(opts)
- new tgz.UncompressStream(opts)
- new zip.UncompressStream(opts)
Common params:
- opts.source {String|Buffer|Stream} - source to be uncompressed, could be a file path, buffer, or a readable stream.
CAUTION for zip.UncompressStream
Due to the design of the .zip file format, it's impossible to interpret a .zip file without loading all data into memory.
Although the API is streaming style(try to keep it handy), it still loads all data into memory.
https://github.com/thejoshwolfe/yauzl#no-streaming-unzip-api
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More