Skip to content

Commit

Permalink
Merge pull request #232 from kevincharm/feature/handle-callback-err
Browse files Browse the repository at this point in the history
Handle callback error in readAsTextAsync & check CRC
  • Loading branch information
cthackers authored Oct 11, 2018
2 parents 74bdec6 + f5310b5 commit 83d44ba
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
13 changes: 11 additions & 2 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ module.exports = function (/*String*/input) {
readAsTextAsync: function (/*Object*/entry, /*Function*/callback, /*String - Optional*/encoding) {
var item = getEntry(entry);
if (item) {
item.getDataAsync(function (data) {
item.getDataAsync(function (data, err) {
if (err) {
callback(data, err);
return;
}

if (data && data.length) {
callback(data.toString(encoding || "utf8"));
} else {
Expand Down Expand Up @@ -470,8 +475,12 @@ module.exports = function (/*String*/input) {
callback(undefined);
return;
}
entry.getDataAsync(function (content) {
entry.getDataAsync(function (content, err) {
if (i <= 0) return;
if (err) {
callback(new Error(err));
return;
}
if (!content) {
i = 0;
callback(new Error(Utils.Errors.CANT_EXTRACT_FILE));
Expand Down
Binary file added test/crc/bad_crc.zip
Binary file not shown.
Binary file added test/crc/good_crc.zip
Binary file not shown.
44 changes: 44 additions & 0 deletions test/crc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
;(function () {
var assert = require('assert');
var path = require('path');
var Zip = require('../../adm-zip');

testGoodCrc();
testBadCrc();

// Good CRC
function testGoodCrc() {
var goodZip = new Zip(path.join(__dirname, 'good_crc.zip'));
var entries = goodZip.getEntries();
assert(entries.length === 1, 'Good CRC: Test archive contains exactly 1 file');

var testFile = entries.filter(function (entry) {
return entry.entryName === 'lorem_ipsum.txt';
});
assert(testFile.length === 1, 'Good CRC: lorem_ipsum.txt file exists as archive entry');

var testFileEntryName = testFile[0].entryName;
goodZip.readAsTextAsync(testFileEntryName, function (data, err) {
assert(!err, 'Good CRC: error object not present');
assert(data && data.length, 'Good CRC: buffer not empty');
});
}

// Bad CRC
function testBadCrc() {
var badZip = new Zip(path.join(__dirname, 'bad_crc.zip'));
var entries = badZip.getEntries();
assert(entries.length === 1, 'Bad CRC: Test archive contains exactly 1 file');

var testFile = entries.filter(function (entry) {
return entry.entryName === 'lorem_ipsum.txt';
});
assert(testFile.length === 1, 'Bad CRC: lorem_ipsum.txt file exists as archive entry');

var testFileEntryName = testFile[0].entryName;
badZip.readAsTextAsync(testFileEntryName, function (data, err) {
assert(data && data.length, 'Bad CRC: buffer not empty');
assert(err, 'Bad CRC: error object present');
});
}
})();
2 changes: 1 addition & 1 deletion zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function (/*Buffer*/input) {
function crc32OK(data) {
// if bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the header is written
if ((_entryHeader.flags & 0x8) !== 0x8) {
if (Utils.crc32(data) !== _entryHeader.crc) {
if (Utils.crc32(data) !== _entryHeader.dataHeader.crc) {
return false;
}
} else {
Expand Down

0 comments on commit 83d44ba

Please sign in to comment.