Skip to content

Commit 71aa543

Browse files
committed
feat(unzip): add normalize option
1 parent 4f70043 commit 71aa543

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

src/decompress.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import { ensureFile } from "./deps.ts";
2-
import { copy, ensureDir, join, noop, Untar, ZipReader } from "./deps.ts";
31
import { formatUnzipEntryFileName } from "./format.ts";
2+
import {
3+
copy,
4+
ensureDir,
5+
ensureFile,
6+
type Entry,
7+
join,
8+
noop,
9+
pass,
10+
TarEntry,
11+
Untar,
12+
ZipReader,
13+
} from "./deps.ts";
414

515
interface UntarOptions {
616
ignore?: (entryName: string) => boolean;
17+
normalize?(entry: TarEntry[]): TarEntry[];
718
}
819

920
export async function untar(
@@ -14,7 +25,11 @@ export async function untar(
1425
const reader = await Deno.open(file, { read: true });
1526
const untar = new Untar(reader);
1627
const paths: string[] = [];
17-
for await (const entry of untar) {
28+
let entrys = await Array.fromAsync(untar);
29+
if (options?.normalize) {
30+
entrys = options.normalize(entrys);
31+
}
32+
for (const entry of entrys) {
1833
const { fileName, type } = entry;
1934
if (options?.ignore && options?.ignore(fileName)) {
2035
continue;
@@ -41,6 +56,8 @@ interface UnzipOptions {
4156
*/
4257
useWebWorkers?: boolean;
4358
nameEncoding?: string;
59+
60+
normalize?(entry: Entry[]): Entry[];
4461
}
4562

4663
export async function unzip(
@@ -52,9 +69,21 @@ export async function unzip(
5269
const zipReader = new ZipReader(fd);
5370
const entrys = await zipReader.getEntries();
5471
const paths: string[] = [];
55-
const { ignore = noop, useWebWorkers = false, nameEncoding } = options || {};
56-
const promises = entrys.map(async (entry) => {
57-
formatUnzipEntryFileName(entry, nameEncoding);
72+
const {
73+
ignore = noop,
74+
useWebWorkers = false,
75+
nameEncoding,
76+
normalize = pass,
77+
} = options || {};
78+
79+
const newEntrys = normalize(
80+
entrys.map((entry) => {
81+
formatUnzipEntryFileName(entry, nameEncoding);
82+
return entry;
83+
}),
84+
);
85+
86+
const promises = newEntrys.map(async (entry) => {
5887
if (ignore(entry.filename)) {
5988
return;
6089
}

src/deps.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export { Tar, Untar } from "https://deno.land/std@0.208.0/archive/mod.ts";
1+
export {
2+
Tar,
3+
TarEntry,
4+
Untar,
5+
} from "https://deno.land/std@0.208.0/archive/mod.ts";
26
export { copy } from "https://deno.land/std@0.208.0/streams/copy.ts";
37
export { green } from "https://deno.land/std@0.208.0/fmt/colors.ts";
48
export {
@@ -32,3 +36,7 @@ export {
3236
export type { Entry } from "https://deno.land/x/zipjs@v2.7.34/index.d.ts";
3337
export { noop } from "https://deno.land/x/easy_std@v0.8.0/src/fn.ts";
3438
export { slash } from "https://deno.land/x/easy_std@v0.8.0/src/path.ts";
39+
40+
export function pass<T>(r: T) {
41+
return r;
42+
}

0 commit comments

Comments
 (0)