deno.land / x / zipjs@v2.7.43 / cli-deno / mc-zip.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env -S deno run --allow-write --allow-read --allow-net
/* global Deno, URL, TextEncoderStream */
import { parse as parseArgs } from "https://deno.land/std@0.147.0/flags/mod.ts";import { normalize as normalizePath, resolve as resolvePath, extname } from "https://deno.land/std@0.147.0/path/mod.ts";import { configure, ZipWriter, terminateWorkers, HttpReader } from "../index.js";
const args = parseArgs(Deno.args);const stdout = getTextWriter(Deno.stdout);main();
async function main() { const zipfile = args._.shift(); const list = Array.from(args._); if (!zipfile || !list.length) { await displayUsage(); } else { await runCommand(zipfile, list, getOptions()); }}
async function displayUsage() { await stdout.write("Copyright (c) 2022 Gildas Lormeau. All rights reserved.\n"); await stdout.write("mc-zip 1.0. Usage:\n"); await stdout.write("mc-zip [options] zipfile list\n"); await stdout.write(" The default action is to create or overwrite zipfile entries from list of file names and URLs. Options:\n"); await stdout.write(" --include-directories include directories in the zip file (default: true)\n"); await stdout.write(" --include-sub-directories include sub-directories in the zip file (default: true)\n"); await stdout.write(" --max-workers number of workers (default: number of logical cores)\n"); await stdout.write(" --buffered-write compress entries in parallel with workers (default: true)\n"); await stdout.write(" --password password (default: empty string)\n"); await stdout.write(" --encryption-strength encryption strength when using AES between 1 and 3 (default: 3)\n"); await stdout.write(" --zip-crypto use ZipCrypto instead of AES (default: false)\n"); await stdout.write(" --data-descriptor add data descriptors (default: true)\n"); await stdout.write(" --data-descriptor-signature add data descriptor signatures (default: false)\n"); await stdout.write(" --keep-order keep entries order (default: true)\n"); await stdout.write(" --zip64 use Zip64 format (default: false)\n"); await stdout.write(" --prevent-parent-directories remove occurences of \"../\" in filenames (default: true)\n"); await stdout.write("\n"); Deno.exit(-1);}
function getOptions() { const options = { bufferedWrite: true, encryptionStrength: 3, dataDescriptor: true, keepOrder: true, includeDirectories: true, includeSubDirectories: true, preventParentDirectories: true }; for (const option of Object.getOwnPropertyNames(args)) { if (option != "_") { options[toCamelCase(option)] = args[option]; } } return options;}
async function runCommand(zipfile, list, options) { list = await Promise.all(list.map(file => getFileInfo(file, options))); if (options.includeDirectories) { list = await Promise.all(list.map(file => addDirectories(file, options))); } list = list.flat(); zipfile = await Deno.open(zipfile, { create: true, write: true }); configure(options); const zipWriter = new ZipWriter(zipfile, options); try { await Promise.all(list.map(file => addFile(zipWriter, file))); await zipWriter.close(); } finally { await terminateWorkers(); }}
async function addFile(zipWriter, file) { const options = { onstart: () => stdout.write(" adding: " + file.name + "\n") }; try { const reader = await getReader(file); if (reader) { await zipWriter.add(file.name, reader, options); } } catch (error) { await stdout.write(" error: " + error.message + ", file: " + file.url + "\n"); }}
async function getFileInfo(file, options) { let name, isFile, isDirectory, resolvedName, size; const remote = isRemote(file); if (remote) { const url = new URL(file, import.meta.url); name = url.hostname + (url.pathname == "/" ? "/index.html" : url.pathname); if (!extname(name)) { name += ".html"; } } else { name = file; resolvedName = resolvePath(name); } name = normalizePath(replaceSlashes(name)); if (resolvedName) { if (options.preventParentDirectories) { name = cleanFilename(name); } const stat = await Deno.stat(resolvedName); isFile = stat.isFile; isDirectory = stat.isDirectory; size = stat.size; } return { url: file, name, remote, isFile, size, isDirectory, resolvedName };}
async function addDirectories(file, options) { if (file.isDirectory) { const result = []; for await (const entry of Deno.readDir(resolvePath(file.url))) { const fileInfo = await getFileInfo(file.url + "/" + entry.name, options); if (entry.isFile) { result.push(fileInfo); } else if (entry.isDirectory && options.includeSubDirectories) { result.push(await addDirectories(fileInfo, options)); } } return result.flat(); } else { return file; }}
async function getReader(file) { if (file.remote) { return new HttpReader(file.url); } else if (file.isFile) { const { size } = file; const { readable } = await Deno.open(resolvePath(file.url), { read: true }); return { readable, size }; }}
function isRemote(path) { return path.startsWith("http:") || path.startsWith("https:");}
function getTextWriter(stdout) { const textEncoderStream = new TextEncoderStream(); textEncoderStream.readable.pipeTo(stdout.writable); return textEncoderStream.writable.getWriter();}
function replaceSlashes(url) { url = url.replace(/\\/g, "/"); const match = url.match(/^(?:\/)?(.*?)(?:\/)?$/); if (match) { return match[1]; } else { return url; }}
function cleanFilename(name) { const result = replaceSlashes(("/" + name + "/").replace(/(\/|\\)+(\.+)(\/|\\)+/g, "/")); if (result == name) { return result; } else { return cleanFilename(result); }}
function toCamelCase(kebabCase) { return kebabCase .split("-") .map((word, indexWord) => indexWord ? word.charAt(0).toUpperCase() + word.substring(1) : word) .join("");}
zipjs

Version Info

Tagged at
3 weeks ago