gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.38k stars 510 forks source link

Demo with node stream? #374

Closed lslzl3000 closed 1 year ago

lslzl3000 commented 1 year ago

It seems that all demos were working with browsers Any example for nodejs? especially for the fs.stream

gildas-lormeau commented 1 year ago

Indeed, the demos only rely on Web APIs. I would recommend to use the Readable.toWeb()/Writable.toWeb() in order to convert Node.js streams into Web streams. Here is an example of code below.

import { ZipWriter, TextReader } from "@zip.js/zip.js";
import { open } from "node:fs/promises";
import { Writable } from "node:stream";

const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
const FILENAME = "lorem.txt";

await test();

async function test() {
  const filehandle = await open("./test.zip", "w");
  const writeStream = filehandle.createWriteStream();
  const zipWriter = new ZipWriter(Writable.toWeb(writeStream));
  await zipWriter.add(FILENAME, new TextReader(TEXT_CONTENT));
  await zipWriter.close();
}

I'm moving this issue into the discussions tab to keep it visible to people.