@zip.js/zip.js 2.7.63 → 2.7.64

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/index.cjs CHANGED
@@ -4287,6 +4287,8 @@ const VERSION_AES = 0x33;
4287
4287
 
4288
4288
  const DIRECTORY_SIGNATURE = "/";
4289
4289
 
4290
+ const HEADER_SIZE = 26;
4291
+
4290
4292
  const MAX_DATE = new Date(2107, 11, 31);
4291
4293
  const MIN_DATE = new Date(1980, 0, 1);
4292
4294
 
@@ -8868,6 +8870,7 @@ class Uint8ArrayReader extends Reader {
8868
8870
 
8869
8871
  constructor(array) {
8870
8872
  super();
8873
+ array = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
8871
8874
  Object.assign(this, {
8872
8875
  array,
8873
8876
  size: array.length
@@ -9562,7 +9565,8 @@ let ZipEntry$1 = class ZipEntry {
9562
9565
  compressedSize
9563
9566
  } = zipEntry;
9564
9567
  const localDirectory = fileEntry.localDirectory = {};
9565
- const dataArray = await readUint8Array(reader, offset, 30, diskNumberStart);
9568
+ const dataArraySize = HEADER_SIZE + 4;
9569
+ const dataArray = await readUint8Array(reader, offset, dataArraySize + 4, diskNumberStart);
9566
9570
  const dataView = getDataView$1(dataArray);
9567
9571
  let password = getOptionValue$1(zipEntry, options, "password");
9568
9572
  let rawPassword = getOptionValue$1(zipEntry, options, "rawPassword");
@@ -9582,7 +9586,7 @@ let ZipEntry$1 = class ZipEntry {
9582
9586
  }
9583
9587
  readCommonHeader(localDirectory, dataView, 4);
9584
9588
  localDirectory.rawExtraField = localDirectory.extraFieldLength ?
9585
- await readUint8Array(reader, offset + 30 + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
9589
+ await readUint8Array(reader, offset + dataArraySize + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
9586
9590
  new Uint8Array();
9587
9591
  readCommonFooter(zipEntry, localDirectory, dataView, 4, true);
9588
9592
  Object.assign(fileEntry, {
@@ -9601,7 +9605,7 @@ let ZipEntry$1 = class ZipEntry {
9601
9605
  throw new Error(ERR_ENCRYPTED);
9602
9606
  }
9603
9607
  }
9604
- const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
9608
+ const dataOffset = offset + dataArraySize + localDirectory.filenameLength + localDirectory.extraFieldLength;
9605
9609
  const size = compressedSize;
9606
9610
  const readable = reader.readable;
9607
9611
  Object.assign(readable, {
@@ -9963,6 +9967,7 @@ const ERR_INVALID_EXTRAFIELD_TYPE = "Extra field type exceeds 65535";
9963
9967
  const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
9964
9968
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (make sure 'keepOrder' is set to 'true')";
9965
9969
  const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
9970
+ const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
9966
9971
 
9967
9972
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
9968
9973
 
@@ -9990,6 +9995,99 @@ class ZipWriter {
9990
9995
  });
9991
9996
  }
9992
9997
 
9998
+ async prependZip(reader) {
9999
+ if (this.filenames.size) {
10000
+ throw new Error(ERR_ZIP_NOT_EMPTY);
10001
+ }
10002
+ ({ reader } = initReader(reader));
10003
+ const [zipEntriesReader, zipDataReader] = reader.readable.tee();
10004
+ const zipReader = new ZipReader(new ReadableStream({
10005
+ async start(controller) {
10006
+ const reader = zipEntriesReader.getReader();
10007
+ await readChunk();
10008
+
10009
+ async function readChunk() {
10010
+ const { done, value } = await reader.read();
10011
+ if (done) {
10012
+ controller.close();
10013
+ } else {
10014
+ controller.enqueue(value);
10015
+ await readChunk();
10016
+ }
10017
+ }
10018
+ }
10019
+ }));
10020
+ const entries = await zipReader.getEntries();
10021
+ await zipReader.close();
10022
+ await zipDataReader.pipeTo(this.writer.writable, { preventClose: true, preventAbort: true });
10023
+ this.writer.writable.size = this.offset = reader.size;
10024
+ this.filenames = new Set(entries.map(entry => entry.filename));
10025
+ this.files = new Map(entries.map(entry => {
10026
+ const {
10027
+ version,
10028
+ compressionMethod,
10029
+ lastModDate,
10030
+ lastAccessDate,
10031
+ creationDate,
10032
+ rawFilename,
10033
+ bitFlag,
10034
+ encrypted,
10035
+ uncompressedSize,
10036
+ compressedSize,
10037
+ diskOffset,
10038
+ diskNumber,
10039
+ zip64
10040
+ } = entry;
10041
+ let {
10042
+ rawExtraFieldZip64,
10043
+ rawExtraFieldAES,
10044
+ rawExtraFieldExtendedTimestamp,
10045
+ rawExtraFieldNTFS,
10046
+ rawExtraField,
10047
+ } = entry;
10048
+ const { level, languageEncodingFlag, dataDescriptor } = bitFlag;
10049
+ rawExtraFieldZip64 = rawExtraFieldZip64 || new Uint8Array();
10050
+ rawExtraFieldAES = rawExtraFieldAES || new Uint8Array();
10051
+ rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || new Uint8Array();
10052
+ rawExtraFieldNTFS = rawExtraFieldNTFS || new Uint8Array();
10053
+ rawExtraField = rawExtraField || new Uint8Array();
10054
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10055
+ const zip64UncompressedSize = zip64 && uncompressedSize > MAX_32_BITS;
10056
+ const zip64CompressedSize = zip64 && compressedSize > MAX_32_BITS;
10057
+ const {
10058
+ headerArray,
10059
+ headerView
10060
+ } = getHeaderArrayData({
10061
+ version,
10062
+ bitflag: getBitflag(level, languageEncodingFlag, dataDescriptor, encrypted),
10063
+ compressionMethod,
10064
+ uncompressedSize,
10065
+ compressedSize,
10066
+ lastModDate,
10067
+ rawFilename,
10068
+ zip64CompressedSize,
10069
+ zip64UncompressedSize,
10070
+ extraFieldLength
10071
+ });
10072
+ Object.assign(entry, {
10073
+ zip64UncompressedSize,
10074
+ zip64CompressedSize,
10075
+ zip64Offset: zip64 && this.offset - diskOffset > MAX_32_BITS,
10076
+ zip64DiskNumberStart: zip64 && diskNumber > MAX_16_BITS,
10077
+ rawExtraFieldZip64,
10078
+ rawExtraFieldAES,
10079
+ rawExtraFieldExtendedTimestamp,
10080
+ rawExtraFieldNTFS,
10081
+ rawExtraField,
10082
+ extendedTimestamp: rawExtraFieldExtendedTimestamp.length > 0 || rawExtraFieldNTFS.length > 0,
10083
+ extraFieldExtendedTimestampFlag: 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0),
10084
+ headerArray,
10085
+ headerView
10086
+ });
10087
+ return [entry.filename, entry];
10088
+ }));
10089
+ }
10090
+
9993
10091
  async add(name = "", reader, options = {}) {
9994
10092
  const zipWriter = this;
9995
10093
  const {
@@ -10025,6 +10123,22 @@ class ZipWriter {
10025
10123
  }
10026
10124
  }
10027
10125
 
10126
+ remove(entry) {
10127
+ const { filenames, files } = this;
10128
+ if (typeof entry == "string") {
10129
+ entry = files.get(entry);
10130
+ }
10131
+ if (entry && entry.filename !== UNDEFINED_VALUE) {
10132
+ const { filename } = entry;
10133
+ if (filenames.has(filename) && files.has(filename)) {
10134
+ filenames.delete(filename);
10135
+ files.delete(filename);
10136
+ return true;
10137
+ }
10138
+ }
10139
+ return false;
10140
+ }
10141
+
10028
10142
  async close(comment = new Uint8Array(), options = {}) {
10029
10143
  const zipWriter = this;
10030
10144
  const { pendingAddFileCalls, writer } = this;
@@ -10146,7 +10260,13 @@ async function addFile(zipWriter, name, reader, options) {
10146
10260
  const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
10147
10261
  const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
10148
10262
  const compressionMethod = getOptionValue(zipWriter, options, "compressionMethod");
10149
- let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
10263
+ let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor");
10264
+ if (bufferedWrite && dataDescriptor === UNDEFINED_VALUE) {
10265
+ dataDescriptor = false;
10266
+ }
10267
+ if (dataDescriptor === UNDEFINED_VALUE || zipCrypto) {
10268
+ dataDescriptor = true;
10269
+ }
10150
10270
  let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
10151
10271
  if (!zipCrypto && (password !== UNDEFINED_VALUE || rawPassword !== UNDEFINED_VALUE) && !(encryptionStrength >= 1 && encryptionStrength <= 3)) {
10152
10272
  throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
@@ -10192,10 +10312,11 @@ async function addFile(zipWriter, name, reader, options) {
10192
10312
  uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
10193
10313
  }
10194
10314
  } else {
10195
- uncompressedSize = reader.size;
10315
+ options.uncompressedSize = uncompressedSize = reader.size;
10196
10316
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
10197
10317
  }
10198
10318
  } else {
10319
+ options.uncompressedSize = uncompressedSize;
10199
10320
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
10200
10321
  }
10201
10322
  }
@@ -10251,7 +10372,10 @@ async function addFile(zipWriter, name, reader, options) {
10251
10372
  passThrough,
10252
10373
  encrypted: Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted),
10253
10374
  signature,
10254
- compressionMethod
10375
+ compressionMethod,
10376
+ uncompressedSize,
10377
+ offset: zipWriter.offset - diskOffset,
10378
+ diskNumberStart: diskNumber,
10255
10379
  });
10256
10380
  const headerInfo = getHeaderInfo(options);
10257
10381
  const dataDescriptorInfo = getDataDescriptorInfo(options);
@@ -10343,20 +10467,26 @@ async function getFileEntry(zipWriter, name, reader, entryInfo, options) {
10343
10467
  await lockPreviousFileEntry;
10344
10468
  await requestLockWriter();
10345
10469
  writingBufferedEntryData = true;
10346
- if (!dataDescriptor) {
10347
- blob = await writeExtraHeaderInfo(fileEntry, blob, writable, options);
10348
- }
10349
- await skipDiskIfNeeded(writable);
10350
10470
  fileEntry.diskNumberStart = writer.diskNumber;
10351
10471
  diskOffset = writer.diskOffset;
10472
+ fileEntry.offset = zipWriter.offset - diskOffset;
10473
+ if (fileEntry.zip64) {
10474
+ setExtraFieldZip64Data(fileEntry);
10475
+ }
10476
+ if (fileEntry.zip64 || !dataDescriptor) {
10477
+ blob = await patchLocalHeader(fileEntry, blob, writable, options);
10478
+ }
10479
+ await skipDiskIfNeeded(writable);
10352
10480
  await blob.stream().pipeTo(writable, { preventClose: true, preventAbort: true, signal });
10353
10481
  writable.size += blob.size;
10354
10482
  writingBufferedEntryData = false;
10483
+ } else {
10484
+ fileEntry.offset = zipWriter.offset - diskOffset;
10485
+ if (fileEntry.zip64) {
10486
+ setExtraFieldZip64Data(fileEntry);
10487
+ }
10355
10488
  }
10356
- fileEntry.offset = zipWriter.offset - diskOffset;
10357
- if (fileEntry.zip64) {
10358
- setZip64ExtraInfo(fileEntry, options);
10359
- } else if (fileEntry.offset > MAX_32_BITS) {
10489
+ if (fileEntry.offset > MAX_32_BITS && !fileEntry.zip64) {
10360
10490
  throw new Error(ERR_UNSUPPORTED_FORMAT);
10361
10491
  }
10362
10492
  zipWriter.offset += fileEntry.size;
@@ -10423,12 +10553,15 @@ async function createFileEntry(reader, writer, { diskNumberStart, lock }, entryI
10423
10553
  const {
10424
10554
  localHeaderArray,
10425
10555
  headerArray,
10556
+ headerView,
10426
10557
  lastModDate,
10427
10558
  rawLastModDate,
10428
10559
  encrypted,
10429
10560
  compressed,
10430
10561
  version,
10431
10562
  compressionMethod,
10563
+ rawExtraFieldZip64,
10564
+ localExtraFieldZip64Length,
10432
10565
  rawExtraFieldExtendedTimestamp,
10433
10566
  extraFieldExtendedTimestampFlag,
10434
10567
  rawExtraFieldNTFS,
@@ -10477,6 +10610,8 @@ async function createFileEntry(reader, writer, { diskNumberStart, lock }, entryI
10477
10610
  rawFilename,
10478
10611
  commentUTF8: true,
10479
10612
  rawComment,
10613
+ rawExtraFieldZip64,
10614
+ localExtraFieldZip64Length,
10480
10615
  rawExtraFieldExtendedTimestamp,
10481
10616
  rawExtraFieldNTFS,
10482
10617
  rawExtraFieldAES,
@@ -10530,28 +10665,8 @@ async function createFileEntry(reader, writer, { diskNumberStart, lock }, entryI
10530
10665
  } else {
10531
10666
  await writeData(writable, localHeaderArray);
10532
10667
  }
10533
- let rawExtraFieldZip64;
10534
- if (zip64) {
10535
- let rawExtraFieldZip64Length = 4;
10536
- if (zip64UncompressedSize) {
10537
- rawExtraFieldZip64Length += 8;
10538
- }
10539
- if (zip64CompressedSize) {
10540
- rawExtraFieldZip64Length += 8;
10541
- }
10542
- if (zip64Offset) {
10543
- rawExtraFieldZip64Length += 8;
10544
- }
10545
- if (zip64DiskNumberStart) {
10546
- rawExtraFieldZip64Length += 4;
10547
- }
10548
- rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
10549
- } else {
10550
- rawExtraFieldZip64 = new Uint8Array();
10551
- }
10552
10668
  setEntryInfo({
10553
10669
  signature,
10554
- rawExtraFieldZip64,
10555
10670
  compressedSize,
10556
10671
  uncompressedSize,
10557
10672
  headerInfo,
@@ -10573,8 +10688,8 @@ async function createFileEntry(reader, writer, { diskNumberStart, lock }, entryI
10573
10688
  compressionMethod,
10574
10689
  version,
10575
10690
  headerArray,
10691
+ headerView,
10576
10692
  signature,
10577
- rawExtraFieldZip64,
10578
10693
  extraFieldExtendedTimestampFlag,
10579
10694
  zip64UncompressedSize,
10580
10695
  zip64CompressedSize,
@@ -10599,10 +10714,62 @@ function getHeaderInfo(options) {
10599
10714
  rawExtraField,
10600
10715
  encryptionStrength,
10601
10716
  extendedTimestamp,
10602
- encrypted
10717
+ passThrough,
10718
+ encrypted,
10719
+ zip64UncompressedSize,
10720
+ zip64CompressedSize,
10721
+ zip64Offset,
10722
+ zip64DiskNumberStart,
10723
+ uncompressedSize,
10724
+ offset,
10725
+ diskNumberStart
10603
10726
  } = options;
10604
10727
  let { version, compressionMethod } = options;
10605
10728
  const compressed = !directory && (level > 0 || (level === UNDEFINED_VALUE && compressionMethod !== 0));
10729
+ let rawExtraFieldZip64;
10730
+ const uncompressedFile = passThrough || !compressed;
10731
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
10732
+ if (zip64) {
10733
+ let rawExtraFieldZip64Length = 4;
10734
+ if (zip64UncompressedSize) {
10735
+ rawExtraFieldZip64Length += 8;
10736
+ }
10737
+ if (zip64CompressedSize) {
10738
+ rawExtraFieldZip64Length += 8;
10739
+ }
10740
+ if (zip64Offset) {
10741
+ rawExtraFieldZip64Length += 8;
10742
+ }
10743
+ if (zip64DiskNumberStart) {
10744
+ rawExtraFieldZip64Length += 4;
10745
+ }
10746
+ rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
10747
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10748
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
10749
+ setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
10750
+ if (zip64ExtraFieldComplete) {
10751
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10752
+ let rawExtraFieldZip64Offset = 4;
10753
+ if (zip64UncompressedSize) {
10754
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10755
+ rawExtraFieldZip64Offset += 8;
10756
+ }
10757
+ if (zip64CompressedSize && uncompressedFile) {
10758
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10759
+ rawExtraFieldZip64Offset += 8;
10760
+ }
10761
+ if (zip64Offset) {
10762
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
10763
+ rawExtraFieldZip64Offset += 8;
10764
+ }
10765
+ if (zip64DiskNumberStart) {
10766
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
10767
+ rawExtraFieldZip64Offset += 4;
10768
+ }
10769
+ }
10770
+ } else {
10771
+ rawExtraFieldZip64 = new Uint8Array();
10772
+ }
10606
10773
  let rawExtraFieldAES;
10607
10774
  if (encrypted && !zipCrypto) {
10608
10775
  rawExtraFieldAES = new Uint8Array(getLength(EXTRAFIELD_DATA_AES) + 2);
@@ -10651,69 +10818,52 @@ function getHeaderInfo(options) {
10651
10818
  } else {
10652
10819
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
10653
10820
  }
10654
- let bitFlag = 0;
10655
- if (useUnicodeFileNames) {
10656
- bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
10657
- }
10658
- if (dataDescriptor) {
10659
- bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
10660
- }
10661
10821
  if (compressionMethod === UNDEFINED_VALUE) {
10662
10822
  compressionMethod = compressed ? COMPRESSION_METHOD_DEFLATE : COMPRESSION_METHOD_STORE;
10663
10823
  }
10664
- if (compressionMethod == COMPRESSION_METHOD_DEFLATE) {
10665
- if (level >= 1 && level < 3) {
10666
- bitFlag = bitFlag | 0b110;
10667
- }
10668
- if (level >= 3 && level < 5) {
10669
- bitFlag = bitFlag | 0b01;
10670
- }
10671
- if (level === 9) {
10672
- bitFlag = bitFlag | 0b10;
10673
- }
10674
- }
10675
10824
  if (zip64) {
10676
10825
  version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
10677
10826
  }
10678
- if (encrypted) {
10679
- bitFlag = bitFlag | BITFLAG_ENCRYPTED;
10680
- if (!zipCrypto) {
10681
- version = version > VERSION_AES ? version : VERSION_AES;
10682
- rawExtraFieldAES[9] = compressionMethod;
10683
- compressionMethod = COMPRESSION_METHOD_AES;
10684
- }
10685
- }
10686
- const headerArray = new Uint8Array(26);
10687
- const headerView = getDataView(headerArray);
10688
- setUint16(headerView, 0, version);
10689
- setUint16(headerView, 2, bitFlag);
10690
- setUint16(headerView, 4, compressionMethod);
10691
- const dateArray = new Uint32Array(1);
10692
- const dateView = getDataView(dateArray);
10693
- let lastModDateMsDos;
10694
- if (lastModDate < MIN_DATE) {
10695
- lastModDateMsDos = MIN_DATE;
10696
- } else if (lastModDate > MAX_DATE) {
10697
- lastModDateMsDos = MAX_DATE;
10698
- } else {
10699
- lastModDateMsDos = lastModDate;
10827
+ if (encrypted && !zipCrypto) {
10828
+ version = version > VERSION_AES ? version : VERSION_AES;
10829
+ rawExtraFieldAES[9] = compressionMethod;
10830
+ compressionMethod = COMPRESSION_METHOD_AES;
10700
10831
  }
10701
- setUint16(dateView, 0, (((lastModDateMsDos.getHours() << 6) | lastModDateMsDos.getMinutes()) << 5) | lastModDateMsDos.getSeconds() / 2);
10702
- setUint16(dateView, 2, ((((lastModDateMsDos.getFullYear() - 1980) << 4) | (lastModDateMsDos.getMonth() + 1)) << 5) | lastModDateMsDos.getDate());
10703
- const rawLastModDate = dateArray[0];
10704
- setUint32(headerView, 6, rawLastModDate);
10705
- setUint16(headerView, 22, getLength(rawFilename));
10706
- const extraFieldLength = getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10707
- setUint16(headerView, 24, extraFieldLength);
10708
- const localHeaderArray = new Uint8Array(30 + getLength(rawFilename) + extraFieldLength);
10832
+ const localExtraFieldZip64Length = zip64ExtraFieldComplete ? getLength(rawExtraFieldZip64) : 0;
10833
+ const extraFieldLength = localExtraFieldZip64Length + getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10834
+ const {
10835
+ headerArray,
10836
+ headerView,
10837
+ rawLastModDate
10838
+ } = getHeaderArrayData({
10839
+ version,
10840
+ bitflag: getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted),
10841
+ compressionMethod,
10842
+ uncompressedSize,
10843
+ lastModDate: lastModDate < MIN_DATE ? MIN_DATE : lastModDate > MAX_DATE ? MAX_DATE : lastModDate,
10844
+ rawFilename,
10845
+ zip64CompressedSize,
10846
+ zip64UncompressedSize,
10847
+ extraFieldLength
10848
+ });
10849
+ let localHeaderOffset = HEADER_SIZE + 4;
10850
+ const localHeaderArray = new Uint8Array(localHeaderOffset + getLength(rawFilename) + extraFieldLength);
10709
10851
  const localHeaderView = getDataView(localHeaderArray);
10710
10852
  setUint32(localHeaderView, 0, LOCAL_FILE_HEADER_SIGNATURE);
10711
10853
  arraySet(localHeaderArray, headerArray, 4);
10712
- arraySet(localHeaderArray, rawFilename, 30);
10713
- arraySet(localHeaderArray, rawExtraFieldAES, 30 + getLength(rawFilename));
10714
- arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + getLength(rawFilename, rawExtraFieldAES));
10715
- arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp));
10716
- arraySet(localHeaderArray, rawExtraField, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS));
10854
+ arraySet(localHeaderArray, rawFilename, localHeaderOffset);
10855
+ localHeaderOffset += getLength(rawFilename);
10856
+ if (zip64ExtraFieldComplete) {
10857
+ arraySet(localHeaderArray, rawExtraFieldZip64, localHeaderOffset);
10858
+ }
10859
+ localHeaderOffset += localExtraFieldZip64Length;
10860
+ arraySet(localHeaderArray, rawExtraFieldAES, localHeaderOffset);
10861
+ localHeaderOffset += getLength(rawExtraFieldAES);
10862
+ arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, localHeaderOffset);
10863
+ localHeaderOffset += getLength(rawExtraFieldExtendedTimestamp);
10864
+ arraySet(localHeaderArray, rawExtraFieldNTFS, localHeaderOffset);
10865
+ localHeaderOffset += getLength(rawExtraFieldNTFS);
10866
+ arraySet(localHeaderArray, rawExtraField, localHeaderOffset);
10717
10867
  return {
10718
10868
  localHeaderArray,
10719
10869
  headerArray,
@@ -10725,6 +10875,8 @@ function getHeaderInfo(options) {
10725
10875
  version,
10726
10876
  compressionMethod,
10727
10877
  extraFieldExtendedTimestampFlag,
10878
+ rawExtraFieldZip64,
10879
+ localExtraFieldZip64Length,
10728
10880
  rawExtraFieldExtendedTimestamp,
10729
10881
  rawExtraFieldNTFS,
10730
10882
  rawExtraFieldAES,
@@ -10779,7 +10931,6 @@ function getDataDescriptorInfo(options) {
10779
10931
  function setEntryInfo(entryInfo, options) {
10780
10932
  const {
10781
10933
  signature,
10782
- rawExtraFieldZip64,
10783
10934
  compressedSize,
10784
10935
  uncompressedSize,
10785
10936
  headerInfo,
@@ -10795,8 +10946,6 @@ function setEntryInfo(entryInfo, options) {
10795
10946
  } = dataDescriptorInfo;
10796
10947
  const {
10797
10948
  zip64,
10798
- zip64UncompressedSize,
10799
- zip64CompressedSize,
10800
10949
  zipCrypto,
10801
10950
  dataDescriptor
10802
10951
  } = options;
@@ -10807,19 +10956,6 @@ function setEntryInfo(entryInfo, options) {
10807
10956
  }
10808
10957
  }
10809
10958
  if (zip64) {
10810
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10811
- setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
10812
- setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
10813
- let rawExtraFieldZip64Offset = 4;
10814
- if (zip64UncompressedSize) {
10815
- setUint32(headerView, 18, MAX_32_BITS);
10816
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10817
- rawExtraFieldZip64Offset += 8;
10818
- }
10819
- if (zip64CompressedSize) {
10820
- setUint32(headerView, 14, MAX_32_BITS);
10821
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(compressedSize));
10822
- }
10823
10959
  if (dataDescriptor) {
10824
10960
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
10825
10961
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
@@ -10834,44 +10970,82 @@ function setEntryInfo(entryInfo, options) {
10834
10970
  }
10835
10971
  }
10836
10972
 
10837
- async function writeExtraHeaderInfo(fileEntry, entryData, writable, { zipCrypto }) {
10973
+ async function patchLocalHeader(fileEntry, entryData, writable, { zipCrypto, dataDescriptor }) {
10974
+ const {
10975
+ zip64,
10976
+ rawFilename,
10977
+ localExtraFieldZip64Length,
10978
+ signature,
10979
+ encrypted,
10980
+ zip64UncompressedSize,
10981
+ zip64CompressedSize,
10982
+ zip64Offset,
10983
+ zip64DiskNumberStart,
10984
+ uncompressedSize,
10985
+ compressedSize,
10986
+ offset,
10987
+ diskNumberStart
10988
+ } = fileEntry;
10838
10989
  let arrayBuffer;
10839
- arrayBuffer = await entryData.slice(0, 26).arrayBuffer();
10840
- if (arrayBuffer.byteLength != 26) {
10841
- arrayBuffer = arrayBuffer.slice(0, 26);
10990
+ let rawExtraFieldZip64Offset = HEADER_SIZE + 4 + getLength(rawFilename);
10991
+ const arrayBufferSize = zip64 && localExtraFieldZip64Length ? rawExtraFieldZip64Offset + localExtraFieldZip64Length : HEADER_SIZE;
10992
+ arrayBuffer = await entryData.slice(0, arrayBufferSize).arrayBuffer();
10993
+ if (arrayBuffer.byteLength != arrayBufferSize) {
10994
+ arrayBuffer = arrayBuffer.slice(0, arrayBufferSize);
10842
10995
  }
10843
10996
  const arrayBufferView = new DataView(arrayBuffer);
10844
- if (!fileEntry.encrypted || zipCrypto) {
10845
- setUint32(arrayBufferView, 14, fileEntry.signature);
10997
+ if (!dataDescriptor) {
10998
+ if (!encrypted || zipCrypto) {
10999
+ setUint32(arrayBufferView, 14, signature);
11000
+ }
11001
+ if (!zip64) {
11002
+ setUint32(arrayBufferView, 18, compressedSize);
11003
+ setUint32(arrayBufferView, 22, uncompressedSize);
11004
+ }
10846
11005
  }
10847
- if (fileEntry.zip64) {
11006
+ if (zip64) {
10848
11007
  setUint32(arrayBufferView, 18, MAX_32_BITS);
10849
11008
  setUint32(arrayBufferView, 22, MAX_32_BITS);
10850
- } else {
10851
- setUint32(arrayBufferView, 18, fileEntry.compressedSize);
10852
- setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
11009
+ if (localExtraFieldZip64Length) {
11010
+ rawExtraFieldZip64Offset += 4;
11011
+ if (zip64UncompressedSize) {
11012
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
11013
+ rawExtraFieldZip64Offset += 8;
11014
+ }
11015
+ if (zip64CompressedSize) {
11016
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(compressedSize));
11017
+ rawExtraFieldZip64Offset += 8;
11018
+ }
11019
+ if (zip64Offset) {
11020
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(offset));
11021
+ rawExtraFieldZip64Offset += 8;
11022
+ }
11023
+ if (zip64DiskNumberStart) {
11024
+ setUint32(arrayBufferView, rawExtraFieldZip64Offset, diskNumberStart);
11025
+ }
11026
+ }
10853
11027
  }
10854
11028
  await writeData(writable, new Uint8Array(arrayBuffer));
10855
11029
  return entryData.slice(arrayBuffer.byteLength);
10856
11030
  }
10857
11031
 
10858
- function setZip64ExtraInfo(fileEntry, options) {
10859
- const { rawExtraFieldZip64, offset, diskNumberStart } = fileEntry;
10860
- const { zip64UncompressedSize, zip64CompressedSize, zip64Offset, zip64DiskNumberStart } = options;
10861
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
11032
+ function setExtraFieldZip64Data(fileEntry) {
11033
+ const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
10862
11034
  let rawExtraFieldZip64Offset = 4;
10863
- if (zip64UncompressedSize) {
11035
+ if (fileEntry.zip64UncompressedSize) {
11036
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.uncompressedSize));
10864
11037
  rawExtraFieldZip64Offset += 8;
10865
11038
  }
10866
- if (zip64CompressedSize) {
11039
+ if (fileEntry.zip64CompressedSize) {
11040
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.compressedSize));
10867
11041
  rawExtraFieldZip64Offset += 8;
10868
11042
  }
10869
- if (zip64Offset) {
10870
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
11043
+ if (fileEntry.zip64Offset) {
11044
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.offset));
10871
11045
  rawExtraFieldZip64Offset += 8;
10872
11046
  }
10873
- if (zip64DiskNumberStart) {
10874
- setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
11047
+ if (fileEntry.zip64DiskNumberStart) {
11048
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, fileEntry.diskNumberStart);
10875
11049
  }
10876
11050
  }
10877
11051
 
@@ -10906,7 +11080,7 @@ async function closeFile(zipWriter, comment, options) {
10906
11080
  } else {
10907
11081
  rawExtraFieldTimestamp = new Uint8Array();
10908
11082
  }
10909
- fileEntry.rawExtraFieldCDExtendedTimestamp = rawExtraFieldTimestamp;
11083
+ fileEntry.rawExtraFieldExtendedTimestamp = rawExtraFieldTimestamp;
10910
11084
  directoryDataLength += 46 +
10911
11085
  getLength(
10912
11086
  rawFilename,
@@ -10927,12 +11101,13 @@ async function closeFile(zipWriter, comment, options) {
10927
11101
  rawFilename,
10928
11102
  rawExtraFieldZip64,
10929
11103
  rawExtraFieldAES,
10930
- rawExtraFieldCDExtendedTimestamp,
11104
+ rawExtraFieldExtendedTimestamp,
10931
11105
  rawExtraFieldNTFS,
10932
11106
  rawExtraField,
10933
11107
  rawComment,
10934
11108
  versionMadeBy,
10935
11109
  headerArray,
11110
+ headerView,
10936
11111
  zip64,
10937
11112
  zip64UncompressedSize,
10938
11113
  zip64CompressedSize,
@@ -10944,10 +11119,9 @@ async function closeFile(zipWriter, comment, options) {
10944
11119
  uncompressedSize,
10945
11120
  compressedSize
10946
11121
  } = fileEntry;
10947
- const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
11122
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10948
11123
  setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
10949
11124
  setUint16(directoryView, offset + 4, versionMadeBy);
10950
- const headerView = getDataView(headerArray);
10951
11125
  if (!zip64UncompressedSize) {
10952
11126
  setUint32(headerView, 18, uncompressedSize);
10953
11127
  }
@@ -10955,28 +11129,40 @@ async function closeFile(zipWriter, comment, options) {
10955
11129
  setUint32(headerView, 14, compressedSize);
10956
11130
  }
10957
11131
  arraySet(directoryArray, headerArray, offset + 6);
10958
- setUint16(directoryView, offset + 30, extraFieldLength);
10959
- setUint16(directoryView, offset + 32, getLength(rawComment));
10960
- setUint16(directoryView, offset + 34, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
10961
- setUint16(directoryView, offset + 36, internalFileAttributes);
11132
+ let directoryOffset = offset + HEADER_SIZE + 4;
11133
+ setUint16(directoryView, directoryOffset, extraFieldLength);
11134
+ directoryOffset += 2;
11135
+ setUint16(directoryView, directoryOffset, getLength(rawComment));
11136
+ directoryOffset += 2;
11137
+ setUint16(directoryView, directoryOffset, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
11138
+ directoryOffset += 2;
11139
+ setUint16(directoryView, directoryOffset, internalFileAttributes);
11140
+ directoryOffset += 2;
10962
11141
  if (externalFileAttributes) {
10963
- setUint32(directoryView, offset + 38, externalFileAttributes);
10964
- }
10965
- setUint32(directoryView, offset + 42, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
10966
- arraySet(directoryArray, rawFilename, offset + 46);
10967
- arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + getLength(rawFilename));
10968
- arraySet(directoryArray, rawExtraFieldAES, offset + 46 + getLength(rawFilename, rawExtraFieldZip64));
10969
- arraySet(directoryArray, rawExtraFieldCDExtendedTimestamp, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES));
10970
- arraySet(directoryArray, rawExtraFieldNTFS, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp));
10971
- arraySet(directoryArray, rawExtraField, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS));
10972
- arraySet(directoryArray, rawComment, offset + 46 + getLength(rawFilename) + extraFieldLength);
10973
- const directoryEntryLength = 46 + getLength(rawFilename, rawComment) + extraFieldLength;
11142
+ setUint32(directoryView, directoryOffset, externalFileAttributes);
11143
+ }
11144
+ directoryOffset += 4;
11145
+ setUint32(directoryView, directoryOffset, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
11146
+ directoryOffset += 4;
11147
+ arraySet(directoryArray, rawFilename, directoryOffset);
11148
+ directoryOffset += getLength(rawFilename);
11149
+ arraySet(directoryArray, rawExtraFieldZip64, directoryOffset);
11150
+ directoryOffset += getLength(rawExtraFieldZip64);
11151
+ arraySet(directoryArray, rawExtraFieldAES, directoryOffset);
11152
+ directoryOffset += getLength(rawExtraFieldAES);
11153
+ arraySet(directoryArray, rawExtraFieldExtendedTimestamp, directoryOffset);
11154
+ directoryOffset += getLength(rawExtraFieldExtendedTimestamp);
11155
+ arraySet(directoryArray, rawExtraFieldNTFS, directoryOffset);
11156
+ directoryOffset += getLength(rawExtraFieldNTFS);
11157
+ arraySet(directoryArray, rawExtraField, directoryOffset);
11158
+ directoryOffset += getLength(rawExtraField);
11159
+ arraySet(directoryArray, rawComment, directoryOffset);
10974
11160
  if (offset - directoryDiskOffset > writer.availableSize) {
10975
11161
  writer.availableSize = 0;
10976
11162
  await writeData(writable, directoryArray.slice(directoryDiskOffset, offset));
10977
11163
  directoryDiskOffset = offset;
10978
11164
  }
10979
- offset += directoryEntryLength;
11165
+ offset = directoryOffset;
10980
11166
  if (options.onprogress) {
10981
11167
  try {
10982
11168
  await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
@@ -11104,6 +11290,67 @@ function getLength(...arrayLikes) {
11104
11290
  return result;
11105
11291
  }
11106
11292
 
11293
+ function getHeaderArrayData({
11294
+ version,
11295
+ bitflag,
11296
+ compressionMethod,
11297
+ uncompressedSize,
11298
+ compressedSize,
11299
+ lastModDate,
11300
+ rawFilename,
11301
+ zip64CompressedSize,
11302
+ zip64UncompressedSize,
11303
+ extraFieldLength
11304
+ }) {
11305
+ const headerArray = new Uint8Array(HEADER_SIZE);
11306
+ const headerView = getDataView(headerArray);
11307
+ setUint16(headerView, 0, version);
11308
+ setUint16(headerView, 2, bitflag);
11309
+ setUint16(headerView, 4, compressionMethod);
11310
+ const dateArray = new Uint32Array(1);
11311
+ const dateView = getDataView(dateArray);
11312
+ setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
11313
+ setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
11314
+ const rawLastModDate = dateArray[0];
11315
+ setUint32(headerView, 6, rawLastModDate);
11316
+ if (zip64CompressedSize || compressedSize !== UNDEFINED_VALUE) {
11317
+ setUint32(headerView, 14, zip64CompressedSize ? MAX_32_BITS : compressedSize);
11318
+ }
11319
+ if (zip64UncompressedSize || uncompressedSize !== UNDEFINED_VALUE) {
11320
+ setUint32(headerView, 18, zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
11321
+ }
11322
+ setUint16(headerView, 22, getLength(rawFilename));
11323
+ setUint16(headerView, 24, extraFieldLength);
11324
+ return {
11325
+ headerArray,
11326
+ headerView,
11327
+ rawLastModDate
11328
+ };
11329
+ }
11330
+
11331
+ function getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted) {
11332
+ let bitFlag = 0;
11333
+ if (useUnicodeFileNames) {
11334
+ bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
11335
+ }
11336
+ if (dataDescriptor) {
11337
+ bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
11338
+ }
11339
+ if (level >= 1 && level < 3) {
11340
+ bitFlag = bitFlag | 0b110;
11341
+ }
11342
+ if (level >= 3 && level < 5) {
11343
+ bitFlag = bitFlag | 0b01;
11344
+ }
11345
+ if (level === 9) {
11346
+ bitFlag = bitFlag | 0b10;
11347
+ }
11348
+ if (encrypted) {
11349
+ bitFlag = bitFlag | BITFLAG_ENCRYPTED;
11350
+ }
11351
+ return bitFlag;
11352
+ }
11353
+
11107
11354
  /*
11108
11355
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
11109
11356
 
@@ -11850,9 +12097,8 @@ async function exportZip(zipWriter, entry, totalSize, options) {
11850
12097
  });
11851
12098
  }
11852
12099
  }
11853
- await zipWriter.add(name, child.reader, Object.assign({
11854
- directory: child.directory
11855
- }, Object.assign({}, options, zipEntryOptions, childOptions, {
12100
+ await zipWriter.add(name, child.reader, Object.assign({}, options, zipEntryOptions, childOptions, {
12101
+ directory: child.directory,
11856
12102
  onprogress: async indexProgress => {
11857
12103
  if (options.onprogress) {
11858
12104
  entryOffsets.set(name, indexProgress);
@@ -11864,7 +12110,7 @@ async function exportZip(zipWriter, entry, totalSize, options) {
11864
12110
  }
11865
12111
  }
11866
12112
  }
11867
- })));
12113
+ }));
11868
12114
  await process(zipWriter, child);
11869
12115
  }
11870
12116
  }
@@ -12045,6 +12291,7 @@ exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
12045
12291
  exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
12046
12292
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
12047
12293
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;
12294
+ exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;
12048
12295
  exports.HttpRangeReader = HttpRangeReader;
12049
12296
  exports.HttpReader = HttpReader;
12050
12297
  exports.Reader = Reader;