@node-projects/jszip 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,269 @@
1
+ import readerFor from "./reader/readerFor.js";
2
+ import utils from "./utils.js";
3
+ import sig from "./signature.js";
4
+ import { ZipEntry } from "./zipEntry.js";
5
+ import support from "./support.js";
6
+
7
+ export class ZipEntries {
8
+ // class ZipEntries {{{
9
+ /**
10
+ * All the entries in the zip file.
11
+ * @constructor
12
+ * @param {Object} loadOptions Options for loading the stream.
13
+ */
14
+ constructor(loadOptions) {
15
+ this.files = [];
16
+ this.loadOptions = loadOptions;
17
+ }
18
+
19
+ /**
20
+ * Check that the reader is on the specified signature.
21
+ * @param {string} expectedSignature the expected signature.
22
+ * @throws {Error} if it is an other signature.
23
+ */
24
+ checkSignature(expectedSignature) {
25
+ if (!this.reader.readAndCheckSignature(expectedSignature)) {
26
+ this.reader.index -= 4;
27
+ var signature = this.reader.readString(4);
28
+ throw new Error("Corrupted zip or bug: unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")");
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Check if the given signature is at the given index.
34
+ * @param {number} askedIndex the index to check.
35
+ * @param {string} expectedSignature the signature to expect.
36
+ * @return {boolean} true if the signature is here, false otherwise.
37
+ */
38
+ isSignature(askedIndex, expectedSignature) {
39
+ var currentIndex = this.reader.index;
40
+ this.reader.setIndex(askedIndex);
41
+ var signature = this.reader.readString(4);
42
+ var result = signature === expectedSignature;
43
+ this.reader.setIndex(currentIndex);
44
+ return result;
45
+ }
46
+
47
+ /**
48
+ * Read the end of the central directory.
49
+ */
50
+ readBlockEndOfCentral() {
51
+ this.diskNumber = this.reader.readInt(2);
52
+ this.diskWithCentralDirStart = this.reader.readInt(2);
53
+ this.centralDirRecordsOnThisDisk = this.reader.readInt(2);
54
+ this.centralDirRecords = this.reader.readInt(2);
55
+ this.centralDirSize = this.reader.readInt(4);
56
+ this.centralDirOffset = this.reader.readInt(4);
57
+
58
+ this.zipCommentLength = this.reader.readInt(2);
59
+ // warning : the encoding depends of the system locale
60
+ // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded.
61
+ // On a windows machine, this field is encoded with the localized windows code page.
62
+ var zipComment = this.reader.readData(this.zipCommentLength);
63
+ var decodeParamType = support.uint8array ? "uint8array" : "array";
64
+ // To get consistent behavior with the generation part, we will assume that
65
+ // this is utf8 encoded unless specified otherwise.
66
+ var decodeContent = utils.transformTo(decodeParamType, zipComment);
67
+ this.zipComment = this.loadOptions.decodeFileName(decodeContent);
68
+ }
69
+
70
+ /**
71
+ * Read the end of the Zip 64 central directory.
72
+ * Not merged with the method readEndOfCentral :
73
+ * The end of central can coexist with its Zip64 brother,
74
+ * I don't want to read the wrong number of bytes !
75
+ */
76
+ readBlockZip64EndOfCentral() {
77
+ this.zip64EndOfCentralSize = this.reader.readInt(8);
78
+ this.reader.skip(4);
79
+ // this.versionMadeBy = this.reader.readString(2);
80
+ // this.versionNeeded = this.reader.readInt(2);
81
+ this.diskNumber = this.reader.readInt(4);
82
+ this.diskWithCentralDirStart = this.reader.readInt(4);
83
+ this.centralDirRecordsOnThisDisk = this.reader.readInt(8);
84
+ this.centralDirRecords = this.reader.readInt(8);
85
+ this.centralDirSize = this.reader.readInt(8);
86
+ this.centralDirOffset = this.reader.readInt(8);
87
+
88
+ this.zip64ExtensibleData = {};
89
+ var extraDataSize = this.zip64EndOfCentralSize - 44,
90
+ index = 0,
91
+ extraFieldId,
92
+ extraFieldLength,
93
+ extraFieldValue;
94
+ while (index < extraDataSize) {
95
+ extraFieldId = this.reader.readInt(2);
96
+ extraFieldLength = this.reader.readInt(4);
97
+ extraFieldValue = this.reader.readData(extraFieldLength);
98
+ this.zip64ExtensibleData[extraFieldId] = {
99
+ id: extraFieldId,
100
+ length: extraFieldLength,
101
+ value: extraFieldValue
102
+ };
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Read the end of the Zip 64 central directory locator.
108
+ */
109
+ readBlockZip64EndOfCentralLocator() {
110
+ this.diskWithZip64CentralDirStart = this.reader.readInt(4);
111
+ this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8);
112
+ this.disksCount = this.reader.readInt(4);
113
+ if (this.disksCount > 1) {
114
+ throw new Error("Multi-volumes zip are not supported");
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Read the local files, based on the offset read in the central part.
120
+ */
121
+ readLocalFiles() {
122
+ var i, file;
123
+ for (i = 0; i < this.files.length; i++) {
124
+ file = this.files[i];
125
+ this.reader.setIndex(file.localHeaderOffset);
126
+ this.checkSignature(sig.LOCAL_FILE_HEADER);
127
+ file.readLocalPart(this.reader);
128
+ file.handleUTF8();
129
+ file.processAttributes();
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Read the central directory.
135
+ */
136
+ readCentralDir() {
137
+ var file;
138
+
139
+ this.reader.setIndex(this.centralDirOffset);
140
+ while (this.reader.readAndCheckSignature(sig.CENTRAL_FILE_HEADER)) {
141
+ file = new ZipEntry({
142
+ zip64: this.zip64
143
+ }, this.loadOptions);
144
+ file.readCentralPart(this.reader);
145
+ this.files.push(file);
146
+ }
147
+
148
+ if (this.centralDirRecords !== this.files.length) {
149
+ if (this.centralDirRecords !== 0 && this.files.length === 0) {
150
+ // We expected some records but couldn't find ANY.
151
+ // This is really suspicious, as if something went wrong.
152
+ throw new Error("Corrupted zip or bug: expected " + this.centralDirRecords + " records in central dir, got " + this.files.length);
153
+ } else {
154
+ // We found some records but not all.
155
+ // Something is wrong but we got something for the user: no error here.
156
+ // console.warn("expected", this.centralDirRecords, "records in central dir, got", this.files.length);
157
+ }
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Read the end of central directory.
163
+ */
164
+ readEndOfCentral() {
165
+ var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END);
166
+ if (offset < 0) {
167
+ // Check if the content is a truncated zip or complete garbage.
168
+ // A "LOCAL_FILE_HEADER" is not required at the beginning (auto
169
+ // extractible zip for example) but it can give a good hint.
170
+ // If an ajax request was used without responseType, we will also
171
+ // get unreadable data.
172
+ var isGarbage = !this.isSignature(0, sig.LOCAL_FILE_HEADER);
173
+
174
+ if (isGarbage) {
175
+ throw new Error("Can't find end of central directory : is this a zip file ? " +
176
+ "If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html");
177
+ } else {
178
+ throw new Error("Corrupted zip: can't find end of central directory");
179
+ }
180
+
181
+ }
182
+ this.reader.setIndex(offset);
183
+ var endOfCentralDirOffset = offset;
184
+ this.checkSignature(sig.CENTRAL_DIRECTORY_END);
185
+ this.readBlockEndOfCentral();
186
+
187
+
188
+ /* extract from the zip spec :
189
+ 4) If one of the fields in the end of central directory
190
+ record is too small to hold required data, the field
191
+ should be set to -1 (0xFFFF or 0xFFFFFFFF) and the
192
+ ZIP64 format record should be created.
193
+ 5) The end of central directory record and the
194
+ Zip64 end of central directory locator record must
195
+ reside on the same disk when splitting or spanning
196
+ an archive.
197
+ */
198
+ if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) {
199
+ this.zip64 = true;
200
+
201
+ /*
202
+ Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from
203
+ the zip file can fit into a 32bits integer. This cannot be solved : JavaScript represents
204
+ all numbers as 64-bit double precision IEEE 754 floating point numbers.
205
+ So, we have 53bits for integers and bitwise operations treat everything as 32bits.
206
+ see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators
207
+ and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5
208
+ */
209
+
210
+ // should look for a zip64 EOCD locator
211
+ offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
212
+ if (offset < 0) {
213
+ throw new Error("Corrupted zip: can't find the ZIP64 end of central directory locator");
214
+ }
215
+ this.reader.setIndex(offset);
216
+ this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
217
+ this.readBlockZip64EndOfCentralLocator();
218
+
219
+ // now the zip64 EOCD record
220
+ if (!this.isSignature(this.relativeOffsetEndOfZip64CentralDir, sig.ZIP64_CENTRAL_DIRECTORY_END)) {
221
+ // console.warn("ZIP64 end of central directory not where expected.");
222
+ this.relativeOffsetEndOfZip64CentralDir = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
223
+ if (this.relativeOffsetEndOfZip64CentralDir < 0) {
224
+ throw new Error("Corrupted zip: can't find the ZIP64 end of central directory");
225
+ }
226
+ }
227
+ this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir);
228
+ this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
229
+ this.readBlockZip64EndOfCentral();
230
+ }
231
+
232
+ var expectedEndOfCentralDirOffset = this.centralDirOffset + this.centralDirSize;
233
+ if (this.zip64) {
234
+ expectedEndOfCentralDirOffset += 20; // end of central dir 64 locator
235
+ expectedEndOfCentralDirOffset += 12 /* should not include the leading 12 bytes */ + this.zip64EndOfCentralSize;
236
+ }
237
+
238
+ var extraBytes = endOfCentralDirOffset - expectedEndOfCentralDirOffset;
239
+
240
+ if (extraBytes > 0) {
241
+ // console.warn(extraBytes, "extra bytes at beginning or within zipfile");
242
+ if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) {
243
+ // The offsets seem wrong, but we have something at the specified offset.
244
+ // So… we keep it.
245
+ } else {
246
+ // the offset is wrong, update the "zero" of the reader
247
+ // this happens if data has been prepended (crx files for example)
248
+ this.reader.zero = extraBytes;
249
+ }
250
+ } else if (extraBytes < 0) {
251
+ throw new Error("Corrupted zip: missing " + Math.abs(extraBytes) + " bytes.");
252
+ }
253
+ }
254
+
255
+ prepareReader(data) {
256
+ this.reader = readerFor(data);
257
+ }
258
+
259
+ /**
260
+ * Read a zip file and create ZipEntries.
261
+ * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file.
262
+ */
263
+ load(data) {
264
+ this.prepareReader(data);
265
+ this.readEndOfCentral();
266
+ this.readCentralDir();
267
+ this.readLocalFiles();
268
+ }
269
+ };
@@ -0,0 +1,296 @@
1
+ import readerFor from "./reader/readerFor.js";
2
+ import utils from "./utils.js";
3
+ import { CompressedObject } from "./compressedObject.js";
4
+ import crc32fn from "./crc32.js";
5
+ import utf8 from "./utf8.js";
6
+ import compressions from "./compressions.js";
7
+ import support from "./support.js";
8
+
9
+ var MADE_BY_DOS = 0x00;
10
+ var MADE_BY_UNIX = 0x03;
11
+
12
+ /**
13
+ * Find a compression registered in JSZip.
14
+ * @param {string} compressionMethod the method magic to find.
15
+ * @return {Object|null} the JSZip compression object, null if none found.
16
+ */
17
+ var findCompression = function (compressionMethod) {
18
+ for (var method in compressions) {
19
+ if (!Object.prototype.hasOwnProperty.call(compressions, method)) {
20
+ continue;
21
+ }
22
+ if (compressions[method].magic === compressionMethod) {
23
+ return compressions[method];
24
+ }
25
+ }
26
+ return null;
27
+ };
28
+
29
+ export class ZipEntry {
30
+ // class ZipEntry {{{
31
+ /**
32
+ * An entry in the zip file.
33
+ * @constructor
34
+ * @param {Object} options Options of the current file.
35
+ * @param {Object} loadOptions Options for loading the stream.
36
+ */
37
+ constructor(options, loadOptions) {
38
+ this.options = options;
39
+ this.loadOptions = loadOptions;
40
+ }
41
+
42
+ /**
43
+ * say if the file is encrypted.
44
+ * @return {boolean} true if the file is encrypted, false otherwise.
45
+ */
46
+ isEncrypted() {
47
+ // bit 1 is set
48
+ return (this.bitFlag & 0x0001) === 0x0001;
49
+ }
50
+
51
+ /**
52
+ * say if the file has utf-8 filename/comment.
53
+ * @return {boolean} true if the filename/comment is in utf-8, false otherwise.
54
+ */
55
+ useUTF8() {
56
+ // bit 11 is set
57
+ return (this.bitFlag & 0x0800) === 0x0800;
58
+ }
59
+
60
+ /**
61
+ * Read the local part of a zip file and add the info in this object.
62
+ * @param {DataReader} reader the reader to use.
63
+ */
64
+ readLocalPart(reader) {
65
+ var compression, localExtraFieldsLength;
66
+
67
+ // we already know everything from the central dir !
68
+ // If the central dir data are false, we are doomed.
69
+ // On the bright side, the local part is scary : zip64, data descriptors, both, etc.
70
+ // The less data we get here, the more reliable this should be.
71
+ // Let's skip the whole header and dash to the data !
72
+ reader.skip(22);
73
+ // in some zip created on windows, the filename stored in the central dir contains \ instead of /.
74
+ // Strangely, the filename here is OK.
75
+ // I would love to treat these zip files as corrupted (see http://www.info-zip.org/FAQ.html#backslashes
76
+ // or APPNOTE#4.4.17.1, "All slashes MUST be forward slashes '/'") but there are a lot of bad zip generators...
77
+ // Search "unzip mismatching "local" filename continuing with "central" filename version" on
78
+ // the internet.
79
+ //
80
+ // I think I see the logic here : the central directory is used to display
81
+ // content and the local directory is used to extract the files. Mixing / and \
82
+ // may be used to display \ to windows users and use / when extracting the files.
83
+ // Unfortunately, this lead also to some issues : http://seclists.org/fulldisclosure/2009/Sep/394
84
+ this.fileNameLength = reader.readInt(2);
85
+ localExtraFieldsLength = reader.readInt(2); // can't be sure this will be the same as the central dir
86
+ // the fileName is stored as binary data, the handleUTF8 method will take care of the encoding.
87
+ this.fileName = reader.readData(this.fileNameLength);
88
+ reader.skip(localExtraFieldsLength);
89
+
90
+ if (this.compressedSize === -1 || this.uncompressedSize === -1) {
91
+ throw new Error("Bug or corrupted zip : didn't get enough information from the central directory " + "(compressedSize === -1 || uncompressedSize === -1)");
92
+ }
93
+
94
+ compression = findCompression(this.compressionMethod);
95
+ if (compression === null) { // no compression found
96
+ throw new Error("Corrupted zip : compression " + utils.pretty(this.compressionMethod) + " unknown (inner file : " + utils.transformTo("string", this.fileName) + ")");
97
+ }
98
+ this.decompressed = new CompressedObject(this.compressedSize, this.uncompressedSize, this.crc32, compression, reader.readData(this.compressedSize));
99
+ }
100
+
101
+ /**
102
+ * Read the central part of a zip file and add the info in this object.
103
+ * @param {DataReader} reader the reader to use.
104
+ */
105
+ readCentralPart(reader) {
106
+ this.versionMadeBy = reader.readInt(2);
107
+ reader.skip(2);
108
+ // this.versionNeeded = reader.readInt(2);
109
+ this.bitFlag = reader.readInt(2);
110
+ this.compressionMethod = reader.readString(2);
111
+ this.date = reader.readDate();
112
+ this.crc32 = reader.readInt(4);
113
+ this.compressedSize = reader.readInt(4);
114
+ this.uncompressedSize = reader.readInt(4);
115
+ var fileNameLength = reader.readInt(2);
116
+ this.extraFieldsLength = reader.readInt(2);
117
+ this.fileCommentLength = reader.readInt(2);
118
+ this.diskNumberStart = reader.readInt(2);
119
+ this.internalFileAttributes = reader.readInt(2);
120
+ this.externalFileAttributes = reader.readInt(4);
121
+ this.localHeaderOffset = reader.readInt(4);
122
+
123
+ if (this.isEncrypted()) {
124
+ throw new Error("Encrypted zip are not supported");
125
+ }
126
+
127
+ // will be read in the local part, see the comments there
128
+ reader.skip(fileNameLength);
129
+ this.readExtraFields(reader);
130
+ this.parseZIP64ExtraField(reader);
131
+ this.fileComment = reader.readData(this.fileCommentLength);
132
+ }
133
+
134
+ /**
135
+ * Parse the external file attributes and get the unix/dos permissions.
136
+ */
137
+ processAttributes() {
138
+ this.unixPermissions = null;
139
+ this.dosPermissions = null;
140
+ var madeBy = this.versionMadeBy >> 8;
141
+
142
+ // Check if we have the DOS directory flag set.
143
+ // We look for it in the DOS and UNIX permissions
144
+ // but some unknown platform could set it as a compatibility flag.
145
+ this.dir = this.externalFileAttributes & 0x0010 ? true : false;
146
+
147
+ if (madeBy === MADE_BY_DOS) {
148
+ // first 6 bits (0 to 5)
149
+ this.dosPermissions = this.externalFileAttributes & 0x3F;
150
+ }
151
+
152
+ if (madeBy === MADE_BY_UNIX) {
153
+ this.unixPermissions = (this.externalFileAttributes >> 16) & 0xFFFF;
154
+ // the octal permissions are in (this.unixPermissions & 0x01FF).toString(8);
155
+ }
156
+
157
+ // fail safe : if the name ends with a / it probably means a folder
158
+ if (!this.dir && this.fileNameStr.slice(-1) === "/") {
159
+ this.dir = true;
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Parse the ZIP64 extra field and merge the info in the current ZipEntry.
165
+ * @param {DataReader} reader the reader to use.
166
+ */
167
+ parseZIP64ExtraField() {
168
+ if (!this.extraFields[0x0001]) {
169
+ return;
170
+ }
171
+
172
+ // should be something, preparing the extra reader
173
+ var extraReader = readerFor(this.extraFields[0x0001].value);
174
+
175
+ // I really hope that these 64bits integer can fit in 32 bits integer, because js
176
+ // won't let us have more.
177
+ if (this.uncompressedSize === utils.MAX_VALUE_32BITS) {
178
+ this.uncompressedSize = extraReader.readInt(8);
179
+ }
180
+ if (this.compressedSize === utils.MAX_VALUE_32BITS) {
181
+ this.compressedSize = extraReader.readInt(8);
182
+ }
183
+ if (this.localHeaderOffset === utils.MAX_VALUE_32BITS) {
184
+ this.localHeaderOffset = extraReader.readInt(8);
185
+ }
186
+ if (this.diskNumberStart === utils.MAX_VALUE_32BITS) {
187
+ this.diskNumberStart = extraReader.readInt(4);
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Read the central part of a zip file and add the info in this object.
193
+ * @param {DataReader} reader the reader to use.
194
+ */
195
+ readExtraFields(reader) {
196
+ var end = reader.index + this.extraFieldsLength,
197
+ extraFieldId,
198
+ extraFieldLength,
199
+ extraFieldValue;
200
+
201
+ if (!this.extraFields) {
202
+ this.extraFields = {};
203
+ }
204
+
205
+ while (reader.index + 4 < end) {
206
+ extraFieldId = reader.readInt(2);
207
+ extraFieldLength = reader.readInt(2);
208
+ extraFieldValue = reader.readData(extraFieldLength);
209
+
210
+ this.extraFields[extraFieldId] = {
211
+ id: extraFieldId,
212
+ length: extraFieldLength,
213
+ value: extraFieldValue
214
+ };
215
+ }
216
+
217
+ reader.setIndex(end);
218
+ }
219
+
220
+ /**
221
+ * Apply an UTF8 transformation if needed.
222
+ */
223
+ handleUTF8() {
224
+ var decodeParamType = support.uint8array ? "uint8array" : "array";
225
+ if (this.useUTF8()) {
226
+ this.fileNameStr = utf8.utf8decode(this.fileName);
227
+ this.fileCommentStr = utf8.utf8decode(this.fileComment);
228
+ } else {
229
+ var upath = this.findExtraFieldUnicodePath();
230
+ if (upath !== null) {
231
+ this.fileNameStr = upath;
232
+ } else {
233
+ // ASCII text or unsupported code page
234
+ var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName);
235
+ this.fileNameStr = this.loadOptions.decodeFileName(fileNameByteArray);
236
+ }
237
+
238
+ var ucomment = this.findExtraFieldUnicodeComment();
239
+ if (ucomment !== null) {
240
+ this.fileCommentStr = ucomment;
241
+ } else {
242
+ // ASCII text or unsupported code page
243
+ var commentByteArray = utils.transformTo(decodeParamType, this.fileComment);
244
+ this.fileCommentStr = this.loadOptions.decodeFileName(commentByteArray);
245
+ }
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Find the unicode path declared in the extra field, if any.
251
+ * @return {String} the unicode path, null otherwise.
252
+ */
253
+ findExtraFieldUnicodePath() {
254
+ var upathField = this.extraFields[0x7075];
255
+ if (upathField) {
256
+ var extraReader = readerFor(upathField.value);
257
+
258
+ // wrong version
259
+ if (extraReader.readInt(1) !== 1) {
260
+ return null;
261
+ }
262
+
263
+ // the crc of the filename changed, this field is out of date.
264
+ if (crc32fn(this.fileName) !== extraReader.readInt(4)) {
265
+ return null;
266
+ }
267
+
268
+ return utf8.utf8decode(extraReader.readData(upathField.length - 5));
269
+ }
270
+ return null;
271
+ }
272
+
273
+ /**
274
+ * Find the unicode comment declared in the extra field, if any.
275
+ * @return {String} the unicode comment, null otherwise.
276
+ */
277
+ findExtraFieldUnicodeComment() {
278
+ var ucommentField = this.extraFields[0x6375];
279
+ if (ucommentField) {
280
+ var extraReader = readerFor(ucommentField.value);
281
+
282
+ // wrong version
283
+ if (extraReader.readInt(1) !== 1) {
284
+ return null;
285
+ }
286
+
287
+ // the crc of the comment changed, this field is out of date.
288
+ if (crc32fn(this.fileComment) !== extraReader.readInt(4)) {
289
+ return null;
290
+ }
291
+
292
+ return utf8.utf8decode(extraReader.readData(ucommentField.length - 5));
293
+ }
294
+ return null;
295
+ }
296
+ };
@@ -0,0 +1,122 @@
1
+ import { StreamHelper } from "./stream/StreamHelper.js";
2
+ import { DataWorker } from "./stream/DataWorker.js";
3
+ import utf8 from "./utf8.js";
4
+ import { CompressedObject } from "./compressedObject.js";
5
+ import { GenericWorker } from "./stream/GenericWorker.js";
6
+
7
+ export class ZipObject {
8
+ /**
9
+ * A simple object representing a file in the zip file.
10
+ * @constructor
11
+ * @param {string} name the name of the file
12
+ * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data
13
+ * @param {Object} options the options of the file
14
+ */
15
+ constructor(name, data, options) {
16
+ this.name = name;
17
+ this.dir = options.dir;
18
+ this.date = options.date;
19
+ this.comment = options.comment;
20
+ this.unixPermissions = options.unixPermissions;
21
+ this.dosPermissions = options.dosPermissions;
22
+
23
+ this._data = data;
24
+ this._dataBinary = options.binary;
25
+ // keep only the compression
26
+ this.options = {
27
+ compression: options.compression,
28
+ compressionOptions: options.compressionOptions
29
+ };
30
+ };
31
+
32
+ /**
33
+ * Create an internal stream for the content of this object.
34
+ * @param {String} type the type of each chunk.
35
+ * @return StreamHelper the stream.
36
+ */
37
+ internalStream(type) {
38
+ var result = null, outputType = "string";
39
+ try {
40
+ if (!type) {
41
+ throw new Error("No output type specified.");
42
+ }
43
+ outputType = type.toLowerCase();
44
+ var askUnicodeString = outputType === "string" || outputType === "text";
45
+ if (outputType === "binarystring" || outputType === "text") {
46
+ outputType = "string";
47
+ }
48
+ result = this._decompressWorker();
49
+
50
+ var isUnicodeString = !this._dataBinary;
51
+
52
+ if (isUnicodeString && !askUnicodeString) {
53
+ result = result.pipe(new utf8.Utf8EncodeWorker());
54
+ }
55
+ if (!isUnicodeString && askUnicodeString) {
56
+ result = result.pipe(new utf8.Utf8DecodeWorker());
57
+ }
58
+ } catch (e) {
59
+ result = new GenericWorker("error");
60
+ result.error(e);
61
+ }
62
+
63
+ return new StreamHelper(result, outputType, "");
64
+ }
65
+
66
+ /**
67
+ * Prepare the content in the asked type.
68
+ * @param {String} type the type of the result.
69
+ * @param {Function} onUpdate a function to call on each internal update.
70
+ * @return Promise the promise of the result.
71
+ */
72
+ async(type, onUpdate) {
73
+ return this.internalStream(type).accumulate(onUpdate);
74
+ }
75
+
76
+ /**
77
+ * Prepare the content as a nodejs stream.
78
+ * @param {String} type the type of each chunk.
79
+ * @param {Function} onUpdate a function to call on each internal update.
80
+ * @return Stream the stream.
81
+ */
82
+ nodeStream(type, onUpdate) {
83
+ return this.internalStream(type || "nodebuffer").toNodejsStream(onUpdate);
84
+ }
85
+
86
+ /**
87
+ * Return a worker for the compressed content.
88
+ * @private
89
+ * @param {Object} compression the compression object to use.
90
+ * @param {Object} compressionOptions the options to use when compressing.
91
+ * @return Worker the worker.
92
+ */
93
+ _compressWorker(compression, compressionOptions) {
94
+ if (
95
+ this._data instanceof CompressedObject &&
96
+ this._data.compression.magic === compression.magic
97
+ ) {
98
+ return this._data.getCompressedWorker();
99
+ } else {
100
+ var result = this._decompressWorker();
101
+ if (!this._dataBinary) {
102
+ result = result.pipe(new utf8.Utf8EncodeWorker());
103
+ }
104
+ return CompressedObject.createWorkerFrom(result, compression, compressionOptions);
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Return a worker for the decompressed content.
110
+ * @private
111
+ * @return Worker the worker.
112
+ */
113
+ _decompressWorker() {
114
+ if (this._data instanceof CompressedObject) {
115
+ return this._data.getContentWorker();
116
+ } else if (this._data instanceof GenericWorker) {
117
+ return this._data;
118
+ } else {
119
+ return new DataWorker(this._data);
120
+ }
121
+ }
122
+ };