From ed5fd83075bf26ff0cec7371628e065f74d007b9 Mon Sep 17 00:00:00 2001 From: teppeis Date: Fri, 23 Nov 2018 15:34:21 +0900 Subject: [PATCH] fix: CRC is unexpectedly changed after zip is re-created --- .gitignore | 7 ++--- headers/entryHeader.js | 2 +- package.json | 2 +- test/crc/index.js | 59 ++++++++++++++++++++++++++---------------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 779d3cf..a3396ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -node_modules/ -test/issue_*/unzipped/ +node_modules +/test/issue_*/unzipped/ +xxx .idea -*.iml \ No newline at end of file +*.iml diff --git a/headers/entryHeader.js b/headers/entryHeader.js index bdfef48..476ba22 100644 --- a/headers/entryHeader.js +++ b/headers/entryHeader.js @@ -213,7 +213,7 @@ module.exports = function () { // modification time (2 bytes time, 2 bytes date) data.writeUInt32LE(_time, Constants.CENTIM); // uncompressed file crc-32 value - data.writeInt32LE(_crc & 0xFFFF, Constants.CENCRC, true); + data.writeUInt32LE(_crc, Constants.CENCRC); // compressed size data.writeUInt32LE(_compressedSize, Constants.CENSIZ); // uncompressed size diff --git a/package.json b/package.json index 232ce13..9798512 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.4.12", "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", "scripts": { - "test": "mocha test/mocha.js" + "test": "mocha test/mocha.js test/crc/index.js" }, "keywords": [ "zip", diff --git a/test/crc/index.js b/test/crc/index.js index 850a133..afad39d 100644 --- a/test/crc/index.js +++ b/test/crc/index.js @@ -1,44 +1,57 @@ -;(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(); +const assert = require('assert'); +const path = require('path'); +const Zip = require('../../adm-zip'); +const rimraf = require('rimraf') + +describe('crc', () => { + const destination = __dirname + '/xxx' + + beforeEach(done => rimraf(destination, done)) + + it('Good CRC', (done) => { + const goodZip = new Zip(path.join(__dirname, 'good_crc.zip')); + const entries = goodZip.getEntries(); assert(entries.length === 1, 'Good CRC: Test archive contains exactly 1 file'); - var testFile = entries.filter(function (entry) { + const 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; + const 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'); + done(); }); - } + }); - // Bad CRC - function testBadCrc() { - var badZip = new Zip(path.join(__dirname, 'bad_crc.zip')); - var entries = badZip.getEntries(); + it('Bad CRC', (done) => { + const badZip = new Zip(path.join(__dirname, 'bad_crc.zip')); + const entries = badZip.getEntries(); assert(entries.length === 1, 'Bad CRC: Test archive contains exactly 1 file'); - var testFile = entries.filter(function (entry) { + const 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; + const 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'); + done(); }); - } -})(); + }); + + it('CRC is not changed after re-created', () => { + const goodZip = new Zip(path.join(__dirname, 'good_crc.zip')); + const original = goodZip.getEntries()[0].header.crc; + assert.equal(original, 3528145192); + const newZipPath = destination + '/good_crc_new.zip'; + goodZip.writeZip(newZipPath); + const newZip = new Zip(newZipPath); + const actual = newZip.getEntries()[0].header.crc; + assert.equal(actual, original); + }); +});