diff --git a/adm-zip.js b/adm-zip.js index 92845be..e4cb0e5 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -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 { @@ -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)); diff --git a/test/crc/bad_crc.zip b/test/crc/bad_crc.zip new file mode 100644 index 0000000..26b4c09 Binary files /dev/null and b/test/crc/bad_crc.zip differ diff --git a/test/crc/good_crc.zip b/test/crc/good_crc.zip new file mode 100644 index 0000000..d816bb4 Binary files /dev/null and b/test/crc/good_crc.zip differ diff --git a/test/crc/index.js b/test/crc/index.js new file mode 100644 index 0000000..850a133 --- /dev/null +++ b/test/crc/index.js @@ -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'); + }); + } +})(); diff --git a/zipEntry.js b/zipEntry.js index 4201e02..e9efc7e 100644 --- a/zipEntry.js +++ b/zipEntry.js @@ -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 {