@zip.js/zip.js 2.7.7 → 2.7.8

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/dist/zip-fs.js CHANGED
@@ -3525,10 +3525,13 @@
3525
3525
  }
3526
3526
  }
3527
3527
  const dataOffset = offset + 30 + localDirectory.filenameLength + localDirectory.extraFieldLength;
3528
+ const size = compressedSize;
3528
3529
  const readable = reader.readable;
3529
- readable.diskNumberStart = diskNumberStart;
3530
- readable.offset = dataOffset;
3531
- let size = readable.size = compressedSize;
3530
+ Object.assign(readable, {
3531
+ diskNumberStart,
3532
+ offset: dataOffset,
3533
+ size
3534
+ });
3532
3535
  const signal = getOptionValue$1(zipEntry, options, "signal");
3533
3536
  const checkPasswordOnly = getOptionValue$1(zipEntry, options, "checkPasswordOnly");
3534
3537
  if (checkPasswordOnly) {
@@ -5149,7 +5152,26 @@
5149
5152
  }
5150
5153
 
5151
5154
  addFileSystemEntry(fileSystemEntry, options = {}) {
5152
- return addFileSystemEntry(this, fileSystemEntry, options);
5155
+ return addFileSystemHandle(this, fileSystemEntry, options);
5156
+ }
5157
+
5158
+ addFileSystemHandle(handle, options = {}) {
5159
+ return addFileSystemHandle(this, handle, options);
5160
+ }
5161
+
5162
+ addFile(file, options = {}) {
5163
+ if (!options.lastModDate) {
5164
+ options.lastModDate = new Date(file.lastModified);
5165
+ }
5166
+ return addChild(this, file.name, {
5167
+ data: file,
5168
+ Reader: function () {
5169
+ const readable = file.stream();
5170
+ const size = file.size;
5171
+ return { readable, size };
5172
+ },
5173
+ options
5174
+ });
5153
5175
  }
5154
5176
 
5155
5177
  addData(name, params) {
@@ -5352,6 +5374,14 @@
5352
5374
  return this.root.addFileSystemEntry(fileSystemEntry, options);
5353
5375
  }
5354
5376
 
5377
+ addFileSystemHandle(handle, options) {
5378
+ return this.root.addFileSystemHandle(handle, options);
5379
+ }
5380
+
5381
+ addFile(file, options) {
5382
+ return this.root.addFile(file, options);
5383
+ }
5384
+
5355
5385
  addData(name, params) {
5356
5386
  return this.root.addData(name, params);
5357
5387
  }
@@ -5542,54 +5572,92 @@
5542
5572
  }
5543
5573
  }
5544
5574
 
5545
- async function addFileSystemEntry(zipEntry, fileSystemEntry, options) {
5546
- if (fileSystemEntry.isDirectory) {
5547
- const entry = zipEntry.addDirectory(fileSystemEntry.name, options);
5548
- await addDirectory(entry, fileSystemEntry);
5549
- return entry;
5550
- } else {
5551
- return new Promise((resolve, reject) => fileSystemEntry.file(file => resolve(zipEntry.addBlob(fileSystemEntry.name, file, options)), reject));
5552
- }
5575
+ async function addFileSystemHandle(zipEntry, handle, options) {
5576
+ await addFile(zipEntry, handle, []);
5553
5577
 
5554
- async function addDirectory(zipEntry, fileEntry) {
5555
- const children = await getChildren(fileEntry);
5556
- for (const child of children) {
5557
- if (child.isDirectory) {
5558
- await addDirectory(zipEntry.addDirectory(child.name, options), child);
5559
- } else {
5560
- await new Promise((resolve, reject) => {
5561
- child.file(file => {
5562
- const childZipEntry = zipEntry.addBlob(child.name, file, options);
5563
- childZipEntry.uncompressedSize = file.size;
5564
- resolve(childZipEntry);
5565
- }, reject);
5566
- });
5578
+ async function addFile(parentEntry, handle, addedEntries) {
5579
+ try {
5580
+ if (handle.isFile || handle.isDirectory) {
5581
+ handle = transformToFileSystemhandle(handle);
5582
+ }
5583
+ if (handle.kind == "file") {
5584
+ const file = await handle.getFile();
5585
+ addedEntries.push(
5586
+ parentEntry.addData(file.name, {
5587
+ Reader: function () {
5588
+ const readable = file.stream();
5589
+ const size = file.size;
5590
+ return { readable, size };
5591
+ },
5592
+ options: {
5593
+ lastModDate: new Date(file.lastModified)
5594
+ }
5595
+ }, options)
5596
+ );
5597
+ } else if (handle.kind == "directory") {
5598
+ const directoryEntry = parentEntry.addDirectory(handle.name);
5599
+ addedEntries.push(directoryEntry);
5600
+ for await (const childHandle of handle.values()) {
5601
+ await addFile(directoryEntry, childHandle, addedEntries);
5602
+ }
5567
5603
  }
5604
+ } catch (error) {
5605
+ const message = error.message + (handle ? " (" + handle.name + ")" : "");
5606
+ throw new Error(message);
5568
5607
  }
5608
+ return addedEntries;
5569
5609
  }
5610
+ }
5570
5611
 
5571
- function getChildren(fileEntry) {
5572
- return new Promise((resolve, reject) => {
5573
- let entries = [];
5574
- if (fileEntry.isDirectory) {
5575
- readEntries(fileEntry.createReader());
5576
- }
5577
- if (fileEntry.isFile) {
5578
- resolve(entries);
5579
- }
5612
+ async function transformToFileSystemhandle(entry) {
5613
+ const handle = {
5614
+ name: entry.name
5615
+ };
5616
+ if (entry.isFile) {
5617
+ handle.kind = "file";
5618
+ handle.getFile = () =>
5619
+ new Promise((resolve, reject) => entry.file(resolve, reject));
5620
+ }
5621
+ if (entry.isDirectory) {
5622
+ handle.kind = "directory";
5623
+ const handles = await transformToFileSystemhandles(entry);
5624
+ handle.values = () => handles;
5625
+ }
5626
+ return handle;
5627
+ }
5580
5628
 
5581
- function readEntries(directoryReader) {
5582
- directoryReader.readEntries(temporaryEntries => {
5583
- if (!temporaryEntries.length) {
5584
- resolve(entries);
5585
- } else {
5586
- entries = entries.concat(temporaryEntries);
5587
- readEntries(directoryReader);
5588
- }
5589
- }, reject);
5629
+ async function transformToFileSystemhandles(entry) {
5630
+ const entries = [];
5631
+ function readEntries(directoryReader, resolve, reject) {
5632
+ directoryReader.readEntries(async (entriesPart) => {
5633
+ if (!entriesPart.length) {
5634
+ resolve(entries);
5635
+ } else {
5636
+ for (const entry of entriesPart) {
5637
+ entries.push(await transformToFileSystemhandle(entry));
5638
+ }
5639
+ readEntries(directoryReader, resolve, reject);
5590
5640
  }
5591
- });
5641
+ }, reject);
5592
5642
  }
5643
+ await new Promise((resolve, reject) =>
5644
+ readEntries(entry.createReader(), resolve, reject)
5645
+ );
5646
+ return {
5647
+ [Symbol.iterator]() {
5648
+ let entryIndex = 0;
5649
+ return {
5650
+ next() {
5651
+ const result = {
5652
+ value: entries[entryIndex],
5653
+ done: entryIndex === entries.length
5654
+ };
5655
+ entryIndex++;
5656
+ return result;
5657
+ }
5658
+ };
5659
+ }
5660
+ };
5593
5661
  }
5594
5662
 
5595
5663
  function resetFS(fs) {