A Node.js wrapper for 7-Zip
At this point this is practically a rewrite. Compared to the original node-7z by Quentin Rossetti this:
- uses bluebird promises instead of "when"
- uses a progress callback instead of Promise.progress events (since those are deprecated)
- uses a secondary project to download the 7z binary (windows only atm, contributions welcome)
- fixes problems with parsing "list" output
- fixes problems parsing command line parameters
- has less redundant code
I chose to use Promises in this library. API is consistent with standard use:
var Zip = require('node-7z'); // Name the class as you want!
var myTask = new Zip();
myTask.extractFull('myArchive.7z', 'destination', { p: 'myPassword' })
// Equivalent to `on('data', function (files) { // ... });`
.progress(function (files) {
console.log('Some files are extracted: %s', files);
});
// When all is done
.then(function () {
console.log('Extracting done!');
});
// On error
.catch(function (err) {
console.error(err);
});npm install --save node-7z
See the 7-Zip documentation for the full list of usages and options (switches).
The type of the list of files can be either String or Array.
Arguments
archivePath to the archive you want to create.filesThe file list to add.optionsAn object of options (7-Zip switches).
Progress
filesA array of all the added files. The/character is used as a path separator on every platform.
Error
errAn Error object.
Arguments
archivePath to the archive you want to delete files from.filesThe file list to delete.optionsAn object of options (7-Zip switches).
Error
errAn Error object.
Arguments
archiveThe path to the archive you want to extract.destWhere to extract the archive.optionsAn object of options.
Progress
filesA array of all the extracted files AND directories. The/character is used as a path separator on every platform.
Error
errAn Error object.
Arguments
archiveThe path to the archive you want to extract.destWhere to extract the archive (creates folders for you).optionsAn object of options.
Progress
filesA array of all the extracted files AND directories. The/character is used as a path separator on every platform.
Error
errAn Error object.
Arguments
archiveThe path to the archive you want to analyse.optionsAn object of options.
Progress
filesA array of objects of all the extracted files AND directories. The/character is used as a path separator on every platform. Object's properties are:date,attr,sizeandname.
Fulfill
specAn object of tech spec about the archive. Properties are:path,type,method,physicalSizeandheadersSize(Some of them may be missing with non-7z archives).
Error
errAn Error object.
Arguments
archiveThe path to the archive you want to analyse.optionsAn object of options.
Progress
filesA array of all the tested files. The/character is used as a path separator on every platform.
Error
errAn Error object.
Arguments
archivePath to the archive you want to update.filesThe file list to update.optionsAn object of options (7-Zip switches).
Progress
filesA array of all the updated files. The/character is used as a path separator on every platform.
Error
errAn Error object.
With the 7za binary compression is made like that:
# adds *.exe and *.dll files to solid archive archive.7z using LZMA method
# with 2 MB dictionary and BCJ filter.
7z a archive.7z *.exe -m0=BCJ -m1=LZMA:d=21With node-7z you can translate it like that:
var archive = new Zip();
archive.add('archive.7z', '*.exe', {
m0: '=BCJ',
m1: '=LZMA:d=21'
})
.then(function () {
// Do stuff...
});When adding, deleting or updating archives you can pass either a string or an
array as second parameter (the files parameter).
var archive = new Zip();
archive.delete('bigArchive.7z', [ 'file1', 'file2' ])
.then(function () {
// Do stuff...
});You can extract with wildcards to specify one or more file extensions. To do
this add a wildcards attribute to the options object. The wildcard
attribute takes an Array as value. In this array each item is a wildcard.
var archive = new Zip();
archive.extractFull('archive.zip', 'destination/', {
wildcards: [ '*.txt', '*.md' ], // extract all text and Markdown files
r: true // in each subfolder too
})
.progress(function (files) {
// Do stuff with files...
})
.then(function () {
// Do stuff...
});Note that the r (for recursive) attribute is passed in this example.
Thanks to sketchpunk #9 for this one
Sometimes you just want to use the lib as the original command line. For
instance you want to apply to switches with different values (e.g.:
-i!*.jpg -i!*.png to target only two types of extensions).
In such cases the default behavior of the options argument is not enough. You
can use the custom raw key in your options object and pass it an Array of
values.
var archive = new Zip();
archive.list('archive.zip', {
raw: [ '-i!*.jpg', '-i!*.png' ], // only images
})
.progress(function (files) {
// Do stuff with files...
})
.then(function () {
// Do stuff...
});With ❤️ from quentinrossetti