Skip to content

Commit bf146ca

Browse files
committed
Add special error message when loading zip is all zeros or empty
1 parent cc78df6 commit bf146ca

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

dist/jszip.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
3-
JSZip v3.11.1 - A JavaScript class for generating and reading zip files
3+
JSZip v3.12.0 - A JavaScript class for generating and reading zip files
44
<http://stuartk.com/jszip>
55
66
(c) 2009-2016 Stuart Knightley <stuart [at] stuartk.com>
@@ -1906,6 +1906,17 @@ DataReader.prototype = {
19061906
(dostime >> 11) & 0x1f, // hour
19071907
(dostime >> 5) & 0x3f, // minute
19081908
(dostime & 0x1f) << 1)); // second
1909+
},
1910+
/**
1911+
* @returns {boolean} true if the reader is all zeros or has 0 length.
1912+
*/
1913+
isAllZeros: function() {
1914+
for (var i = 0; i < this.length; i++) {
1915+
if (this.byteAt(i) !== 0) {
1916+
return false;
1917+
}
1918+
}
1919+
return true;
19091920
}
19101921
};
19111922
module.exports = DataReader;
@@ -3870,6 +3881,10 @@ ZipEntries.prototype = {
38703881
this.readCentralDir();
38713882
this.readLocalFiles();
38723883
} catch (centralDirectoryError) {
3884+
if (this.reader.isAllZeros()) {
3885+
throw new Error("Corrupted zip: all zeros, unrecoverable");
3886+
}
3887+
38733888
if (this.loadOptions.recoverCorrupted) {
38743889
if (this.loadOptions.onCorruptCentralDirectory) {
38753890
this.loadOptions.onCorruptCentralDirectory(centralDirectoryError);

dist/jszip.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/reader/DataReader.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ DataReader.prototype = {
111111
(dostime >> 11) & 0x1f, // hour
112112
(dostime >> 5) & 0x3f, // minute
113113
(dostime & 0x1f) << 1)); // second
114+
},
115+
/**
116+
* @returns {boolean} true if the reader is all zeros or has 0 length.
117+
*/
118+
isAllZeros: function() {
119+
for (var i = 0; i < this.length; i++) {
120+
if (this.byteAt(i) !== 0) {
121+
return false;
122+
}
123+
}
124+
return true;
114125
}
115126
};
116127
module.exports = DataReader;

lib/zipEntries.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ ZipEntries.prototype = {
320320
this.readCentralDir();
321321
this.readLocalFiles();
322322
} catch (centralDirectoryError) {
323+
if (this.reader.isAllZeros()) {
324+
throw new Error("Corrupted zip: all zeros, unrecoverable");
325+
}
326+
323327
if (this.loadOptions.recoverCorrupted) {
324328
if (this.loadOptions.onCorruptCentralDirectory) {
325329
this.loadOptions.onCorruptCentralDirectory(centralDirectoryError);

test/asserts/corruption.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,30 @@ QUnit.module("corruption", function () {
4141
})
4242
.catch(JSZipTestUtils.assertNoError);
4343
});
44+
45+
JSZipTestUtils.testZipFile("special error message for all zeros", "ref/zeros.bin", function(assert, file) {
46+
var done = assert.async();
47+
JSZip.loadAsync(file)
48+
.then(function () {
49+
assert.ok(false, "somehow successfully extracted zip of all zeros");
50+
done();
51+
})
52+
.catch(function (err) {
53+
assert.ok(err.message.match("all zeros"), "all zeros : the error message is useful");
54+
done();
55+
});
56+
});
57+
58+
JSZipTestUtils.testZipFile("special error message for empty", "ref/empty.bin", function(assert, file) {
59+
var done = assert.async();
60+
JSZip.loadAsync(file)
61+
.then(function () {
62+
assert.ok(false, "somehow successfully extracted empty file");
63+
done();
64+
})
65+
.catch(function (err) {
66+
assert.ok(err.message.match("all zeros"), "empty : the error message is useful");
67+
done();
68+
});
69+
});
4470
});

test/ref/empty.bin

Whitespace-only changes.

test/ref/zeros.bin

1 KB
Binary file not shown.

0 commit comments

Comments
 (0)