JSFiddle - React, Tailwind, and code Playground
JavaScript
import * as zip from "https://deno.land/x/zipjs/index.js";
const {
configure,
BlobReader,
ZipReader,
ZipWriter,
} = zip;
configure({
useWebWorkers: false
});
getZipFileBlob()
.then(getFirstEntryText)
.then(helloWorldText => console.log(helloWorldText));
// ----
// Writes a zip file which contains a text file
// ----
async function getZipFileBlob() {
// Creates a TransformStream object, the zip content will be written in the
// `writable` property.
const zipFileStream = new TransformStream();
// Creates a Promise object resolved to the zip content returned as a Blob
// object retrieved from `zipFileStream.readable`.
const zipFileBlobPromise = new Response(zipFileStream.readable).blob();
// Creates a ReadableStream object storing the text of the entry to add in the
// zip (i.e. "Hello world!").
const helloWorldReadable = new Blob(["Hello world!"]).stream();
// Creates a ZipWriter object writing data into `zipFileStream.writable`, adds
// the entry "hello.txt" containing the text "Hello world!" retrieved from
// `helloWorldReadable`, and closes the writer.
const zipWriter = new ZipWriter(zipFileStream.writable);
await zipWriter.add("hello.txt", helloWorldReadable);
await zipWriter.close();
// Retrieves the Blob object containing the zip content into `zipFileBlob`.
const zipFileBlob = await zipFileBlobPromise;
return zipFileBlob;
}
// ----
// Returns the content as text of the first entry in the zip file
// ----
async function getFirstEntryText(zipFileBlob) {
// Creates a BlobReader object used to read `zipFileBlob`.
const zipFileReader = new BlobReader(zipFileBlob);
// Creates a TransformStream object, the content of the first entry in the zip
// will be written in the `writable` property.
const helloWorldStream = new TransformStream();
// Creates a Promise object resolved to the content of the first entry returned
// as text from `helloWorldStream.readable`.
const helloWorldTextPromise = new...