-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipcli.js
More file actions
executable file
·111 lines (100 loc) · 3.25 KB
/
Copy pathzipcli.js
File metadata and controls
executable file
·111 lines (100 loc) · 3.25 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-import=code4fukui.github.io,deno.land
import {
basename,
relative,
resolve,
SEPARATOR,
} from "https://deno.land/std@0.224.0/path/mod.ts";
import { Zip } from "https://code4fukui.github.io/Zip/Zip.js";
const usage =
`Usage: zipcli.js [-o output.zip] [-p password] <file|directory>...
Options:
-o <file> Output ZIP file
-p <password> Encrypt files using ZipCrypto
-h, --help Show this help`;
export const parseArgs = (args) => {
const options = { inputs: [] };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg == "-h" || arg == "--help") {
options.help = true;
} else if (arg == "-o" || arg == "-p") {
const value = args[++i];
if (value == null || value == "") {
throw new Error(`${arg} requires a value`);
}
if (arg == "-o") options.output = value;
else options.password = value;
} else if (arg.startsWith("-")) {
throw new Error(`unknown option: ${arg}`);
} else {
options.inputs.push(arg);
}
}
return options;
};
const archiveName = (path) => path.split(SEPARATOR).join("/");
const collectDirectory = async (root, path, files, excludedPath) => {
const entries = [];
for await (const entry of Deno.readDir(path)) entries.push(entry);
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
const fullPath = resolve(path, entry.name);
if (fullPath == excludedPath) continue;
if (entry.isDirectory) {
await collectDirectory(root, fullPath, files, excludedPath);
} else if (entry.isFile) {
files.push({
name: archiveName(`${basename(root)}/${relative(root, fullPath)}`),
data: await Deno.readFile(fullPath),
date: (await Deno.stat(fullPath)).mtime,
});
}
}
};
export const createZip = async ({ inputs, output, password }) => {
if (inputs.length == 0) throw new Error("no input files");
const outputPath = resolve(
output ??
(inputs.length == 1
? `${basename(resolve(inputs[0]))}.zip`
: "archive.zip"),
);
const files = [];
for (const input of inputs) {
const inputPath = resolve(input);
const info = await Deno.stat(inputPath);
if (info.isDirectory) {
await collectDirectory(inputPath, inputPath, files, outputPath);
} else if (info.isFile) {
if (inputPath == outputPath) {
throw new Error("output file cannot also be an input");
}
files.push({
name: basename(inputPath),
data: await Deno.readFile(inputPath),
date: info.mtime,
});
} else {
throw new Error(`unsupported input: ${input}`);
}
}
if (files.length == 0) throw new Error("no files to compress");
await Deno.writeFile(outputPath, Zip.compress(files, { password }));
return { outputPath, count: files.length };
};
export const main = async (args = Deno.args) => {
const options = parseArgs(args);
if (options.help) {
console.log(usage);
return;
}
const result = await createZip(options);
console.log(`Created ${result.outputPath} (${result.count} files)`);
};
if (import.meta.main) {
main().catch((error) => {
console.error(`zipcli.js: ${error.message}`);
console.error(usage);
Deno.exitCode = 1;
});
}