zip.js

A library to zip and unzip files in the browser and Deno supporting multi-c...

README

Introduction


zip.js is a JavaScript open-source library (BSD-3-Clause license) for
compressing and decompressing zip files. It has been designed to handle large amounts
of data. It supports notably multi-core compression, native compression with
compression streams, files larger than 4GB with Zip64, split zip files and data
encryption.

Documentation


See here for more info: https://gildas-lormeau.github.io/zip.js/

Examples


Hello world


  1. ``` js
  2. import {
  3.   BlobReader,
  4.   BlobWriter,
  5.   TextReader,
  6.   TextWriter,
  7.   ZipReader,
  8.   ZipWriter,
  9. } from "https://deno.land/x/zipjs/index.js";

  10. // ----
  11. // Write the zip file
  12. // ----

  13. // Creates a BlobWriter object where the zip content will be written.
  14. const zipFileWriter = new BlobWriter();
  15. // Creates a TextReader object storing the text of the entry to add in the zip
  16. // (i.e. "Hello world!").
  17. const helloWorldReader = new TextReader("Hello world!");

  18. // Creates a ZipWriter object writing data via `zipFileWriter`, adds the entry
  19. // "hello.txt" containing the text "Hello world!" via `helloWorldReader`, and
  20. // closes the writer.
  21. const zipWriter = new ZipWriter(zipFileWriter);
  22. await zipWriter.add("hello.txt", helloWorldReader);
  23. await zipWriter.close();

  24. // Retrieves the Blob object containing the zip content into `zipFileBlob`. It
  25. // is also returned by zipWriter.close() for more convenience.
  26. const zipFileBlob = await zipFileWriter.getData();

  27. // ----
  28. // Read the zip file
  29. // ----

  30. // Creates a BlobReader object used to read `zipFileBlob`.
  31. const zipFileReader = new BlobReader(zipFileBlob);
  32. // Creates a TextWriter object where the content of the first entry in the zip
  33. // will be written.
  34. const helloWorldWriter = new TextWriter();

  35. // Creates a ZipReader object reading the zip content via `zipFileReader`,
  36. // retrieves metadata (name, dates, etc.) of the first entry, retrieves its
  37. // content via `helloWorldWriter`, and closes the reader.
  38. const zipReader = new ZipReader(zipFileReader);
  39. const firstEntry = (await zipReader.getEntries()).shift();
  40. const helloWorldText = await firstEntry.getData(helloWorldWriter);
  41. await zipReader.close();

  42. // Displays "Hello world!".
  43. console.log(helloWorldText);
  44. ```

Run the code on JSFiddle: https://jsfiddle.net/dns7pkxt/

Hello world with Streams


  1. ``` js
  2. import {
  3.   BlobReader,
  4.   ZipReader,
  5.   ZipWriter,
  6. } from "https://deno.land/x/zipjs/index.js";

  7. // ----
  8. // Write the zip file
  9. // ----

  10. // Creates a TransformStream object, the zip content will be written in the
  11. // `writable` property.
  12. const zipFileStream = new TransformStream();
  13. // Creates a Promise object resolved to the zip content returned as a Blob
  14. // object retrieved from `zipFileStream.readable`.
  15. const zipFileBlobPromise = new Response(zipFileStream.readable).blob();
  16. // Creates a ReadableStream object storing the text of the entry to add in the
  17. // zip (i.e. "Hello world!").
  18. const helloWorldReadable = new Blob(["Hello world!"]).stream();

  19. // Creates a ZipWriter object writing data into `zipFileStream.writable`, adds
  20. // the entry "hello.txt" containing the text "Hello world!" retrieved from
  21. // `helloWorldReadable`, and closes the writer.
  22. const zipWriter = new ZipWriter(zipFileStream.writable);
  23. await zipWriter.add("hello.txt", helloWorldReadable);
  24. await zipWriter.close();

  25. // Retrieves the Blob object containing the zip content into `zipFileBlob`.
  26. const zipFileBlob = await zipFileBlobPromise;

  27. // ----
  28. // Read the zip file
  29. // ----

  30. // Creates a BlobReader object used to read `zipFileBlob`.
  31. const zipFileReader = new BlobReader(zipFileBlob);
  32. // Creates a TransformStream object, the content of the first entry in the zip
  33. // will be written in the `writable` property.
  34. const helloWorldStream = new TransformStream();
  35. // Creates a Promise object resolved to the content of the first entry returned
  36. // as text from `helloWorldStream.readable`.
  37. const helloWorldTextPromise = new Response(helloWorldStream.readable).text();

  38. // Creates a ZipReader object reading the zip content via `zipFileReader`,
  39. // retrieves metadata (name, dates, etc.) of the first entry, retrieves its
  40. // content into `helloWorldStream.writable`, and closes the reader.
  41. const zipReader = new ZipReader(zipFileReader);
  42. const firstEntry = (await zipReader.getEntries()).shift();
  43. await firstEntry.getData(helloWorldStream.writable);
  44. await zipReader.close();

  45. // Displays "Hello world!".
  46. const helloWorldText = await helloWorldTextPromise;
  47. console.log(helloWorldText);
  48. ```

Run the code on JSFiddle: https://jsfiddle.net/exnyq1ft/

Adding concurrently multiple entries in a zip file


  1. ``` js
  2. import {
  3.   BlobWriter,
  4.   HttpReader,
  5.   TextReader,
  6.   ZipWriter,
  7. } from "https://unpkg.com/@zip.js/zip.js/index.js";

  8. const README_URL = "https://unpkg.com/@zip.js/zip.js/README.md";
  9. getZipFileBlob()
  10.   .then(downloadFile);

  11. async function getZipFileBlob() {
  12.   const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
  13.   await Promise.all([
  14.     zipWriter.add("hello.txt", new TextReader("Hello world!")),
  15.     zipWriter.add("README.md", new HttpReader(README_URL)),
  16.   ]);
  17.   return zipWriter.close();
  18. }

  19. function downloadFile(blob) {
  20.   document.body.appendChild(Object.assign(document.createElement("a"), {
  21.     download: "hello.zip",
  22.     href: URL.createObjectURL(blob),
  23.     textContent: "Download zip file",
  24.   }));
  25. }
  26. ```

Run the code on Plunker: https://plnkr.co/edit/4sVljNIpqSUE9HCA?preview

See here for more examples:
https://github.com/gildas-lormeau/zip.js/tree/master/tests/all