Skip to content

Commit 7c00768

Browse files
committed
Extract unzipFile function
1 parent 9038586 commit 7c00768

File tree

2 files changed

+43
-58
lines changed

2 files changed

+43
-58
lines changed

extensions/ql-vscode/src/common/unzip-concurrently.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { availableParallelism } from "os";
2-
import { dirname, join } from "path";
3-
import { createWriteStream, ensureDir } from "fs-extra";
4-
import {
5-
copyStream,
6-
openZip,
7-
openZipReadStream,
8-
readZipEntries,
9-
} from "./unzip";
2+
import { openZip, readZipEntries, unzipFile } from "./unzip";
103
import PQueue from "p-queue";
114

125
export async function unzipToDirectoryConcurrently(
@@ -28,30 +21,7 @@ export async function unzipToDirectoryConcurrently(
2821

2922
await queue.addAll(
3023
entries.map((entry) => async () => {
31-
const path = join(destinationPath, entry.fileName);
32-
33-
if (/\/$/.test(entry.fileName)) {
34-
// Directory file names end with '/'
35-
36-
await ensureDir(path);
37-
} else {
38-
// Ensure the directory exists
39-
await ensureDir(dirname(path));
40-
41-
const readable = await openZipReadStream(zipFile, entry);
42-
43-
let mode: number | undefined = entry.externalFileAttributes >>> 16;
44-
if (mode <= 0) {
45-
mode = undefined;
46-
}
47-
48-
const writeStream = createWriteStream(path, {
49-
autoClose: true,
50-
mode,
51-
});
52-
53-
await copyStream(readable, writeStream);
54-
}
24+
await unzipFile(zipFile, entry, destinationPath);
5525
}),
5626
);
5727
} finally {

extensions/ql-vscode/src/common/unzip.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function readZipEntries(zipFile: ZipFile): Promise<ZipEntry[]> {
5151
});
5252
}
5353

54-
export function openZipReadStream(
54+
function openZipReadStream(
5555
zipFile: ZipFile,
5656
entry: ZipEntry,
5757
): Promise<Readable> {
@@ -86,7 +86,7 @@ export async function openZipBuffer(
8686
});
8787
}
8888

89-
export async function copyStream(
89+
async function copyStream(
9090
readable: Readable,
9191
writeStream: WriteStream,
9292
): Promise<void> {
@@ -102,6 +102,44 @@ export async function copyStream(
102102
});
103103
}
104104

105+
/**
106+
* Unzips a single file from a zip archive.
107+
*
108+
* @param zipFile
109+
* @param entry
110+
* @param rootDestinationPath
111+
*/
112+
export async function unzipFile(
113+
zipFile: ZipFile,
114+
entry: ZipEntry,
115+
rootDestinationPath: string,
116+
): Promise<void> {
117+
const path = join(rootDestinationPath, entry.fileName);
118+
119+
if (/\/$/.test(entry.fileName)) {
120+
// Directory file names end with '/'
121+
122+
await ensureDir(path);
123+
} else {
124+
// Ensure the directory exists
125+
await ensureDir(dirname(path));
126+
127+
const readable = await openZipReadStream(zipFile, entry);
128+
129+
let mode: number | undefined = entry.externalFileAttributes >>> 16;
130+
if (mode <= 0) {
131+
mode = undefined;
132+
}
133+
134+
const writeStream = createWriteStream(path, {
135+
autoClose: true,
136+
mode,
137+
});
138+
139+
await copyStream(readable, writeStream);
140+
}
141+
}
142+
105143
/**
106144
* Sequentially unzips all files from a zip archive. Please use
107145
* `unzipToDirectoryConcurrently` if you can. This function is only
@@ -124,30 +162,7 @@ export async function unzipToDirectorySequentially(
124162
const entries = await readZipEntries(zipFile);
125163

126164
for (const entry of entries) {
127-
const path = join(destinationPath, entry.fileName);
128-
129-
if (/\/$/.test(entry.fileName)) {
130-
// Directory file names end with '/'
131-
132-
await ensureDir(path);
133-
} else {
134-
// Ensure the directory exists
135-
await ensureDir(dirname(path));
136-
137-
const readable = await openZipReadStream(zipFile, entry);
138-
139-
let mode: number | undefined = entry.externalFileAttributes >>> 16;
140-
if (mode <= 0) {
141-
mode = undefined;
142-
}
143-
144-
const writeStream = createWriteStream(path, {
145-
autoClose: true,
146-
mode,
147-
});
148-
149-
await copyStream(readable, writeStream);
150-
}
165+
await unzipFile(zipFile, entry, destinationPath);
151166
}
152167
} finally {
153168
zipFile.close();

0 commit comments

Comments
 (0)