Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdemonte committed Dec 30, 2016
0 parents commit 8c1d7c9
Show file tree
Hide file tree
Showing 14 changed files with 551 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
bin/
node_modules/
.idea/
.DS_Store
3 changes: 3 additions & 0 deletions .jshintrc
@@ -0,0 +1,3 @@
{
"node" : true
}
7 changes: 7 additions & 0 deletions .npmignore
@@ -0,0 +1,7 @@
bin/
node_modules/
.idea/
.gitignore
.jshintrc
.npmignore
.DS_Store
130 changes: 130 additions & 0 deletions README.md
@@ -0,0 +1,130 @@
7za
===

A node wrapper for 7z including the latest version of `7za`.

Description
-----------

During the installation process, the latest version of [p7zip](https://github.com/jbdemonte/p7zip) is compiled to be used.
This module handle both callback and promise syntax.

Installation
------------

```
npm install --save 7za
```


Usage
-----
```
var Zip = require('7za');
Zip
.add('test.7z', '*.js')
.then(function (count) {
console.log('File added: ', count);
})
.list('test.7z')
.then(function (data) {
console.log('Path: ', data.path);
console.log('Type: ', data.type);
console.log('Method: ', data.method);
data.files.forEach(function (file, index) {
console.log('\nFile #' + (index + 1));
console.log(file.name);
console.log(file.date);
});
})
.catch(function (err) {
console.log(err);
});
```

Promise library use node Promise, but may be replaced:

```
var Zip = require('7za');
Zip.Promise = require('bluebird');
```


API
---

### Zip.add

**Arguments**
* `archive` The archive path.
* `files` The file list to add (string or array of string).

**Returns**
* `count` The file count added.


### Zip.delete

**Arguments**
* `archive` The archive path.
* `files` The file list to delete (string or array of string).

**Returns**
* none


### Zip.extract

**Arguments**
* `archive` The archive path.
* `destination` The extraction path (optional).
* `full` Extract with full paths (optional, default=false).

**Returns**
* `files` Array of all the extracted files.


### Zip.list

**Arguments**
* `archive` The archive path.

**Returns**
* `data` object

* `data.path` string
* `data.type` string
* `data.physicalSize` number
* `data.headersSize` number
* `data.method` string
* `data.solid` string
* `data.blocks` number
* `data.files` array

* `data.files[].attr` string
* `data.files[].compressed` number
* `data.files[].date` date
* `data.files[].name` number
* `data.files[].size` number


### Zip.rename

**Arguments**
* `archive` The archive path.
* `files` Hashmap of the file list to rename ({oldName: newName, ...}.

**Returns**
* none

### Zip.update

**Arguments**
* `archive` Path to the archive.
* `files` The file list to update (string or array of string).

**Returns**
* `count` The file count updated.
41 changes: 41 additions & 0 deletions index.js
@@ -0,0 +1,41 @@
var tools = require('./lib/tools');

var exports = module.exports = {};

function load(name) {
var module = require('./lib/' + name);
return function () {
var callback, promise;
var args = Array.prototype.slice.call(arguments);
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
}
promise = module.apply(this, args);
if (callback) {
promise
.then(function (result) {
console.log('AA', result);
callback(null, result);
})
.catch(function (err) {
console.log('catch ', err);
callback(err);
});
} else {
return promise;
}
};
}

Object.defineProperty(exports, 'Promise', {
get: function () {
return tools.Promise;
},
set: function (cls) {
tools.Promise = cls;
}
});

['add', 'delete', 'extract', 'list', 'rename', 'update'].forEach(function (name) {
exports[name] = load(name);
});
25 changes: 25 additions & 0 deletions lib/add.js
@@ -0,0 +1,25 @@
var tools = require('./tools');

/**
* Adds files to archive
* @param {string} archive
* @param {string|string[]} files
* @return {Promise}
*/
module.exports = function (archive, files) {
return tools
.run(['a', archive].concat(files))
.then(function (lines) {

var needle = 'Items to compress: ';
var count = 0;

lines.some(function (line) {
if (tools.start(line, needle)) {
count = parseInt(line.substr(needle.length).trim(), 10);
}
});

return count;
});
};
13 changes: 13 additions & 0 deletions lib/delete.js
@@ -0,0 +1,13 @@
var tools = require('./tools');

/**
* Deletes files from archive
* @param {string} archive
* @param {string|string[]} files
* @return {Promise}
*/
module.exports = function (archive, files) {
return tools
.run(['d', archive].concat(files))
.then(function () {});
};
34 changes: 34 additions & 0 deletions lib/extract.js
@@ -0,0 +1,34 @@
var tools = require('./tools');

/**
* Extracts files from an archive to a directory
* @param {string} archive
* @param {string} [destination]
* @param {boolean} [full=false] Extract with full paths
* @return {Promise}
*/
module.exports = function (archive, destination, full) {
if (typeof destination === 'boolean') {
full = destination;
destination = undefined;
}
var args = [full ? 'x' : 'e', archive];
if (destination) {
args.push('-o' + destination);
}
return tools
.run(args)
.then(function (lines) {

var files = [];
var needle = 'Extracting archive: ';

lines.forEach(function (line) {
if (tools.start(line, needle)) {
files.push(line.substr(needle.length).trim());
}
});

return files;
});
};
74 changes: 74 additions & 0 deletions lib/list.js
@@ -0,0 +1,74 @@
var tools = require('./tools');

var properties = ['Path', 'Type', 'Physical Size', 'Headers Size', 'Method', 'Solid', 'Blocks'];
var regex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ([\.D][\.R][\.H][\.S][\.A]) +(\d+) +(\d+)? +(.+)/;

function camelCase(str) {
return str
.toLowerCase()
.split(' ')
.map(function (part, index) {
return index ? part[0].toUpperCase() + part.substr(1) : part;
})
.join('');
}

function ArchiveInfo() {
this.path = undefined;
this.type = undefined;
this.physicalSize = undefined;
this.headersSize = undefined;
this.method = undefined;
this.solid = undefined;
this.blocks = undefined;
this.files = [];
}

function ArchiveFile() {
this.attr = undefined;
this.compressed = undefined;
this.date = undefined;
this.name = undefined;
this.size = undefined;
}

/**
* List contents of archive
* @param {string} archive
* @return {Promise}
*/
module.exports = function (archive) {
return tools
.run('l', archive)
.then(function (lines) {

var info = new ArchiveInfo();

lines.forEach(function (line) {
var res = regex.exec(line);
if (res) {
var file = new ArchiveFile();
file.date = new Date(res[1]);
file.attr = res[2];
file.size = parseInt(res[3], 10) || 0;
file.compressed = parseInt(res[4]) || 0;
file.name = res[5];
info.files.push(file);
} else {
properties.some(function (property) {
if (tools.start(line, property + ' = ')) {
info[camelCase(property)] = line.substr(property.length + 3);
}
});
}
});

['physicalSize', 'headersSize', 'blocks'].forEach(function (property) {
if (info[property]) {
info[property] = parseInt(info[property], 10);
}
});

return info;
});
};
18 changes: 18 additions & 0 deletions lib/rename.js
@@ -0,0 +1,18 @@
var tools = require('./tools');

/**
* Renames files in archive
* @param {string} archive
* @param {object} files - hashmap {oldName => newName}
* @return {Promise}
*/
module.exports = function (archive, files) {
var args = ['rn', archive];
Object.keys(files).forEach(function (source) {
args.push(source);
args.push(files[source]);
});
return tools
.run(args)
.then(function () {});
};

0 comments on commit 8c1d7c9

Please sign in to comment.