@zip.js/zip.js 2.6.41 → 2.6.43

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.
package/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # Introduction
2
2
 
3
- zip.js is an open-source library (BSD-3-Clause license) implemented in
4
- JavaScript to compress and decompress zip files. It has been designed to handle
5
- large amounts of data and notably supports multi-core compression, compression
6
- streams, Zip64 and encryption.
3
+ zip.js is a JavaScript open-source library (BSD-3-Clause license) for
4
+ compressing and decompressing zip files. It was designed to handle large amounts
5
+ of data. It supports notably multi-core compression, native compression with
6
+ compression streams, files larger than 4GB with Zip64, split zip files and data
7
+ encryption.
7
8
 
8
9
  # Documentation
9
10
 
@@ -92,7 +93,7 @@ const zipFileBlobPromise = new Response(zipFileStream.readable).blob();
92
93
  const helloWorldReadable = new Blob(["Hello world!"]).stream();
93
94
 
94
95
  // Creates a ZipWriter object writing data into `zipFileStream.writable`, adds
95
- // the entry "hello.txt" containing the text "Hello world!" retrieved from
96
+ // the entry "hello.txt" containing the text "Hello world!" retrieved from
96
97
  // `helloWorldReadable`, and closes the writer.
97
98
  const zipWriter = new ZipWriter(zipFileStream.writable);
98
99
  await zipWriter.add("hello.txt", helloWorldReadable);
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env -S deno run --allow-write --allow-read --allow-net
2
2
 
3
- /* global Deno, fetch, URL, TextEncoderStream */
3
+ /* global Deno, URL, TextEncoderStream */
4
4
 
5
5
  import { parse as parseArgs } from "https://deno.land/std@0.147.0/flags/mod.ts";
6
6
  import { normalize as normalizePath, resolve as resolvePath, extname } from "https://deno.land/std@0.147.0/path/mod.ts";
7
- import { configure, ZipWriter, terminateWorkers } from "../index.js";
7
+ import { configure, ZipWriter, terminateWorkers, HttpReader } from "../index.js";
8
8
 
9
9
  const args = parseArgs(Deno.args);
10
10
  const stdout = getTextWriter(Deno.stdout);
@@ -81,9 +81,9 @@ async function addFile(zipWriter, file) {
81
81
  onstart: () => stdout.write(" adding: " + file.name + "\n")
82
82
  };
83
83
  try {
84
- const readable = await getReadable(file);
85
- if (readable) {
86
- await zipWriter.add(file.name, { readable }, options);
84
+ const reader = await getReader(file);
85
+ if (reader) {
86
+ await zipWriter.add(file.name, reader, options);
87
87
  }
88
88
  } catch (error) {
89
89
  await stdout.write(" error: " + error.message + ", file: " + file.url + "\n");
@@ -91,7 +91,7 @@ async function addFile(zipWriter, file) {
91
91
  }
92
92
 
93
93
  async function getFileInfo(file, options) {
94
- let name, isFile, isDirectory, resolvedName;
94
+ let name, isFile, isDirectory, resolvedName, size;
95
95
  const remote = isRemote(file);
96
96
  if (remote) {
97
97
  const url = new URL(file, import.meta.url);
@@ -111,9 +111,10 @@ async function getFileInfo(file, options) {
111
111
  const stat = await Deno.stat(resolvedName);
112
112
  isFile = stat.isFile;
113
113
  isDirectory = stat.isDirectory;
114
+ size = stat.size;
114
115
  }
115
116
  return {
116
- url: file, name, remote, isFile, isDirectory, resolvedName
117
+ url: file, name, remote, isFile, size, isDirectory, resolvedName
117
118
  };
118
119
  }
119
120
 
@@ -134,12 +135,17 @@ async function addDirectories(file, options) {
134
135
  }
135
136
  }
136
137
 
137
- async function getReadable(file) {
138
- return file.remote ?
139
- (await fetch(file.url)).body :
140
- file.isFile ?
141
- (await Deno.open(resolvePath(file.url), { read: true })).readable :
142
- null;
138
+ async function getReader(file) {
139
+ if (file.remote) {
140
+ return new HttpReader(file.url);
141
+ } else if (file.isFile) {
142
+ const { size } = file;
143
+ const { readable } = await Deno.open(resolvePath(file.url), { read: true });
144
+ return {
145
+ readable,
146
+ size
147
+ };
148
+ }
143
149
  }
144
150
 
145
151
  function isRemote(path) {
@@ -8343,14 +8343,34 @@
8343
8343
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8344
8344
  */
8345
8345
 
8346
+ const PROPERTY_NAME_FILENAME = "filename";
8347
+ const PROPERTY_NAME_RAW_FILENAME = "rawFilename";
8348
+ const PROPERTY_NAME_COMMENT = "comment";
8349
+ const PROPERTY_NAME_RAW_COMMENT = "rawComment";
8350
+ const PROPERTY_NAME_UNCOMPPRESSED_SIZE = "uncompressedSize";
8351
+ const PROPERTY_NAME_COMPPRESSED_SIZE = "compressedSize";
8352
+ const PROPERTY_NAME_OFFSET = "offset";
8353
+ const PROPERTY_NAME_DISK_NUMBER_START = "diskNumberStart";
8354
+ const PROPERTY_NAME_LAST_MODIFICATION_DATE = "lastModDate";
8355
+ const PROPERTY_NAME_RAW_LAST_MODIFICATION_DATE = "rawLastModDate";
8356
+ const PROPERTY_NAME_LAST_ACCESS_DATE = "lastAccessDate";
8357
+ const PROPERTY_NAME_RAW_LAST_ACCESS_DATE = "rawLastAccessDate";
8358
+ const PROPERTY_NAME_CREATION_DATE = "creationDate";
8359
+ const PROPERTY_NAME_RAW_CREATION_DATE = "rawCreationDate";
8360
+ const PROPERTY_NAME_INTERNAL_FILE_ATTRIBUTE = "internalFileAttribute";
8361
+ const PROPERTY_NAME_EXTERNAL_FILE_ATTRIBUTE = "externalFileAttribute";
8362
+ const PROPERTY_NAME_MS_DOS_COMPATIBLE = "msDosCompatible";
8363
+ const PROPERTY_NAME_ZIP64 = "zip64";
8364
+
8346
8365
  const PROPERTY_NAMES = [
8347
- "filename", "rawFilename", "directory", "encrypted", "compressedSize", "uncompressedSize",
8348
- "lastModDate", "rawLastModDate", "comment", "rawComment", "signature", "extraField",
8349
- "rawExtraField", "bitFlag", "extraFieldZip64", "extraFieldUnicodePath", "extraFieldUnicodeComment",
8350
- "extraFieldAES", "filenameUTF8", "commentUTF8", "offset", "zip64", "compressionMethod",
8351
- "extraFieldNTFS", "lastAccessDate", "creationDate", "extraFieldExtendedTimestamp",
8352
- "version", "versionMadeBy", "msDosCompatible", "internalFileAttribute", "externalFileAttribute",
8353
- "diskNumberStart"];
8366
+ PROPERTY_NAME_FILENAME, PROPERTY_NAME_RAW_FILENAME, PROPERTY_NAME_COMPPRESSED_SIZE, PROPERTY_NAME_UNCOMPPRESSED_SIZE,
8367
+ PROPERTY_NAME_LAST_MODIFICATION_DATE, PROPERTY_NAME_RAW_LAST_MODIFICATION_DATE, PROPERTY_NAME_COMMENT, PROPERTY_NAME_RAW_COMMENT,
8368
+ PROPERTY_NAME_LAST_ACCESS_DATE, PROPERTY_NAME_CREATION_DATE, PROPERTY_NAME_OFFSET, PROPERTY_NAME_DISK_NUMBER_START,
8369
+ PROPERTY_NAME_DISK_NUMBER_START, PROPERTY_NAME_INTERNAL_FILE_ATTRIBUTE, PROPERTY_NAME_EXTERNAL_FILE_ATTRIBUTE,
8370
+ PROPERTY_NAME_MS_DOS_COMPATIBLE, PROPERTY_NAME_ZIP64,
8371
+ "directory", "bitFlag", "encrypted", "signature", "filenameUTF8", "commentUTF8", "compressionMethod", "version", "versionMadeBy",
8372
+ "extraField", "rawExtraField", "extraFieldZip64", "extraFieldUnicodePath", "extraFieldUnicodeComment", "extraFieldAES", "extraFieldNTFS",
8373
+ "extraFieldExtendedTimestamp"];
8354
8374
 
8355
8375
  class Entry {
8356
8376
 
@@ -8402,10 +8422,10 @@
8402
8422
  const CHARSET_UTF8 = "utf-8";
8403
8423
  const CHARSET_CP437 = "cp437";
8404
8424
  const ZIP64_PROPERTIES = [
8405
- ["uncompressedSize", MAX_32_BITS],
8406
- ["compressedSize", MAX_32_BITS],
8407
- ["offset", MAX_32_BITS],
8408
- ["diskNumberStart", MAX_16_BITS]
8425
+ [PROPERTY_NAME_UNCOMPPRESSED_SIZE, MAX_32_BITS],
8426
+ [PROPERTY_NAME_COMPPRESSED_SIZE, MAX_32_BITS],
8427
+ [PROPERTY_NAME_OFFSET, MAX_32_BITS],
8428
+ [PROPERTY_NAME_DISK_NUMBER_START, MAX_16_BITS]
8409
8429
  ];
8410
8430
  const ZIP64_EXTRACTION = {
8411
8431
  [MAX_16_BITS]: {
@@ -8765,12 +8785,12 @@
8765
8785
  }
8766
8786
  const extraFieldUnicodePath = extraField.get(EXTRAFIELD_TYPE_UNICODE_PATH);
8767
8787
  if (extraFieldUnicodePath) {
8768
- await readExtraFieldUnicode(extraFieldUnicodePath, "filename", "rawFilename", directory, fileEntry);
8788
+ await readExtraFieldUnicode(extraFieldUnicodePath, PROPERTY_NAME_FILENAME, PROPERTY_NAME_RAW_FILENAME, directory, fileEntry);
8769
8789
  directory.extraFieldUnicodePath = extraFieldUnicodePath;
8770
8790
  }
8771
8791
  const extraFieldUnicodeComment = extraField.get(EXTRAFIELD_TYPE_UNICODE_COMMENT);
8772
8792
  if (extraFieldUnicodeComment) {
8773
- await readExtraFieldUnicode(extraFieldUnicodeComment, "comment", "rawComment", directory, fileEntry);
8793
+ await readExtraFieldUnicode(extraFieldUnicodeComment, PROPERTY_NAME_COMMENT, PROPERTY_NAME_RAW_COMMENT, directory, fileEntry);
8774
8794
  directory.extraFieldUnicodeComment = extraFieldUnicodeComment;
8775
8795
  }
8776
8796
  const extraFieldAES = extraField.get(EXTRAFIELD_TYPE_AES);
@@ -8884,16 +8904,16 @@
8884
8904
  const timeProperties = [];
8885
8905
  const timeRawProperties = [];
8886
8906
  if ((flags & 0x1) == 0x1) {
8887
- timeProperties.push("lastModDate");
8888
- timeRawProperties.push("rawLastModDate");
8907
+ timeProperties.push(PROPERTY_NAME_LAST_MODIFICATION_DATE);
8908
+ timeRawProperties.push(PROPERTY_NAME_RAW_LAST_MODIFICATION_DATE);
8889
8909
  }
8890
8910
  if ((flags & 0x2) == 0x2) {
8891
- timeProperties.push("lastAccessDate");
8892
- timeRawProperties.push("rawLastAccessDate");
8911
+ timeProperties.push(PROPERTY_NAME_LAST_ACCESS_DATE);
8912
+ timeRawProperties.push(PROPERTY_NAME_RAW_LAST_ACCESS_DATE);
8893
8913
  }
8894
8914
  if ((flags & 0x4) == 0x4) {
8895
- timeProperties.push("creationDate");
8896
- timeRawProperties.push("rawCreationDate");
8915
+ timeProperties.push(PROPERTY_NAME_CREATION_DATE);
8916
+ timeRawProperties.push(PROPERTY_NAME_RAW_CREATION_DATE);
8897
8917
  }
8898
8918
  let offset = 1;
8899
8919
  timeProperties.forEach((propertyName, indexProperty) => {
@@ -9120,9 +9140,9 @@
9120
9140
  if (versionMadeBy > MAX_16_BITS) {
9121
9141
  throw new Error(ERR_INVALID_VERSION);
9122
9142
  }
9123
- const lastModDate = getOptionValue(zipWriter, options, "lastModDate") || new Date();
9124
- const lastAccessDate = getOptionValue(zipWriter, options, "lastAccessDate");
9125
- const creationDate = getOptionValue(zipWriter, options, "creationDate");
9143
+ const lastModDate = getOptionValue(zipWriter, options, PROPERTY_NAME_LAST_MODIFICATION_DATE) || new Date();
9144
+ const lastAccessDate = getOptionValue(zipWriter, options, PROPERTY_NAME_LAST_ACCESS_DATE);
9145
+ const creationDate = getOptionValue(zipWriter, options, PROPERTY_NAME_CREATION_DATE);
9126
9146
  const password = getOptionValue(zipWriter, options, "password");
9127
9147
  const encryptionStrength = getOptionValue(zipWriter, options, "encryptionStrength") || 3;
9128
9148
  const zipCrypto = getOptionValue(zipWriter, options, "zipCrypto");
@@ -9160,13 +9180,13 @@
9160
9180
  keepOrder = true;
9161
9181
  }
9162
9182
  let uncompressedSize = 0;
9163
- let msDosCompatible = getOptionValue(zipWriter, options, "msDosCompatible");
9183
+ let msDosCompatible = getOptionValue(zipWriter, options, PROPERTY_NAME_MS_DOS_COMPATIBLE);
9164
9184
  if (msDosCompatible === UNDEFINED_VALUE) {
9165
9185
  msDosCompatible = true;
9166
9186
  }
9167
- const internalFileAttribute = getOptionValue(zipWriter, options, "internalFileAttribute") || 0;
9168
- const externalFileAttribute = getOptionValue(zipWriter, options, "externalFileAttribute") || 0;
9169
- let zip64 = getOptionValue(zipWriter, options, "zip64");
9187
+ const internalFileAttribute = getOptionValue(zipWriter, options, PROPERTY_NAME_INTERNAL_FILE_ATTRIBUTE) || 0;
9188
+ const externalFileAttribute = getOptionValue(zipWriter, options, PROPERTY_NAME_EXTERNAL_FILE_ATTRIBUTE) || 0;
9189
+ let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
9170
9190
  if (reader) {
9171
9191
  if (reader instanceof ReadableStream) {
9172
9192
  reader = {
@@ -9242,11 +9262,8 @@
9242
9262
  let fileEntry;
9243
9263
  try {
9244
9264
  fileEntry = await getFileEntry(zipWriter, name, reader, { headerInfo, dataDescriptorInfo }, options);
9245
- } catch (error) {
9246
- if (!error.corruptedEntry && maximumEntrySize) {
9247
- zipWriter.pendingEntriesSize -= maximumEntrySize;
9248
- }
9249
- throw error;
9265
+ } finally {
9266
+ zipWriter.pendingEntriesSize -= maximumEntrySize;
9250
9267
  }
9251
9268
  Object.assign(fileEntry, { name, comment, extraField });
9252
9269
  return new Entry(fileEntry);
@@ -9293,7 +9310,7 @@
9293
9310
  if (zipWriter.addSplitZipSignature) {
9294
9311
  delete zipWriter.addSplitZipSignature;
9295
9312
  const signatureArray = new Uint8Array(4);
9296
- const signatureArrayView = new DataView(signatureArray.buffer);
9313
+ const signatureArrayView = getDataView(signatureArray);
9297
9314
  setUint32(signatureArrayView, 0, SPLIT_ZIP_FILE_SIGNATURE);
9298
9315
  await writeData(writable, signatureArray);
9299
9316
  zipWriter.offset += 4;
@@ -9431,9 +9448,9 @@
9431
9448
  externalFileAttribute,
9432
9449
  diskNumberStart
9433
9450
  };
9451
+ let compressedSize = 0;
9434
9452
  let uncompressedSize = 0;
9435
9453
  let signature;
9436
- let compressedSize = 0;
9437
9454
  const { writable } = writer;
9438
9455
  if (reader) {
9439
9456
  reader.chunkSize = getChunkSize(config);
@@ -9828,7 +9845,7 @@
9828
9845
  const endOfdirectoryView = getDataView(endOfdirectoryArray);
9829
9846
  let lastDiskNumber = writer.diskNumber;
9830
9847
  const { availableSize } = writer;
9831
- if (availableSize < endOfdirectoryArray.length) {
9848
+ if (availableSize < getLength(endOfdirectoryArray)) {
9832
9849
  lastDiskNumber++;
9833
9850
  }
9834
9851
  offset = 0;
@@ -10582,7 +10599,7 @@
10582
10599
  configure({ baseURL });
10583
10600
  e(configure);
10584
10601
 
10585
- /// <reference types="./index.d.ts" />
10602
+ /// <reference types="./index" />
10586
10603
 
10587
10604
  configure({ Deflate: ZipDeflate, Inflate: ZipInflate });
10588
10605