From 1a334b235803309d421c762daec55d5a35dcf591 Mon Sep 17 00:00:00 2001 From: Kosuke Suzuki Date: Sun, 27 Sep 2020 02:20:06 +0900 Subject: [PATCH] add multibyte-encoded comment with byte length instead of character length --- package.json | 2 +- test/multibyte/test.js | 24 ++++++++++++++++++ .../\343\201\230\343\201\243\343\201\267.zip" | Bin 0 -> 363 bytes .../\343\201\230\343\201\243\343\201\267.txt" | 1 + zipFile.js | 4 +-- 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/multibyte/test.js create mode 100644 "test/multibyte/\343\201\230\343\201\243\343\201\267.zip" create mode 100644 "test/multibyte/\343\201\230\343\201\243\343\201\267/\343\201\230\343\201\243\343\201\267.txt" diff --git a/package.json b/package.json index e035aa8..1bbbe2e 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.4.16", "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/crc/index.js" + "test": "mocha test/mocha.js test/crc/index.js test/multibyte/test.js" }, "keywords": [ "zip", diff --git a/test/multibyte/test.js b/test/multibyte/test.js new file mode 100644 index 0000000..33a97c7 --- /dev/null +++ b/test/multibyte/test.js @@ -0,0 +1,24 @@ +const { expect } = require("chai"); +const Utils = require("../../util"); +const AdmZip = require("../../adm-zip"); +const path = require("path"); + +describe('adm-zip', () => { + it('adds multibyte ZIP comment in UTF-8 with appropriate byte', () => { + const zip = new AdmZip(); + zip.addLocalFile( + path.join(__dirname, "./じっぷ/じっぷ.txt") + ); + zip.addZipComment("じっぷ"); + const willSend = zip.toBuffer(); + const end = willSend.slice( + willSend.lastIndexOf(Utils.Constants.ENDSIG) + ); + const commentLength = end.readInt16LE(Utils.Constants.ENDCOM, 2); + expect(commentLength).to.eq(9); + const expected = Buffer.from("じっぷ"); + const actual = end.slice(Utils.Constants.ENDCOM + 2); + expect(actual).to.include(expected); + expect(expected).to.include(actual); + }); +}); diff --git "a/test/multibyte/\343\201\230\343\201\243\343\201\267.zip" "b/test/multibyte/\343\201\230\343\201\243\343\201\267.zip" new file mode 100644 index 0000000000000000000000000000000000000000..ec11111305364ce2a8a183db392ea21c21030466 GIT binary patch literal 363 zcmWIWW@h1HVBlb2xXfT3$bba67-Se8H_mw6xcG77cKy&0P6p<`5&7{TTw1}+z{v7~ znSlXJ1i;O>0W{<9)@WX!i3}htj$#JfG`*6F60m{nk@@j^fEdj{xI(S~Z$>6LW?T-G zfI3%z;jbfz3D*a7E-S>jXeJ|@hv`_Dc?=9&8eLG#gPRO-I?!;C(=iNZWdk{b2?*B$ L>G?p+iEtDEJd0od literal 0 HcmV?d00001 diff --git "a/test/multibyte/\343\201\230\343\201\243\343\201\267/\343\201\230\343\201\243\343\201\267.txt" "b/test/multibyte/\343\201\230\343\201\243\343\201\267/\343\201\230\343\201\243\343\201\267.txt" new file mode 100644 index 0000000..ffdf4e6 --- /dev/null +++ "b/test/multibyte/\343\201\230\343\201\243\343\201\267/\343\201\230\343\201\243\343\201\267.txt" @@ -0,0 +1 @@ +じっぷ diff --git a/zipFile.js b/zipFile.js index 505da9d..1d580ed 100644 --- a/zipFile.js +++ b/zipFile.js @@ -133,8 +133,8 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) { return _comment.toString(); }, set comment(val) { - mainHeader.commentLength = val.length; - _comment = val; + _comment = Utils.toBuffer(val); + mainHeader.commentLength = _comment.length; }, getEntryCount: function() {