Skip to content

Commit 416271a

Browse files
authored
Merge pull request #505 from 5saviahv/decoder
inital "decoder" functionality
2 parents 626dd94 + 4d84157 commit 416271a

File tree

7 files changed

+57
-19
lines changed

7 files changed

+57
-19
lines changed

adm-zip.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ module.exports = function (/**String*/ input, /** object */ options) {
4646
// instanciate utils filesystem
4747
const filetools = new Utils(opts);
4848

49+
opts.decoder = Utils.decoder;
50+
if (typeof opts.decoder !== "object" || typeof opts.decoder.encode !== "function" || typeof opts.decoder.decode !== "function") {
51+
opts.decoder = Utils.decoder;
52+
}
53+
4954
// if input is file name we retrieve its content
5055
if (input && "string" === typeof input) {
5156
// load zip file
@@ -553,7 +558,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
553558

554559
// prepare new entry
555560
if (!update) {
556-
entry = new ZipEntry();
561+
entry = new ZipEntry(opts);
557562
entry.entryName = entryName;
558563
}
559564
entry.comment = comment || "";
@@ -731,7 +736,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
731736
throw new Error(Utils.Errors.NO_ZIP);
732737
}
733738
_zip.entries.forEach(function (entry) {
734-
var entryName = sanitize(targetPath, canonical(entry.entryName.toString()));
739+
var entryName = sanitize(targetPath, canonical(entry.entryName));
735740
if (entry.isDirectory) {
736741
filetools.makeDir(entryName);
737742
return;
@@ -784,7 +789,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
784789

785790
targetPath = pth.resolve(targetPath);
786791
// convert entryName to
787-
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName.toString())));
792+
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName)));
788793
const getError = (msg, file) => new Error(msg + ': "' + file + '"');
789794

790795
// separate directories from files
@@ -819,7 +824,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
819824
if (err) {
820825
next(err);
821826
} else {
822-
const entryName = pth.normalize(canonical(entry.entryName.toString()));
827+
const entryName = pth.normalize(canonical(entry.entryName));
823828
const filePath = sanitize(targetPath, entryName);
824829
entry.getDataAsync(function (content, err_1) {
825830
if (err_1) {

headers/entryHeader.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ module.exports = function () {
6363
_flags = val;
6464
},
6565

66+
get flags_efs() {
67+
return (_flags & Constants.FLG_EFS) > 0;
68+
},
69+
set flags_efs(val) {
70+
if (val) {
71+
_flags |= Constants.FLG_EFS;
72+
} else {
73+
_flags &= ~Constants.FLG_EFS;
74+
}
75+
},
76+
6677
get method() {
6778
return _method;
6879
},

util/decoder.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
efs: true,
3+
encode: (data) => Buffer.from(data, "utf8"),
4+
decode: (data) => data.toString("utf8")
5+
};

util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ module.exports = require("./utils");
22
module.exports.Constants = require("./constants");
33
module.exports.Errors = require("./errors");
44
module.exports.FileAttr = require("./fattr");
5+
module.exports.decoder = require("./decoder");

util/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ Utils.sanitize = function (/*string*/ prefix, /*string*/ name) {
267267
};
268268

269269
// converts buffer, Uint8Array, string types to buffer
270-
Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input) {
270+
Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input, /* function */ encoder) {
271271
if (Buffer.isBuffer(input)) {
272272
return input;
273273
} else if (input instanceof Uint8Array) {
274274
return Buffer.from(input);
275275
} else {
276276
// expect string all other values are invalid and return empty buffer
277-
return typeof input === "string" ? Buffer.from(input, "utf8") : Buffer.alloc(0);
277+
return typeof input === "string" ? encoder(input) : Buffer.alloc(0);
278278
}
279279
};
280280

zipEntry.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ var Utils = require("./util"),
33
Constants = Utils.Constants,
44
Methods = require("./methods");
55

6-
module.exports = function (/*Buffer*/ input) {
6+
module.exports = function (/** object */ options, /*Buffer*/ input) {
77
var _centralHeader = new Headers.EntryHeader(),
88
_entryName = Buffer.alloc(0),
99
_comment = Buffer.alloc(0),
1010
_isDirectory = false,
1111
uncompressedData = null,
12-
_extra = Buffer.alloc(0);
12+
_extra = Buffer.alloc(0),
13+
_efs = true;
14+
15+
// assign options
16+
const opts = options;
17+
18+
const decoder = typeof opts.decoder === "object" ? opts.decoder : Utils.decoder;
19+
_efs = decoder.hasOwnProperty("efs") ? decoder.efs : false;
1320

1421
function getCompressedDataFromZip() {
1522
//if (!input || !Buffer.isBuffer(input)) {
@@ -198,18 +205,26 @@ module.exports = function (/*Buffer*/ input) {
198205

199206
return {
200207
get entryName() {
201-
return _entryName.toString();
208+
return decoder.decode(_entryName);
202209
},
203210
get rawEntryName() {
204211
return _entryName;
205212
},
206213
set entryName(val) {
207-
_entryName = Utils.toBuffer(val);
214+
_entryName = Utils.toBuffer(val, decoder.encode);
208215
var lastChar = _entryName[_entryName.length - 1];
209216
_isDirectory = lastChar === 47 || lastChar === 92;
210217
_centralHeader.fileNameLength = _entryName.length;
211218
},
212219

220+
get efs() {
221+
if (typeof _efs === "function") {
222+
return _efs(this.entryName);
223+
} else {
224+
return _efs;
225+
}
226+
},
227+
213228
get extra() {
214229
return _extra;
215230
},
@@ -220,10 +235,10 @@ module.exports = function (/*Buffer*/ input) {
220235
},
221236

222237
get comment() {
223-
return _comment.toString();
238+
return decoder.decode(_comment);
224239
},
225240
set comment(val) {
226-
_comment = Utils.toBuffer(val);
241+
_comment = Utils.toBuffer(val, decoder.encode);
227242
_centralHeader.commentLength = _comment.length;
228243
},
229244

@@ -249,7 +264,7 @@ module.exports = function (/*Buffer*/ input) {
249264
},
250265

251266
setData: function (value) {
252-
uncompressedData = Utils.toBuffer(value);
267+
uncompressedData = Utils.toBuffer(value, Utils.decoder.encode);
253268
if (!_isDirectory && uncompressedData.length) {
254269
_centralHeader.size = uncompressedData.length;
255270
_centralHeader.method = Utils.Constants.DEFLATED;
@@ -293,6 +308,7 @@ module.exports = function (/*Buffer*/ input) {
293308
},
294309

295310
packCentralHeader: function () {
311+
_centralHeader.flag_efs = this.efs;
296312
// 1. create header (buffer)
297313
var header = _centralHeader.centralHeaderToBinary();
298314
var addpos = Utils.Constants.CENHDR;

zipFile.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
1111
var password = null;
1212

1313
// assign options
14-
const opts = Object.assign(Object.create(null), options);
14+
const opts = options;
1515

16-
const { noSort } = opts;
16+
const { noSort, decoder } = opts;
1717

1818
if (inBuffer) {
1919
// is a memory buffer
@@ -29,7 +29,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
2929

3030
for (let i = 0; i < totalEntries; i++) {
3131
let tmp = index;
32-
const entry = new ZipEntry(inBuffer);
32+
const entry = new ZipEntry(opts, inBuffer);
3333

3434
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
3535
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
@@ -50,7 +50,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
5050
var index = mainHeader.offset; // offset of first CEN header
5151
for (var i = 0; i < entryList.length; i++) {
5252
var tmp = index,
53-
entry = new ZipEntry(inBuffer);
53+
entry = new ZipEntry(opts, inBuffer);
5454
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
5555

5656
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
@@ -134,10 +134,10 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
134134
* @return {String}
135135
*/
136136
get comment() {
137-
return _comment.toString();
137+
return decoder.decode(_comment);
138138
},
139139
set comment(val) {
140-
_comment = Utils.toBuffer(val);
140+
_comment = Utils.toBuffer(val, decoder.encode);
141141
mainHeader.commentLength = _comment.length;
142142
},
143143

0 commit comments

Comments
 (0)