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 zipFileWriter = new TransformStream();
// Creates a Promise object resolved to the zip content returned as a Blob
// object via the `readable` property of `zipFileWriter`.
const zipFileBlobPromise = new Response(zipFileWriter.readable).blob();
// Creates a Reader object containing a `readable` property. This property is
// set to a ReadableStream object storing the text of the entry to add in the
// zip (i.e. "Hello world!").
const helloWorldReader = {
readable: new Blob(["Hello world!"]).stream()
};
// Creates a ZipWriter object writing data via `zipFileWriter`, adds the entry
// "hello.txt" containing the text "Hello world!" via `helloWorldReader`, and
// closes the writer.
const zipWriter = new ZipWriter(zipFileWriter);
await zipWriter.add("hello.txt", helloWorldReader);
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 helloWorldWriter = new TransformStream();
// Creates a Promise object resolved to the content of the first entry returned
// as text...