Skip to content

Commit 83d44ba

Browse files
authored
Merge pull request #232 from kevincharm/feature/handle-callback-err
Handle callback error in readAsTextAsync & check CRC
2 parents 74bdec6 + f5310b5 commit 83d44ba

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

adm-zip.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ module.exports = function (/*String*/input) {
113113
readAsTextAsync: function (/*Object*/entry, /*Function*/callback, /*String - Optional*/encoding) {
114114
var item = getEntry(entry);
115115
if (item) {
116-
item.getDataAsync(function (data) {
116+
item.getDataAsync(function (data, err) {
117+
if (err) {
118+
callback(data, err);
119+
return;
120+
}
121+
117122
if (data && data.length) {
118123
callback(data.toString(encoding || "utf8"));
119124
} else {
@@ -470,8 +475,12 @@ module.exports = function (/*String*/input) {
470475
callback(undefined);
471476
return;
472477
}
473-
entry.getDataAsync(function (content) {
478+
entry.getDataAsync(function (content, err) {
474479
if (i <= 0) return;
480+
if (err) {
481+
callback(new Error(err));
482+
return;
483+
}
475484
if (!content) {
476485
i = 0;
477486
callback(new Error(Utils.Errors.CANT_EXTRACT_FILE));

test/crc/bad_crc.zip

1.47 KB
Binary file not shown.

test/crc/good_crc.zip

1.47 KB
Binary file not shown.

test/crc/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
;(function () {
2+
var assert = require('assert');
3+
var path = require('path');
4+
var Zip = require('../../adm-zip');
5+
6+
testGoodCrc();
7+
testBadCrc();
8+
9+
// Good CRC
10+
function testGoodCrc() {
11+
var goodZip = new Zip(path.join(__dirname, 'good_crc.zip'));
12+
var entries = goodZip.getEntries();
13+
assert(entries.length === 1, 'Good CRC: Test archive contains exactly 1 file');
14+
15+
var testFile = entries.filter(function (entry) {
16+
return entry.entryName === 'lorem_ipsum.txt';
17+
});
18+
assert(testFile.length === 1, 'Good CRC: lorem_ipsum.txt file exists as archive entry');
19+
20+
var testFileEntryName = testFile[0].entryName;
21+
goodZip.readAsTextAsync(testFileEntryName, function (data, err) {
22+
assert(!err, 'Good CRC: error object not present');
23+
assert(data && data.length, 'Good CRC: buffer not empty');
24+
});
25+
}
26+
27+
// Bad CRC
28+
function testBadCrc() {
29+
var badZip = new Zip(path.join(__dirname, 'bad_crc.zip'));
30+
var entries = badZip.getEntries();
31+
assert(entries.length === 1, 'Bad CRC: Test archive contains exactly 1 file');
32+
33+
var testFile = entries.filter(function (entry) {
34+
return entry.entryName === 'lorem_ipsum.txt';
35+
});
36+
assert(testFile.length === 1, 'Bad CRC: lorem_ipsum.txt file exists as archive entry');
37+
38+
var testFileEntryName = testFile[0].entryName;
39+
badZip.readAsTextAsync(testFileEntryName, function (data, err) {
40+
assert(data && data.length, 'Bad CRC: buffer not empty');
41+
assert(err, 'Bad CRC: error object present');
42+
});
43+
}
44+
})();

zipEntry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (/*Buffer*/input) {
2323
function crc32OK(data) {
2424
// 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
2525
if ((_entryHeader.flags & 0x8) !== 0x8) {
26-
if (Utils.crc32(data) !== _entryHeader.crc) {
26+
if (Utils.crc32(data) !== _entryHeader.dataHeader.crc) {
2727
return false;
2828
}
2929
} else {

0 commit comments

Comments
 (0)