Skip to content

Commit ed5fd83

Browse files
committed
fix: CRC is unexpectedly changed after zip is re-created
1 parent cb300c9 commit ed5fd83

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

Diff for: .gitignore

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
node_modules/
2-
test/issue_*/unzipped/
1+
node_modules
2+
/test/issue_*/unzipped/
3+
xxx
34
.idea
4-
*.iml
5+
*.iml

Diff for: headers/entryHeader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ module.exports = function () {
213213
// modification time (2 bytes time, 2 bytes date)
214214
data.writeUInt32LE(_time, Constants.CENTIM);
215215
// uncompressed file crc-32 value
216-
data.writeInt32LE(_crc & 0xFFFF, Constants.CENCRC, true);
216+
data.writeUInt32LE(_crc, Constants.CENCRC);
217217
// compressed size
218218
data.writeUInt32LE(_compressedSize, Constants.CENSIZ);
219219
// uncompressed size

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.4.12",
44
"description": "Javascript implementation of zip for nodejs with support for electron original-fs. Allows user to create or extract zip files both in memory or to/from disk",
55
"scripts": {
6-
"test": "mocha test/mocha.js"
6+
"test": "mocha test/mocha.js test/crc/index.js"
77
},
88
"keywords": [
99
"zip",

Diff for: test/crc/index.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
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();
1+
const assert = require('assert');
2+
const path = require('path');
3+
const Zip = require('../../adm-zip');
4+
const rimraf = require('rimraf')
5+
6+
describe('crc', () => {
7+
const destination = __dirname + '/xxx'
8+
9+
beforeEach(done => rimraf(destination, done))
10+
11+
it('Good CRC', (done) => {
12+
const goodZip = new Zip(path.join(__dirname, 'good_crc.zip'));
13+
const entries = goodZip.getEntries();
1314
assert(entries.length === 1, 'Good CRC: Test archive contains exactly 1 file');
1415

15-
var testFile = entries.filter(function (entry) {
16+
const testFile = entries.filter(function (entry) {
1617
return entry.entryName === 'lorem_ipsum.txt';
1718
});
1819
assert(testFile.length === 1, 'Good CRC: lorem_ipsum.txt file exists as archive entry');
1920

20-
var testFileEntryName = testFile[0].entryName;
21+
const testFileEntryName = testFile[0].entryName;
2122
goodZip.readAsTextAsync(testFileEntryName, function (data, err) {
2223
assert(!err, 'Good CRC: error object not present');
2324
assert(data && data.length, 'Good CRC: buffer not empty');
25+
done();
2426
});
25-
}
27+
});
2628

27-
// Bad CRC
28-
function testBadCrc() {
29-
var badZip = new Zip(path.join(__dirname, 'bad_crc.zip'));
30-
var entries = badZip.getEntries();
29+
it('Bad CRC', (done) => {
30+
const badZip = new Zip(path.join(__dirname, 'bad_crc.zip'));
31+
const entries = badZip.getEntries();
3132
assert(entries.length === 1, 'Bad CRC: Test archive contains exactly 1 file');
3233

33-
var testFile = entries.filter(function (entry) {
34+
const testFile = entries.filter(function (entry) {
3435
return entry.entryName === 'lorem_ipsum.txt';
3536
});
3637
assert(testFile.length === 1, 'Bad CRC: lorem_ipsum.txt file exists as archive entry');
3738

38-
var testFileEntryName = testFile[0].entryName;
39+
const testFileEntryName = testFile[0].entryName;
3940
badZip.readAsTextAsync(testFileEntryName, function (data, err) {
4041
assert(data && data.length, 'Bad CRC: buffer not empty');
4142
assert(err, 'Bad CRC: error object present');
43+
done();
4244
});
43-
}
44-
})();
45+
});
46+
47+
it('CRC is not changed after re-created', () => {
48+
const goodZip = new Zip(path.join(__dirname, 'good_crc.zip'));
49+
const original = goodZip.getEntries()[0].header.crc;
50+
assert.equal(original, 3528145192);
51+
const newZipPath = destination + '/good_crc_new.zip';
52+
goodZip.writeZip(newZipPath);
53+
const newZip = new Zip(newZipPath);
54+
const actual = newZip.getEntries()[0].header.crc;
55+
assert.equal(actual, original);
56+
});
57+
});

0 commit comments

Comments
 (0)