@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.
@@ -4293,6 +4293,8 @@
4293
4293
 
4294
4294
  const DIRECTORY_SIGNATURE = "/";
4295
4295
 
4296
+ const HEADER_SIZE = 26;
4297
+
4296
4298
  const MAX_DATE = new Date(2107, 11, 31);
4297
4299
  const MIN_DATE = new Date(1980, 0, 1);
4298
4300
 
@@ -8874,6 +8876,7 @@
8874
8876
 
8875
8877
  constructor(array) {
8876
8878
  super();
8879
+ array = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
8877
8880
  Object.assign(this, {
8878
8881
  array,
8879
8882
  size: array.length
@@ -9568,7 +9571,8 @@
9568
9571
  compressedSize
9569
9572
  } = zipEntry;
9570
9573
  const localDirectory = fileEntry.localDirectory = {};
9571
- const dataArray = await readUint8Array(reader, offset, 30, diskNumberStart);
9574
+ const dataArraySize = HEADER_SIZE + 4;
9575
+ const dataArray = await readUint8Array(reader, offset, dataArraySize + 4, diskNumberStart);
9572
9576
  const dataView = getDataView$1(dataArray);
9573
9577
  let password = getOptionValue$1(zipEntry, options, "password");
9574
9578
  let rawPassword = getOptionValue$1(zipEntry, options, "rawPassword");
@@ -9588,7 +9592,7 @@
9588
9592
  }
9589
9593
  readCommonHeader(localDirectory, dataView, 4);
9590
9594
  localDirectory.rawExtraField = localDirectory.extraFieldLength ?
9591
- await readUint8Array(reader, offset + 30 + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
9595
+ await readUint8Array(reader, offset + dataArraySize + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
9592
9596
  new Uint8Array();
9593
9597
  readCommonFooter(zipEntry, localDirectory, dataView, 4, true);
9594
9598
  Object.assign(fileEntry, {
@@ -9607,7 +9611,7 @@
9607
9611
  throw new Error(ERR_ENCRYPTED);
9608
9612
  }
9609
9613
  }
9610
- const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
9614
+ const dataOffset = offset + dataArraySize + localDirectory.filenameLength + localDirectory.extraFieldLength;
9611
9615
  const size = compressedSize;
9612
9616
  const readable = reader.readable;
9613
9617
  Object.assign(readable, {
@@ -9969,6 +9973,7 @@
9969
9973
  const ERR_INVALID_EXTRAFIELD_DATA = "Extra field data exceeds 64KB";
9970
9974
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (make sure 'keepOrder' is set to 'true')";
9971
9975
  const ERR_UNDEFINED_UNCOMPRESSED_SIZE = "Undefined uncompressed size";
9976
+ const ERR_ZIP_NOT_EMPTY = "Zip file not empty";
9972
9977
 
9973
9978
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
9974
9979
 
@@ -9996,6 +10001,99 @@
9996
10001
  });
9997
10002
  }
9998
10003
 
10004
+ async prependZip(reader) {
10005
+ if (this.filenames.size) {
10006
+ throw new Error(ERR_ZIP_NOT_EMPTY);
10007
+ }
10008
+ ({ reader } = initReader(reader));
10009
+ const [zipEntriesReader, zipDataReader] = reader.readable.tee();
10010
+ const zipReader = new ZipReader(new ReadableStream({
10011
+ async start(controller) {
10012
+ const reader = zipEntriesReader.getReader();
10013
+ await readChunk();
10014
+
10015
+ async function readChunk() {
10016
+ const { done, value } = await reader.read();
10017
+ if (done) {
10018
+ controller.close();
10019
+ } else {
10020
+ controller.enqueue(value);
10021
+ await readChunk();
10022
+ }
10023
+ }
10024
+ }
10025
+ }));
10026
+ const entries = await zipReader.getEntries();
10027
+ await zipReader.close();
10028
+ await zipDataReader.pipeTo(this.writer.writable, { preventClose: true, preventAbort: true });
10029
+ this.writer.writable.size = this.offset = reader.size;
10030
+ this.filenames = new Set(entries.map(entry => entry.filename));
10031
+ this.files = new Map(entries.map(entry => {
10032
+ const {
10033
+ version,
10034
+ compressionMethod,
10035
+ lastModDate,
10036
+ lastAccessDate,
10037
+ creationDate,
10038
+ rawFilename,
10039
+ bitFlag,
10040
+ encrypted,
10041
+ uncompressedSize,
10042
+ compressedSize,
10043
+ diskOffset,
10044
+ diskNumber,
10045
+ zip64
10046
+ } = entry;
10047
+ let {
10048
+ rawExtraFieldZip64,
10049
+ rawExtraFieldAES,
10050
+ rawExtraFieldExtendedTimestamp,
10051
+ rawExtraFieldNTFS,
10052
+ rawExtraField,
10053
+ } = entry;
10054
+ const { level, languageEncodingFlag, dataDescriptor } = bitFlag;
10055
+ rawExtraFieldZip64 = rawExtraFieldZip64 || new Uint8Array();
10056
+ rawExtraFieldAES = rawExtraFieldAES || new Uint8Array();
10057
+ rawExtraFieldExtendedTimestamp = rawExtraFieldExtendedTimestamp || new Uint8Array();
10058
+ rawExtraFieldNTFS = rawExtraFieldNTFS || new Uint8Array();
10059
+ rawExtraField = rawExtraField || new Uint8Array();
10060
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10061
+ const zip64UncompressedSize = zip64 && uncompressedSize > MAX_32_BITS;
10062
+ const zip64CompressedSize = zip64 && compressedSize > MAX_32_BITS;
10063
+ const {
10064
+ headerArray,
10065
+ headerView
10066
+ } = getHeaderArrayData({
10067
+ version,
10068
+ bitflag: getBitflag(level, languageEncodingFlag, dataDescriptor, encrypted),
10069
+ compressionMethod,
10070
+ uncompressedSize,
10071
+ compressedSize,
10072
+ lastModDate,
10073
+ rawFilename,
10074
+ zip64CompressedSize,
10075
+ zip64UncompressedSize,
10076
+ extraFieldLength
10077
+ });
10078
+ Object.assign(entry, {
10079
+ zip64UncompressedSize,
10080
+ zip64CompressedSize,
10081
+ zip64Offset: zip64 && this.offset - diskOffset > MAX_32_BITS,
10082
+ zip64DiskNumberStart: zip64 && diskNumber > MAX_16_BITS,
10083
+ rawExtraFieldZip64,
10084
+ rawExtraFieldAES,
10085
+ rawExtraFieldExtendedTimestamp,
10086
+ rawExtraFieldNTFS,
10087
+ rawExtraField,
10088
+ extendedTimestamp: rawExtraFieldExtendedTimestamp.length > 0 || rawExtraFieldNTFS.length > 0,
10089
+ extraFieldExtendedTimestampFlag: 0x1 + (lastAccessDate ? 0x2 : 0) + (creationDate ? 0x4 : 0),
10090
+ headerArray,
10091
+ headerView
10092
+ });
10093
+ return [entry.filename, entry];
10094
+ }));
10095
+ }
10096
+
9999
10097
  async add(name = "", reader, options = {}) {
10000
10098
  const zipWriter = this;
10001
10099
  const {
@@ -10031,6 +10129,22 @@
10031
10129
  }
10032
10130
  }
10033
10131
 
10132
+ remove(entry) {
10133
+ const { filenames, files } = this;
10134
+ if (typeof entry == "string") {
10135
+ entry = files.get(entry);
10136
+ }
10137
+ if (entry && entry.filename !== UNDEFINED_VALUE) {
10138
+ const { filename } = entry;
10139
+ if (filenames.has(filename) && files.has(filename)) {
10140
+ filenames.delete(filename);
10141
+ files.delete(filename);
10142
+ return true;
10143
+ }
10144
+ }
10145
+ return false;
10146
+ }
10147
+
10034
10148
  async close(comment = new Uint8Array(), options = {}) {
10035
10149
  const zipWriter = this;
10036
10150
  const { pendingAddFileCalls, writer } = this;
@@ -10152,7 +10266,13 @@
10152
10266
  const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
10153
10267
  const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
10154
10268
  const compressionMethod = getOptionValue(zipWriter, options, "compressionMethod");
10155
- let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
10269
+ let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor");
10270
+ if (bufferedWrite && dataDescriptor === UNDEFINED_VALUE) {
10271
+ dataDescriptor = false;
10272
+ }
10273
+ if (dataDescriptor === UNDEFINED_VALUE || zipCrypto) {
10274
+ dataDescriptor = true;
10275
+ }
10156
10276
  let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
10157
10277
  if (!zipCrypto && (password !== UNDEFINED_VALUE || rawPassword !== UNDEFINED_VALUE) && !(encryptionStrength >= 1 && encryptionStrength <= 3)) {
10158
10278
  throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
@@ -10198,10 +10318,11 @@
10198
10318
  uncompressedSize = maximumCompressedSize = MAX_32_BITS + 1;
10199
10319
  }
10200
10320
  } else {
10201
- uncompressedSize = reader.size;
10321
+ options.uncompressedSize = uncompressedSize = reader.size;
10202
10322
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
10203
10323
  }
10204
10324
  } else {
10325
+ options.uncompressedSize = uncompressedSize;
10205
10326
  maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
10206
10327
  }
10207
10328
  }
@@ -10257,7 +10378,10 @@
10257
10378
  passThrough,
10258
10379
  encrypted: Boolean((password && getLength(password)) || (rawPassword && getLength(rawPassword))) || (passThrough && encrypted),
10259
10380
  signature,
10260
- compressionMethod
10381
+ compressionMethod,
10382
+ uncompressedSize,
10383
+ offset: zipWriter.offset - diskOffset,
10384
+ diskNumberStart: diskNumber,
10261
10385
  });
10262
10386
  const headerInfo = getHeaderInfo(options);
10263
10387
  const dataDescriptorInfo = getDataDescriptorInfo(options);
@@ -10349,20 +10473,26 @@
10349
10473
  await lockPreviousFileEntry;
10350
10474
  await requestLockWriter();
10351
10475
  writingBufferedEntryData = true;
10352
- if (!dataDescriptor) {
10353
- blob = await writeExtraHeaderInfo(fileEntry, blob, writable, options);
10354
- }
10355
- await skipDiskIfNeeded(writable);
10356
10476
  fileEntry.diskNumberStart = writer.diskNumber;
10357
10477
  diskOffset = writer.diskOffset;
10478
+ fileEntry.offset = zipWriter.offset - diskOffset;
10479
+ if (fileEntry.zip64) {
10480
+ setExtraFieldZip64Data(fileEntry);
10481
+ }
10482
+ if (fileEntry.zip64 || !dataDescriptor) {
10483
+ blob = await patchLocalHeader(fileEntry, blob, writable, options);
10484
+ }
10485
+ await skipDiskIfNeeded(writable);
10358
10486
  await blob.stream().pipeTo(writable, { preventClose: true, preventAbort: true, signal });
10359
10487
  writable.size += blob.size;
10360
10488
  writingBufferedEntryData = false;
10489
+ } else {
10490
+ fileEntry.offset = zipWriter.offset - diskOffset;
10491
+ if (fileEntry.zip64) {
10492
+ setExtraFieldZip64Data(fileEntry);
10493
+ }
10361
10494
  }
10362
- fileEntry.offset = zipWriter.offset - diskOffset;
10363
- if (fileEntry.zip64) {
10364
- setZip64ExtraInfo(fileEntry, options);
10365
- } else if (fileEntry.offset > MAX_32_BITS) {
10495
+ if (fileEntry.offset > MAX_32_BITS && !fileEntry.zip64) {
10366
10496
  throw new Error(ERR_UNSUPPORTED_FORMAT);
10367
10497
  }
10368
10498
  zipWriter.offset += fileEntry.size;
@@ -10429,12 +10559,15 @@
10429
10559
  const {
10430
10560
  localHeaderArray,
10431
10561
  headerArray,
10562
+ headerView,
10432
10563
  lastModDate,
10433
10564
  rawLastModDate,
10434
10565
  encrypted,
10435
10566
  compressed,
10436
10567
  version,
10437
10568
  compressionMethod,
10569
+ rawExtraFieldZip64,
10570
+ localExtraFieldZip64Length,
10438
10571
  rawExtraFieldExtendedTimestamp,
10439
10572
  extraFieldExtendedTimestampFlag,
10440
10573
  rawExtraFieldNTFS,
@@ -10483,6 +10616,8 @@
10483
10616
  rawFilename,
10484
10617
  commentUTF8: true,
10485
10618
  rawComment,
10619
+ rawExtraFieldZip64,
10620
+ localExtraFieldZip64Length,
10486
10621
  rawExtraFieldExtendedTimestamp,
10487
10622
  rawExtraFieldNTFS,
10488
10623
  rawExtraFieldAES,
@@ -10536,28 +10671,8 @@
10536
10671
  } else {
10537
10672
  await writeData(writable, localHeaderArray);
10538
10673
  }
10539
- let rawExtraFieldZip64;
10540
- if (zip64) {
10541
- let rawExtraFieldZip64Length = 4;
10542
- if (zip64UncompressedSize) {
10543
- rawExtraFieldZip64Length += 8;
10544
- }
10545
- if (zip64CompressedSize) {
10546
- rawExtraFieldZip64Length += 8;
10547
- }
10548
- if (zip64Offset) {
10549
- rawExtraFieldZip64Length += 8;
10550
- }
10551
- if (zip64DiskNumberStart) {
10552
- rawExtraFieldZip64Length += 4;
10553
- }
10554
- rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
10555
- } else {
10556
- rawExtraFieldZip64 = new Uint8Array();
10557
- }
10558
10674
  setEntryInfo({
10559
10675
  signature,
10560
- rawExtraFieldZip64,
10561
10676
  compressedSize,
10562
10677
  uncompressedSize,
10563
10678
  headerInfo,
@@ -10579,8 +10694,8 @@
10579
10694
  compressionMethod,
10580
10695
  version,
10581
10696
  headerArray,
10697
+ headerView,
10582
10698
  signature,
10583
- rawExtraFieldZip64,
10584
10699
  extraFieldExtendedTimestampFlag,
10585
10700
  zip64UncompressedSize,
10586
10701
  zip64CompressedSize,
@@ -10605,10 +10720,62 @@
10605
10720
  rawExtraField,
10606
10721
  encryptionStrength,
10607
10722
  extendedTimestamp,
10608
- encrypted
10723
+ passThrough,
10724
+ encrypted,
10725
+ zip64UncompressedSize,
10726
+ zip64CompressedSize,
10727
+ zip64Offset,
10728
+ zip64DiskNumberStart,
10729
+ uncompressedSize,
10730
+ offset,
10731
+ diskNumberStart
10609
10732
  } = options;
10610
10733
  let { version, compressionMethod } = options;
10611
10734
  const compressed = !directory && (level > 0 || (level === UNDEFINED_VALUE && compressionMethod !== 0));
10735
+ let rawExtraFieldZip64;
10736
+ const uncompressedFile = passThrough || !compressed;
10737
+ const zip64ExtraFieldComplete = zip64 && (options.bufferedWrite || ((!zip64UncompressedSize && !zip64CompressedSize) || uncompressedFile));
10738
+ if (zip64) {
10739
+ let rawExtraFieldZip64Length = 4;
10740
+ if (zip64UncompressedSize) {
10741
+ rawExtraFieldZip64Length += 8;
10742
+ }
10743
+ if (zip64CompressedSize) {
10744
+ rawExtraFieldZip64Length += 8;
10745
+ }
10746
+ if (zip64Offset) {
10747
+ rawExtraFieldZip64Length += 8;
10748
+ }
10749
+ if (zip64DiskNumberStart) {
10750
+ rawExtraFieldZip64Length += 4;
10751
+ }
10752
+ rawExtraFieldZip64 = new Uint8Array(rawExtraFieldZip64Length);
10753
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10754
+ setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
10755
+ setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
10756
+ if (zip64ExtraFieldComplete) {
10757
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10758
+ let rawExtraFieldZip64Offset = 4;
10759
+ if (zip64UncompressedSize) {
10760
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10761
+ rawExtraFieldZip64Offset += 8;
10762
+ }
10763
+ if (zip64CompressedSize && uncompressedFile) {
10764
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10765
+ rawExtraFieldZip64Offset += 8;
10766
+ }
10767
+ if (zip64Offset) {
10768
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
10769
+ rawExtraFieldZip64Offset += 8;
10770
+ }
10771
+ if (zip64DiskNumberStart) {
10772
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
10773
+ rawExtraFieldZip64Offset += 4;
10774
+ }
10775
+ }
10776
+ } else {
10777
+ rawExtraFieldZip64 = new Uint8Array();
10778
+ }
10612
10779
  let rawExtraFieldAES;
10613
10780
  if (encrypted && !zipCrypto) {
10614
10781
  rawExtraFieldAES = new Uint8Array(getLength(EXTRAFIELD_DATA_AES) + 2);
@@ -10657,69 +10824,52 @@
10657
10824
  } else {
10658
10825
  rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
10659
10826
  }
10660
- let bitFlag = 0;
10661
- if (useUnicodeFileNames) {
10662
- bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
10663
- }
10664
- if (dataDescriptor) {
10665
- bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
10666
- }
10667
10827
  if (compressionMethod === UNDEFINED_VALUE) {
10668
10828
  compressionMethod = compressed ? COMPRESSION_METHOD_DEFLATE : COMPRESSION_METHOD_STORE;
10669
10829
  }
10670
- if (compressionMethod == COMPRESSION_METHOD_DEFLATE) {
10671
- if (level >= 1 && level < 3) {
10672
- bitFlag = bitFlag | 0b110;
10673
- }
10674
- if (level >= 3 && level < 5) {
10675
- bitFlag = bitFlag | 0b01;
10676
- }
10677
- if (level === 9) {
10678
- bitFlag = bitFlag | 0b10;
10679
- }
10680
- }
10681
10830
  if (zip64) {
10682
10831
  version = version > VERSION_ZIP64 ? version : VERSION_ZIP64;
10683
10832
  }
10684
- if (encrypted) {
10685
- bitFlag = bitFlag | BITFLAG_ENCRYPTED;
10686
- if (!zipCrypto) {
10687
- version = version > VERSION_AES ? version : VERSION_AES;
10688
- rawExtraFieldAES[9] = compressionMethod;
10689
- compressionMethod = COMPRESSION_METHOD_AES;
10690
- }
10691
- }
10692
- const headerArray = new Uint8Array(26);
10693
- const headerView = getDataView(headerArray);
10694
- setUint16(headerView, 0, version);
10695
- setUint16(headerView, 2, bitFlag);
10696
- setUint16(headerView, 4, compressionMethod);
10697
- const dateArray = new Uint32Array(1);
10698
- const dateView = getDataView(dateArray);
10699
- let lastModDateMsDos;
10700
- if (lastModDate < MIN_DATE) {
10701
- lastModDateMsDos = MIN_DATE;
10702
- } else if (lastModDate > MAX_DATE) {
10703
- lastModDateMsDos = MAX_DATE;
10704
- } else {
10705
- lastModDateMsDos = lastModDate;
10833
+ if (encrypted && !zipCrypto) {
10834
+ version = version > VERSION_AES ? version : VERSION_AES;
10835
+ rawExtraFieldAES[9] = compressionMethod;
10836
+ compressionMethod = COMPRESSION_METHOD_AES;
10706
10837
  }
10707
- setUint16(dateView, 0, (((lastModDateMsDos.getHours() << 6) | lastModDateMsDos.getMinutes()) << 5) | lastModDateMsDos.getSeconds() / 2);
10708
- setUint16(dateView, 2, ((((lastModDateMsDos.getFullYear() - 1980) << 4) | (lastModDateMsDos.getMonth() + 1)) << 5) | lastModDateMsDos.getDate());
10709
- const rawLastModDate = dateArray[0];
10710
- setUint32(headerView, 6, rawLastModDate);
10711
- setUint16(headerView, 22, getLength(rawFilename));
10712
- const extraFieldLength = getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10713
- setUint16(headerView, 24, extraFieldLength);
10714
- const localHeaderArray = new Uint8Array(30 + getLength(rawFilename) + extraFieldLength);
10838
+ const localExtraFieldZip64Length = zip64ExtraFieldComplete ? getLength(rawExtraFieldZip64) : 0;
10839
+ const extraFieldLength = localExtraFieldZip64Length + getLength(rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10840
+ const {
10841
+ headerArray,
10842
+ headerView,
10843
+ rawLastModDate
10844
+ } = getHeaderArrayData({
10845
+ version,
10846
+ bitflag: getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted),
10847
+ compressionMethod,
10848
+ uncompressedSize,
10849
+ lastModDate: lastModDate < MIN_DATE ? MIN_DATE : lastModDate > MAX_DATE ? MAX_DATE : lastModDate,
10850
+ rawFilename,
10851
+ zip64CompressedSize,
10852
+ zip64UncompressedSize,
10853
+ extraFieldLength
10854
+ });
10855
+ let localHeaderOffset = HEADER_SIZE + 4;
10856
+ const localHeaderArray = new Uint8Array(localHeaderOffset + getLength(rawFilename) + extraFieldLength);
10715
10857
  const localHeaderView = getDataView(localHeaderArray);
10716
10858
  setUint32(localHeaderView, 0, LOCAL_FILE_HEADER_SIGNATURE);
10717
10859
  arraySet(localHeaderArray, headerArray, 4);
10718
- arraySet(localHeaderArray, rawFilename, 30);
10719
- arraySet(localHeaderArray, rawExtraFieldAES, 30 + getLength(rawFilename));
10720
- arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, 30 + getLength(rawFilename, rawExtraFieldAES));
10721
- arraySet(localHeaderArray, rawExtraFieldNTFS, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp));
10722
- arraySet(localHeaderArray, rawExtraField, 30 + getLength(rawFilename, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS));
10860
+ arraySet(localHeaderArray, rawFilename, localHeaderOffset);
10861
+ localHeaderOffset += getLength(rawFilename);
10862
+ if (zip64ExtraFieldComplete) {
10863
+ arraySet(localHeaderArray, rawExtraFieldZip64, localHeaderOffset);
10864
+ }
10865
+ localHeaderOffset += localExtraFieldZip64Length;
10866
+ arraySet(localHeaderArray, rawExtraFieldAES, localHeaderOffset);
10867
+ localHeaderOffset += getLength(rawExtraFieldAES);
10868
+ arraySet(localHeaderArray, rawExtraFieldExtendedTimestamp, localHeaderOffset);
10869
+ localHeaderOffset += getLength(rawExtraFieldExtendedTimestamp);
10870
+ arraySet(localHeaderArray, rawExtraFieldNTFS, localHeaderOffset);
10871
+ localHeaderOffset += getLength(rawExtraFieldNTFS);
10872
+ arraySet(localHeaderArray, rawExtraField, localHeaderOffset);
10723
10873
  return {
10724
10874
  localHeaderArray,
10725
10875
  headerArray,
@@ -10731,6 +10881,8 @@
10731
10881
  version,
10732
10882
  compressionMethod,
10733
10883
  extraFieldExtendedTimestampFlag,
10884
+ rawExtraFieldZip64,
10885
+ localExtraFieldZip64Length,
10734
10886
  rawExtraFieldExtendedTimestamp,
10735
10887
  rawExtraFieldNTFS,
10736
10888
  rawExtraFieldAES,
@@ -10785,7 +10937,6 @@
10785
10937
  function setEntryInfo(entryInfo, options) {
10786
10938
  const {
10787
10939
  signature,
10788
- rawExtraFieldZip64,
10789
10940
  compressedSize,
10790
10941
  uncompressedSize,
10791
10942
  headerInfo,
@@ -10801,8 +10952,6 @@
10801
10952
  } = dataDescriptorInfo;
10802
10953
  const {
10803
10954
  zip64,
10804
- zip64UncompressedSize,
10805
- zip64CompressedSize,
10806
10955
  zipCrypto,
10807
10956
  dataDescriptor
10808
10957
  } = options;
@@ -10813,19 +10962,6 @@
10813
10962
  }
10814
10963
  }
10815
10964
  if (zip64) {
10816
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
10817
- setUint16(rawExtraFieldZip64View, 0, EXTRAFIELD_TYPE_ZIP64);
10818
- setUint16(rawExtraFieldZip64View, 2, getLength(rawExtraFieldZip64) - 4);
10819
- let rawExtraFieldZip64Offset = 4;
10820
- if (zip64UncompressedSize) {
10821
- setUint32(headerView, 18, MAX_32_BITS);
10822
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
10823
- rawExtraFieldZip64Offset += 8;
10824
- }
10825
- if (zip64CompressedSize) {
10826
- setUint32(headerView, 14, MAX_32_BITS);
10827
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(compressedSize));
10828
- }
10829
10965
  if (dataDescriptor) {
10830
10966
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 4, BigInt(compressedSize));
10831
10967
  setBigUint64(dataDescriptorView, dataDescriptorOffset + 12, BigInt(uncompressedSize));
@@ -10840,44 +10976,82 @@
10840
10976
  }
10841
10977
  }
10842
10978
 
10843
- async function writeExtraHeaderInfo(fileEntry, entryData, writable, { zipCrypto }) {
10979
+ async function patchLocalHeader(fileEntry, entryData, writable, { zipCrypto, dataDescriptor }) {
10980
+ const {
10981
+ zip64,
10982
+ rawFilename,
10983
+ localExtraFieldZip64Length,
10984
+ signature,
10985
+ encrypted,
10986
+ zip64UncompressedSize,
10987
+ zip64CompressedSize,
10988
+ zip64Offset,
10989
+ zip64DiskNumberStart,
10990
+ uncompressedSize,
10991
+ compressedSize,
10992
+ offset,
10993
+ diskNumberStart
10994
+ } = fileEntry;
10844
10995
  let arrayBuffer;
10845
- arrayBuffer = await entryData.slice(0, 26).arrayBuffer();
10846
- if (arrayBuffer.byteLength != 26) {
10847
- arrayBuffer = arrayBuffer.slice(0, 26);
10996
+ let rawExtraFieldZip64Offset = HEADER_SIZE + 4 + getLength(rawFilename);
10997
+ const arrayBufferSize = zip64 && localExtraFieldZip64Length ? rawExtraFieldZip64Offset + localExtraFieldZip64Length : HEADER_SIZE;
10998
+ arrayBuffer = await entryData.slice(0, arrayBufferSize).arrayBuffer();
10999
+ if (arrayBuffer.byteLength != arrayBufferSize) {
11000
+ arrayBuffer = arrayBuffer.slice(0, arrayBufferSize);
10848
11001
  }
10849
11002
  const arrayBufferView = new DataView(arrayBuffer);
10850
- if (!fileEntry.encrypted || zipCrypto) {
10851
- setUint32(arrayBufferView, 14, fileEntry.signature);
11003
+ if (!dataDescriptor) {
11004
+ if (!encrypted || zipCrypto) {
11005
+ setUint32(arrayBufferView, 14, signature);
11006
+ }
11007
+ if (!zip64) {
11008
+ setUint32(arrayBufferView, 18, compressedSize);
11009
+ setUint32(arrayBufferView, 22, uncompressedSize);
11010
+ }
10852
11011
  }
10853
- if (fileEntry.zip64) {
11012
+ if (zip64) {
10854
11013
  setUint32(arrayBufferView, 18, MAX_32_BITS);
10855
11014
  setUint32(arrayBufferView, 22, MAX_32_BITS);
10856
- } else {
10857
- setUint32(arrayBufferView, 18, fileEntry.compressedSize);
10858
- setUint32(arrayBufferView, 22, fileEntry.uncompressedSize);
11015
+ if (localExtraFieldZip64Length) {
11016
+ rawExtraFieldZip64Offset += 4;
11017
+ if (zip64UncompressedSize) {
11018
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(uncompressedSize));
11019
+ rawExtraFieldZip64Offset += 8;
11020
+ }
11021
+ if (zip64CompressedSize) {
11022
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(compressedSize));
11023
+ rawExtraFieldZip64Offset += 8;
11024
+ }
11025
+ if (zip64Offset) {
11026
+ setBigUint64(arrayBufferView, rawExtraFieldZip64Offset, BigInt(offset));
11027
+ rawExtraFieldZip64Offset += 8;
11028
+ }
11029
+ if (zip64DiskNumberStart) {
11030
+ setUint32(arrayBufferView, rawExtraFieldZip64Offset, diskNumberStart);
11031
+ }
11032
+ }
10859
11033
  }
10860
11034
  await writeData(writable, new Uint8Array(arrayBuffer));
10861
11035
  return entryData.slice(arrayBuffer.byteLength);
10862
11036
  }
10863
11037
 
10864
- function setZip64ExtraInfo(fileEntry, options) {
10865
- const { rawExtraFieldZip64, offset, diskNumberStart } = fileEntry;
10866
- const { zip64UncompressedSize, zip64CompressedSize, zip64Offset, zip64DiskNumberStart } = options;
10867
- const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
11038
+ function setExtraFieldZip64Data(fileEntry) {
11039
+ const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
10868
11040
  let rawExtraFieldZip64Offset = 4;
10869
- if (zip64UncompressedSize) {
11041
+ if (fileEntry.zip64UncompressedSize) {
11042
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.uncompressedSize));
10870
11043
  rawExtraFieldZip64Offset += 8;
10871
11044
  }
10872
- if (zip64CompressedSize) {
11045
+ if (fileEntry.zip64CompressedSize) {
11046
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.compressedSize));
10873
11047
  rawExtraFieldZip64Offset += 8;
10874
11048
  }
10875
- if (zip64Offset) {
10876
- setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(offset));
11049
+ if (fileEntry.zip64Offset) {
11050
+ setBigUint64(rawExtraFieldZip64View, rawExtraFieldZip64Offset, BigInt(fileEntry.offset));
10877
11051
  rawExtraFieldZip64Offset += 8;
10878
11052
  }
10879
- if (zip64DiskNumberStart) {
10880
- setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, diskNumberStart);
11053
+ if (fileEntry.zip64DiskNumberStart) {
11054
+ setUint32(rawExtraFieldZip64View, rawExtraFieldZip64Offset, fileEntry.diskNumberStart);
10881
11055
  }
10882
11056
  }
10883
11057
 
@@ -10912,7 +11086,7 @@
10912
11086
  } else {
10913
11087
  rawExtraFieldTimestamp = new Uint8Array();
10914
11088
  }
10915
- fileEntry.rawExtraFieldCDExtendedTimestamp = rawExtraFieldTimestamp;
11089
+ fileEntry.rawExtraFieldExtendedTimestamp = rawExtraFieldTimestamp;
10916
11090
  directoryDataLength += 46 +
10917
11091
  getLength(
10918
11092
  rawFilename,
@@ -10933,12 +11107,13 @@
10933
11107
  rawFilename,
10934
11108
  rawExtraFieldZip64,
10935
11109
  rawExtraFieldAES,
10936
- rawExtraFieldCDExtendedTimestamp,
11110
+ rawExtraFieldExtendedTimestamp,
10937
11111
  rawExtraFieldNTFS,
10938
11112
  rawExtraField,
10939
11113
  rawComment,
10940
11114
  versionMadeBy,
10941
11115
  headerArray,
11116
+ headerView,
10942
11117
  zip64,
10943
11118
  zip64UncompressedSize,
10944
11119
  zip64CompressedSize,
@@ -10950,10 +11125,9 @@
10950
11125
  uncompressedSize,
10951
11126
  compressedSize
10952
11127
  } = fileEntry;
10953
- const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
11128
+ const extraFieldLength = getLength(rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldExtendedTimestamp, rawExtraFieldNTFS, rawExtraField);
10954
11129
  setUint32(directoryView, offset, CENTRAL_FILE_HEADER_SIGNATURE);
10955
11130
  setUint16(directoryView, offset + 4, versionMadeBy);
10956
- const headerView = getDataView(headerArray);
10957
11131
  if (!zip64UncompressedSize) {
10958
11132
  setUint32(headerView, 18, uncompressedSize);
10959
11133
  }
@@ -10961,28 +11135,40 @@
10961
11135
  setUint32(headerView, 14, compressedSize);
10962
11136
  }
10963
11137
  arraySet(directoryArray, headerArray, offset + 6);
10964
- setUint16(directoryView, offset + 30, extraFieldLength);
10965
- setUint16(directoryView, offset + 32, getLength(rawComment));
10966
- setUint16(directoryView, offset + 34, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
10967
- setUint16(directoryView, offset + 36, internalFileAttributes);
11138
+ let directoryOffset = offset + HEADER_SIZE + 4;
11139
+ setUint16(directoryView, directoryOffset, extraFieldLength);
11140
+ directoryOffset += 2;
11141
+ setUint16(directoryView, directoryOffset, getLength(rawComment));
11142
+ directoryOffset += 2;
11143
+ setUint16(directoryView, directoryOffset, zip64 && zip64DiskNumberStart ? MAX_16_BITS : diskNumberStart);
11144
+ directoryOffset += 2;
11145
+ setUint16(directoryView, directoryOffset, internalFileAttributes);
11146
+ directoryOffset += 2;
10968
11147
  if (externalFileAttributes) {
10969
- setUint32(directoryView, offset + 38, externalFileAttributes);
10970
- }
10971
- setUint32(directoryView, offset + 42, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
10972
- arraySet(directoryArray, rawFilename, offset + 46);
10973
- arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + getLength(rawFilename));
10974
- arraySet(directoryArray, rawExtraFieldAES, offset + 46 + getLength(rawFilename, rawExtraFieldZip64));
10975
- arraySet(directoryArray, rawExtraFieldCDExtendedTimestamp, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES));
10976
- arraySet(directoryArray, rawExtraFieldNTFS, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp));
10977
- arraySet(directoryArray, rawExtraField, offset + 46 + getLength(rawFilename, rawExtraFieldZip64, rawExtraFieldAES, rawExtraFieldCDExtendedTimestamp, rawExtraFieldNTFS));
10978
- arraySet(directoryArray, rawComment, offset + 46 + getLength(rawFilename) + extraFieldLength);
10979
- const directoryEntryLength = 46 + getLength(rawFilename, rawComment) + extraFieldLength;
11148
+ setUint32(directoryView, directoryOffset, externalFileAttributes);
11149
+ }
11150
+ directoryOffset += 4;
11151
+ setUint32(directoryView, directoryOffset, zip64 && zip64Offset ? MAX_32_BITS : fileEntryOffset);
11152
+ directoryOffset += 4;
11153
+ arraySet(directoryArray, rawFilename, directoryOffset);
11154
+ directoryOffset += getLength(rawFilename);
11155
+ arraySet(directoryArray, rawExtraFieldZip64, directoryOffset);
11156
+ directoryOffset += getLength(rawExtraFieldZip64);
11157
+ arraySet(directoryArray, rawExtraFieldAES, directoryOffset);
11158
+ directoryOffset += getLength(rawExtraFieldAES);
11159
+ arraySet(directoryArray, rawExtraFieldExtendedTimestamp, directoryOffset);
11160
+ directoryOffset += getLength(rawExtraFieldExtendedTimestamp);
11161
+ arraySet(directoryArray, rawExtraFieldNTFS, directoryOffset);
11162
+ directoryOffset += getLength(rawExtraFieldNTFS);
11163
+ arraySet(directoryArray, rawExtraField, directoryOffset);
11164
+ directoryOffset += getLength(rawExtraField);
11165
+ arraySet(directoryArray, rawComment, directoryOffset);
10980
11166
  if (offset - directoryDiskOffset > writer.availableSize) {
10981
11167
  writer.availableSize = 0;
10982
11168
  await writeData(writable, directoryArray.slice(directoryDiskOffset, offset));
10983
11169
  directoryDiskOffset = offset;
10984
11170
  }
10985
- offset += directoryEntryLength;
11171
+ offset = directoryOffset;
10986
11172
  if (options.onprogress) {
10987
11173
  try {
10988
11174
  await options.onprogress(indexFileEntry + 1, files.size, new Entry(fileEntry));
@@ -11110,6 +11296,67 @@
11110
11296
  return result;
11111
11297
  }
11112
11298
 
11299
+ function getHeaderArrayData({
11300
+ version,
11301
+ bitflag,
11302
+ compressionMethod,
11303
+ uncompressedSize,
11304
+ compressedSize,
11305
+ lastModDate,
11306
+ rawFilename,
11307
+ zip64CompressedSize,
11308
+ zip64UncompressedSize,
11309
+ extraFieldLength
11310
+ }) {
11311
+ const headerArray = new Uint8Array(HEADER_SIZE);
11312
+ const headerView = getDataView(headerArray);
11313
+ setUint16(headerView, 0, version);
11314
+ setUint16(headerView, 2, bitflag);
11315
+ setUint16(headerView, 4, compressionMethod);
11316
+ const dateArray = new Uint32Array(1);
11317
+ const dateView = getDataView(dateArray);
11318
+ setUint16(dateView, 0, (((lastModDate.getHours() << 6) | lastModDate.getMinutes()) << 5) | lastModDate.getSeconds() / 2);
11319
+ setUint16(dateView, 2, ((((lastModDate.getFullYear() - 1980) << 4) | (lastModDate.getMonth() + 1)) << 5) | lastModDate.getDate());
11320
+ const rawLastModDate = dateArray[0];
11321
+ setUint32(headerView, 6, rawLastModDate);
11322
+ if (zip64CompressedSize || compressedSize !== UNDEFINED_VALUE) {
11323
+ setUint32(headerView, 14, zip64CompressedSize ? MAX_32_BITS : compressedSize);
11324
+ }
11325
+ if (zip64UncompressedSize || uncompressedSize !== UNDEFINED_VALUE) {
11326
+ setUint32(headerView, 18, zip64UncompressedSize ? MAX_32_BITS : uncompressedSize);
11327
+ }
11328
+ setUint16(headerView, 22, getLength(rawFilename));
11329
+ setUint16(headerView, 24, extraFieldLength);
11330
+ return {
11331
+ headerArray,
11332
+ headerView,
11333
+ rawLastModDate
11334
+ };
11335
+ }
11336
+
11337
+ function getBitflag(level, useUnicodeFileNames, dataDescriptor, encrypted) {
11338
+ let bitFlag = 0;
11339
+ if (useUnicodeFileNames) {
11340
+ bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
11341
+ }
11342
+ if (dataDescriptor) {
11343
+ bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
11344
+ }
11345
+ if (level >= 1 && level < 3) {
11346
+ bitFlag = bitFlag | 0b110;
11347
+ }
11348
+ if (level >= 3 && level < 5) {
11349
+ bitFlag = bitFlag | 0b01;
11350
+ }
11351
+ if (level === 9) {
11352
+ bitFlag = bitFlag | 0b10;
11353
+ }
11354
+ if (encrypted) {
11355
+ bitFlag = bitFlag | BITFLAG_ENCRYPTED;
11356
+ }
11357
+ return bitFlag;
11358
+ }
11359
+
11113
11360
  /*
11114
11361
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
11115
11362
 
@@ -11856,9 +12103,8 @@
11856
12103
  });
11857
12104
  }
11858
12105
  }
11859
- await zipWriter.add(name, child.reader, Object.assign({
11860
- directory: child.directory
11861
- }, Object.assign({}, options, zipEntryOptions, childOptions, {
12106
+ await zipWriter.add(name, child.reader, Object.assign({}, options, zipEntryOptions, childOptions, {
12107
+ directory: child.directory,
11862
12108
  onprogress: async indexProgress => {
11863
12109
  if (options.onprogress) {
11864
12110
  entryOffsets.set(name, indexProgress);
@@ -11870,7 +12116,7 @@
11870
12116
  }
11871
12117
  }
11872
12118
  }
11873
- })));
12119
+ }));
11874
12120
  await process(zipWriter, child);
11875
12121
  }
11876
12122
  }
@@ -12051,6 +12297,7 @@
12051
12297
  exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
12052
12298
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
12053
12299
  exports.ERR_WRITER_NOT_INITIALIZED = ERR_WRITER_NOT_INITIALIZED;
12300
+ exports.ERR_ZIP_NOT_EMPTY = ERR_ZIP_NOT_EMPTY;
12054
12301
  exports.HttpRangeReader = HttpRangeReader;
12055
12302
  exports.HttpReader = HttpReader;
12056
12303
  exports.Reader = Reader;