@zip.js/zip.js 2.5.25 → 2.6.1

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
@@ -2,3 +2,65 @@ zip.js is an open-source library (BSD-3-Clause license) for zipping and unzippin
2
2
 
3
3
  See here for more info:
4
4
  https://gildas-lormeau.github.io/zip.js/
5
+
6
+ ```js
7
+ // Hello world with zip.js (and Streams)
8
+
9
+ import { ZipWriter, ZipReader, BlobReader } from "https://deno.land/x/zipjs/index.js";
10
+
11
+ // ----
12
+ // Write the zip file
13
+ // ----
14
+
15
+ // Creates a TransformStream object where the zip content will be written.
16
+ const zipStream = new TransformStream();
17
+ // Creates a Promise object resolved to the zip content returned as a Blob object.
18
+ const zipBlobPromise = new Response(zipStream.readable).blob();
19
+ // Creates a ReadableStream object containing the text of the entry to add in the zip.
20
+ const helloWorldReadable = new Blob(["Hello world!"], { type: "text/plain" }).stream();
21
+
22
+
23
+ // Creates a ZipWriter object writing data via zipStream, adds the file "hello.txt" containing
24
+ // the text "Hello world!" via helloWorldReadable in the zip, and closes the writer.
25
+ const zipWriter = new ZipWriter(zipStream);
26
+ await zipWriter.add("hello.txt", { readable: helloWorldReadable });
27
+ await zipWriter.close();
28
+
29
+
30
+ // Retrieves the Blob object containing the zip content.
31
+ const zipBlob = await zipBlobPromise;
32
+
33
+ // Uncomment the following code to download the zip file (in a browser)
34
+ //
35
+ // const anchorElement = document.createElement("a");
36
+ // anchorElement.href = URL.createObjectURL(zipBlob);
37
+ // anchorElement.download = "hello.zip";
38
+ // anchorElement.click();
39
+ //
40
+
41
+
42
+ // ----
43
+ // Read the zip file
44
+ // ----
45
+
46
+ // Creates a TransformStream object where the content of the first entry in the zip will be written.
47
+ const contentStream = new TransformStream();
48
+ // Creates a Promise object resolved to the first entry content returned as text.
49
+ const contentTextPromise = new Response(contentStream.readable).text();
50
+
51
+
52
+ // Reads zipBlob via a BlobReader object, retrieves metadata (name, dates, etc.) of the first entry,
53
+ // retrieves its content via contentStream, and closes the reader.
54
+ // Note: it is *not* possible to use a ReadableStream object to read a zip because random access
55
+ // to data is required to fetch entries reliably and efficiently.
56
+ const zipReader = new ZipReader(new BlobReader(zipBlob));
57
+ const firstEntry = (await zipReader.getEntries()).shift();
58
+ await firstEntry.getData(contentStream);
59
+ await zipReader.close();
60
+
61
+
62
+ // Displays "Hello world!".
63
+ const contentText = await contentTextPromise;
64
+ console.log(contentText);
65
+ ```
66
+ See here for more examples: https://github.com/gildas-lormeau/zip.js/tree/master/tests/all
@@ -3,7 +3,7 @@
3
3
  /* global Deno, fetch, URL, TextEncoderStream */
4
4
 
5
5
  import { parse as parseArgs } from "https://deno.land/std@0.147.0/flags/mod.ts";
6
- import { fromFileUrl, normalize as normalizePath, resolve as resolvePath, extname } from "https://deno.land/std@0.147.0/path/mod.ts";
6
+ import { normalize as normalizePath, resolve as resolvePath, extname } from "https://deno.land/std@0.147.0/path/mod.ts";
7
7
  import { configure, ZipWriter, terminateWorkers } from "../index.js";
8
8
 
9
9
  const args = parseArgs(Deno.args);
@@ -99,8 +99,6 @@ async function getFileInfo(file, options) {
99
99
  if (!extname(name)) {
100
100
  name += ".html";
101
101
  }
102
- } else if (file.startsWith("file:")) {
103
- name = fromFileUrl(file);
104
102
  } else {
105
103
  name = file;
106
104
  resolvedName = resolvePath(name);
@@ -7017,7 +7017,6 @@
7017
7017
  const MESSAGE_PULL = "pull";
7018
7018
  const MESSAGE_DATA = "data";
7019
7019
  const MESSAGE_CLOSE = "close";
7020
- const ERR_ABORT = "AbortError";
7021
7020
 
7022
7021
  class Codec {
7023
7022
 
@@ -7285,7 +7284,7 @@
7285
7284
  let indexWorker = 0;
7286
7285
 
7287
7286
  async function runWorker(stream, workerOptions) {
7288
- const { options, config, streamOptions } = workerOptions;
7287
+ const { options, config } = workerOptions;
7289
7288
  const streamCopy = !options.compressed && !options.signed && !options.encrypted;
7290
7289
  const { useWebWorkers, useCompressionStream, codecType } = options;
7291
7290
  const { workerScripts } = config;
@@ -7307,16 +7306,7 @@
7307
7306
  worker = await new Promise(resolve => pendingRequests.push({ resolve, stream, workerOptions }));
7308
7307
  }
7309
7308
  }
7310
- try {
7311
- return await worker.run();
7312
- } catch (error) {
7313
- const { signal } = streamOptions;
7314
- if (signal && signal.aborted && (signal.reason == error || error.name == ERR_ABORT)) {
7315
- throw new Error(ERR_ABORT);
7316
- } else {
7317
- throw error;
7318
- }
7319
- }
7309
+ return worker.run();
7320
7310
 
7321
7311
  function onTaskFinished(workerData) {
7322
7312
  if (pendingRequests.length) {
@@ -7459,11 +7449,10 @@
7459
7449
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7460
7450
  */
7461
7451
 
7462
- /* global Blob, atob, btoa, XMLHttpRequest, URL, fetch, ReadableStream, WritableStream */
7452
+ /* global Blob, atob, btoa, XMLHttpRequest, URL, fetch, ReadableStream, WritableStream, FileReader */
7463
7453
 
7464
7454
  const ERR_HTTP_STATUS = "HTTP error ";
7465
7455
  const ERR_HTTP_RANGE = "HTTP Range not supported";
7466
- const ERR_NOT_SEEKABLE_READER = "Reader is not seekable";
7467
7456
 
7468
7457
  const CONTENT_TYPE_TEXT_PLAIN = "text/plain";
7469
7458
  const HTTP_HEADER_CONTENT_LENGTH = "Content-Length";
@@ -7569,7 +7558,16 @@
7569
7558
 
7570
7559
  getData() {
7571
7560
  const writer = this;
7572
- return writer.blob.text();
7561
+ if (writer.blob.text && (writer.encoding === undefined || (writer.encoding && writer.encoding.toLowerCase() == "utf-8"))) {
7562
+ return writer.blob.text();
7563
+ } else {
7564
+ const reader = new FileReader();
7565
+ return new Promise((resolve, reject) => {
7566
+ reader.onload = event => resolve(event.target.result);
7567
+ reader.onerror = () => reject(reader.error);
7568
+ reader.readAsText(writer.blob, writer.encoding);
7569
+ });
7570
+ }
7573
7571
  }
7574
7572
  }
7575
7573
 
@@ -7689,32 +7687,6 @@
7689
7687
  blobWriter.arrayBuffers = [];
7690
7688
  }
7691
7689
 
7692
- class ReadableStreamReader extends Stream {
7693
-
7694
- constructor(readable) {
7695
- super();
7696
- if (!readable) {
7697
- this.readable = new ReadableStream({});
7698
- }
7699
- }
7700
- }
7701
-
7702
- class WritableStreamWriter extends Writer {
7703
-
7704
- constructor(writable, { preventClose } = {}) {
7705
- super();
7706
- this.preventClose = preventClose;
7707
- if (!writable) {
7708
- writable = new WritableStream({});
7709
- }
7710
- Object.defineProperty(this, "writable", {
7711
- get() {
7712
- return writable;
7713
- }
7714
- });
7715
- }
7716
- }
7717
-
7718
7690
  class FetchReader extends Reader {
7719
7691
 
7720
7692
  constructor(url, options) {
@@ -10087,7 +10059,6 @@
10087
10059
  exports.BlobWriter = BlobWriter;
10088
10060
  exports.Data64URIReader = Data64URIReader;
10089
10061
  exports.Data64URIWriter = Data64URIWriter;
10090
- exports.ERR_ABORT = ERR_ABORT;
10091
10062
  exports.ERR_BAD_FORMAT = ERR_BAD_FORMAT;
10092
10063
  exports.ERR_CENTRAL_DIRECTORY_NOT_FOUND = ERR_CENTRAL_DIRECTORY_NOT_FOUND;
10093
10064
  exports.ERR_DUPLICATED_NAME = ERR_DUPLICATED_NAME;
@@ -10107,19 +10078,16 @@
10107
10078
  exports.ERR_INVALID_SIGNATURE = ERR_INVALID_SIGNATURE;
10108
10079
  exports.ERR_INVALID_VERSION = ERR_INVALID_VERSION;
10109
10080
  exports.ERR_LOCAL_FILE_HEADER_NOT_FOUND = ERR_LOCAL_FILE_HEADER_NOT_FOUND;
10110
- exports.ERR_NOT_SEEKABLE_READER = ERR_NOT_SEEKABLE_READER;
10111
10081
  exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
10112
10082
  exports.ERR_UNSUPPORTED_ENCRYPTION = ERR_UNSUPPORTED_ENCRYPTION;
10113
10083
  exports.ERR_UNSUPPORTED_FORMAT = ERR_UNSUPPORTED_FORMAT;
10114
10084
  exports.HttpRangeReader = HttpRangeReader;
10115
10085
  exports.HttpReader = HttpReader;
10116
- exports.ReadableStreamReader = ReadableStreamReader;
10117
10086
  exports.Reader = Reader;
10118
10087
  exports.TextReader = TextReader;
10119
10088
  exports.TextWriter = TextWriter;
10120
10089
  exports.Uint8ArrayReader = Uint8ArrayReader;
10121
10090
  exports.Uint8ArrayWriter = Uint8ArrayWriter;
10122
- exports.WritableStreamWriter = WritableStreamWriter;
10123
10091
  exports.Writer = Writer;
10124
10092
  exports.ZipReader = ZipReader;
10125
10093
  exports.ZipWriter = ZipWriter;