Replies: 4 comments
-
However, I am currently working on |
Beta Was this translation helpful? Give feedback.
-
Also a wrapper for zipping a directory with a few lines of codes would be useful for Node.js. Something like this: const zip = new fflate.ZipSync();
zip.add({path: "./dir"});
zip.write("./result.zip"); |
Beta Was this translation helpful? Give feedback.
-
No release? I just need to create a zip from files of a directory now. |
Beta Was this translation helpful? Give feedback.
-
Fortunately, I have the old code snippet. So, I have spent the time only to verify and modify it. Seems, this is fine: import fs from "node:fs";
import path from "node:path";
import * as fflate from "fflate";
import {listFiles} from "@alttiri/util-node-js";
class Zip {
buffers = [];
constructor() {
this.zip = new fflate.Zip((err, data, final) => {
err && console.error(err, data, final);
this.buffers.push(data);
});
}
async add({data, filename, mtime, level}) {
let zipDeflate;
if (level === undefined) {
zipDeflate = new fflate.ZipPassThrough(filename);
} else {
zipDeflate = new fflate.ZipDeflate(filename, {level});
}
zipDeflate.mtime = mtime;
if (typeof data === "string" || data instanceof String) {
data = new TextEncoder().encode(data);
} else
if (data instanceof Blob) {
data = await Zip.blobToUi8Array(data);
}
this.zip.add(zipDeflate);
zipDeflate.push(data, true);
}
/** @returns {Blob} */
blob() {
this.zip.end();
return new Blob(this.buffers);
}
/** @returns {Promise<Uint8Array>} */
ui8Array() {
return Zip.blobToUi8Array(this.blob());
}
/** @param {Blob} blob
* @return {Promise<Uint8Array>} */
static async blobToUi8Array(blob) {
const ab = await blob.arrayBuffer();
return new Uint8Array(ab);
}
}
export async function zipFolder(folderPath, resultZipPath) {
const zip = new Zip();
for await (const item of listFiles({filepath: folderPath})) {
const relFilepath = path.relative(folderPath, item.path);
if ("stats" in item) {
await zip.add({
filename: relFilepath,
mtime: item.stats.mtime,
data: fs.readFileSync(item.path),
// level: 9,
});
// console.log(relFilepath);
}
}
const wsZip = fs.createWriteStream(resultZipPath);
wsZip.write(await zip.ui8Array());
}
console.time("zip");
await zipFolder("./dist", "result.zip");
console.timeEnd("zip"); |
Beta Was this translation helpful? Give feedback.
-
JSZip has a short and clear example.
I think it would convenient if this lib supports this approach from the box:
Optionally, the same approach for async version:
Beta Was this translation helpful? Give feedback.
All reactions