Skip to content

Commit

Permalink
list required field
Browse files Browse the repository at this point in the history
  • Loading branch information
mansurt committed Apr 24, 2017
1 parent a6c3061 commit 9a02e81
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (archive, dest, options) {
? true
: false;
if (isWrongPassword) {
return reject(new Error('Wrong password?'));
return reject(new Error('Wrong password'));
}

data.split('\n').forEach(function (line) {
Expand Down
2 changes: 1 addition & 1 deletion lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (archive, dest, options) {
? true
: false;
if (isWrongPassword) {
return reject(new Error('Wrong password?'));
return reject(new Error('Wrong password'));
}

data.split('\n').forEach(function (line) {
Expand Down
197 changes: 126 additions & 71 deletions lib/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var path = require('path');
var when = require('when');
var _ = require('underscore');

var u = {
run: require('../util/run'),
Expand All @@ -17,68 +16,7 @@ var u = {
* @resolve {Object} Tech spec about the archive.
* @reject {Error} The error as issued by 7-Zip.
*/
module.exports = function(archive, options) {
function parseProperty(data) {
const propertyDelim = ' = ';

let pos = data.indexOf(propertyDelim);
let property = {};

if (pos === -1) {
return null;
}

let key = data.substring(0, pos);
let value = data.substring(pos + propertyDelim.length, data.length);
property[key] = value;
return property;
};

function parseSltOutput(data) {
const metaBegining = '--';
const metaDataDelim = '----------';
const fileDelim = '';

let lines = data.split('\n');
let retval = {};

let i = 0;

for (; i < lines.length && (lines[i].indexOf(metaBegining) !== 0); i++);
i++; //skip the beginning

let metaData = {};
for (; i < lines.length && lines[i] !== metaDataDelim; i++) {
if (lines[i].length === 0) {
continue;
}

let property = parseProperty(lines[i]);
metaData = _.extend(metaData, property);
}

retval.metaData = metaData;
i++; //for metaDataDelim

//parse files
let files = [];
for (; i < lines.length; i++) {
if (!lines[i].length) {
continue;
}
let file = {};
for (; i < lines.length && lines[i] !== fileDelim; i++) {
let property = parseProperty(lines[i]);
file = _.extend(file, property);
}

files.push(file);
}

retval.files = files;
return retval;
};

module.exports = function(archive, options, filesRequiredFields) {
return when.promise(function(resolve, reject, progress) {

var spec = {};
Expand Down Expand Up @@ -107,25 +45,42 @@ module.exports = function(archive, options) {
}

if (options && options.slt) {
let retval = parseSltOutput(data);
return resolve(retval);
try {
let retval = parseSltOutput(data, filesRequiredFields);
return resolve(retval);
}
catch(err) {
return reject(retval);
};
}

data.split('\n').forEach(function(line) {
// Populate the tech specs of the archive that are passed to the
// resolve handler.
if (line.substr(0, 7) === 'Path = ') {
spec.path = line.substr(7, line.length);
if (isRequiredProperty(filesRequiredFields, 'Path')) {
spec.path = line.substr(7, line.length);
}
} else if (line.substr(0, 7) === 'Type = ') {
spec.type = line.substr(7, line.length);
if (isRequiredProperty(filesRequiredFields, 'Type')) {
spec.type = line.substr(7, line.length);
}
} else if (line.substr(0, 9) === 'Method = ') {
spec.method = line.substr(9, line.length);
if (isRequiredProperty(filesRequiredFields, 'Method')) {
spec.method = line.substr(9, line.length);
}
} else if (line.substr(0, 16) === 'Physical Size = ') {
spec.physicalSize = parseInt(line.substr(16, line.length), 10);
if (isRequiredProperty(filesRequiredFields, 'Physical Size')) {
spec.physicalSize = parseInt(line.substr(16, line.length), 10);
}
} else if (line.substr(0, 15) === 'Headers Size = ') {
spec.headersSize = parseInt(line.substr(15, line.length), 10);
if (isRequiredProperty(filesRequiredFields, 'Headers Size')) {
spec.headersSize = parseInt(line.substr(15, line.length), 10);
}
} else if (line.substr(0, 12) === 'Encrypted = ') {
spec.encrypted = line.substr(12, line.length);
if (isRequiredProperty(filesRequiredFields, 'Encrypted')) {
spec.encrypted = line.substr(12, line.length);
}
} else {
// Parse the stdout to find entries
var res = regex.exec(line);
Expand Down Expand Up @@ -165,4 +120,104 @@ module.exports = function(archive, options) {
});

});
};

function isRequiredProperty(requiredPropertyArr, name) {
return !requiredPropertyArr || (requiredPropertyArr.indexOf(name) >= 0);
}

const propertyDelim = ' = ';

function parseProperty(data, propObj, requiredPropertyArr) {
let pos = data.indexOf(propertyDelim);

if (pos === -1) {
return;
}

let key = data.substring(0, pos);
let value = data.substring(pos + propertyDelim.length, data.length);
if (isRequiredProperty(requiredPropertyArr, key)) {
propObj[key] = value;
}
};

const metaBegining = '--';
const metaDataDelim = '----------';
const fileDelim = '';

/*
* Output of 7z with slt option contains 7z version. '--', metaData, '----------', and fileData for each archived file separated by an empty line.
* Both metaData and fileData are set of <key> = <value> separated by a new line
* Example:
*
* 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
* p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,4 CPUs)
*
* Listing archive: example.7z
*
* --
* Path = example.7z
* Type = 7z
* Method = LZMA
* Solid = +
* Blocks = 1
* Physical Size = 24948
* Headers Size = 178
* ----------
* Path = test.7z
* Size = 304
* Packed Size = 24770
* Modified = 2017-03-20 13:10:16
* Attributes = ....A
* CRC = 55F6B511
* Encrypted = -
* Method = LZMA:16
* Block = 0
* Path = test.zip
* Size = 24305
* Packed Size =
* Modified = 2016-03-06 13:55:38
* Attributes = ....A
* CRC = F891B5C2
* Encrypted = -
* Method = LZMA:16
* Block = 0
*/
function parseSltOutput(data, filesRequiredFields) {
let lines = data.split('\n');
let retval = {metaData: {}, files: []};

let i = lines.indexOf(metaBegining);
if (i < 0) {
throw new Error('Invalid data ' + JSON.stringify(data));
}

for (i = i + 1; i < lines.length && lines[i] !== metaDataDelim; i++) {
if (lines[i].length === 0) {
continue;
}

parseProperty(lines[i], retval.metaData, null /* shouldn't filter metaData properties */);
}

i++; //for metaDataDelim

//parse files
for (; i < lines.length; i++) {
if (!lines[i].length) {
continue;
}
let file = {};
for (; i < lines.length && lines[i] !== fileDelim; i++) {
parseProperty(lines[i], file, filesRequiredFields);
}

retval.files.push(file);
}

return retval;
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"homepage": "https://github.com/quentinrossetti/node-7zip.git",
"dependencies": {
"cross-spawn": "^5.1.0",
"underscore": "^1.8.3",
"when": "^3.7.8"
},
"devDependencies": {
Expand Down

0 comments on commit 9a02e81

Please sign in to comment.