-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const fs = require("fs");
const archiver = require("archiver");
function zipDirectory(sourceDir, outPath) {
const archive = archiver("zip", { zlib: { level: 9 } });
const stream = fs.createWriteStream(outPath);
return new Promise((resolve, reject) => {
archive
.directory(sourceDir, false)
.on("error", (err) => reject(err))
.pipe(stream);
stream.on("close", () => resolve());
archive.finalize();
});
}
function zipFiles(sourceFileList, outPath) {
const archive = archiver("zip", { zlib: { level: 9 } });
const stream = fs.createWriteStream(outPath);
archive.pipe(stream);
return new Promise((resolve, reject) => {
for (let i of sourceFileList) {
let a = i.split("/");
let filename = a[a.length - 1];
console.log(filename);
archive.file(i, { name: filename }).on("error", (err) => reject(err));
}
stream.on("close", () => resolve());
archive.finalize();
});
}
//zipDirectory("./test", "test2.zip");
zipFiles(
[
"/home/akhil/projects/JS-ZIP-Template/test/WaterBrush-Regular.ttf",
"/home/akhil/projects/JS-ZIP-Template/Untitled.png",
],
"awa.zip"
);