Skip to content

Commit 857a997

Browse files
committed
Add progress reporting for extracted files
1 parent 262744e commit 857a997

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { availableParallelism } from "os";
2-
import { unzipToDirectory } from "./unzip";
2+
import { UnzipProgressCallback, unzipToDirectory } from "./unzip";
33
import PQueue from "p-queue";
44

55
export async function unzipToDirectoryConcurrently(
66
archivePath: string,
77
destinationPath: string,
8+
progress?: UnzipProgressCallback,
89
): Promise<void> {
910
const queue = new PQueue({
1011
concurrency: availableParallelism(),
1112
});
1213

13-
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
14-
await queue.addAll(tasks);
15-
});
14+
return unzipToDirectory(
15+
archivePath,
16+
destinationPath,
17+
progress,
18+
async (tasks) => {
19+
await queue.addAll(tasks);
20+
},
21+
);
1622
}

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ async function copyStream(
9797
});
9898
}
9999

100+
type UnzipProgress = {
101+
filesExtracted: number;
102+
totalFiles: number;
103+
};
104+
105+
export type UnzipProgressCallback = (progress: UnzipProgress) => void;
106+
100107
/**
101108
* Unzips a single file from a zip archive.
102109
*
@@ -143,10 +150,12 @@ async function unzipFile(
143150
* @param archivePath
144151
* @param destinationPath
145152
* @param taskRunner A function that runs the tasks (either sequentially or concurrently).
153+
* @param progress
146154
*/
147155
export async function unzipToDirectory(
148156
archivePath: string,
149157
destinationPath: string,
158+
progress: UnzipProgressCallback | undefined,
150159
taskRunner: (tasks: Array<() => Promise<void>>) => Promise<void>,
151160
): Promise<void> {
152161
const zipFile = await openZip(archivePath, {
@@ -158,8 +167,23 @@ export async function unzipToDirectory(
158167
try {
159168
const entries = await readZipEntries(zipFile);
160169

170+
let filesExtracted = 0;
171+
const totalFiles = entries.length;
172+
173+
const reportProgress = () => {
174+
progress?.({
175+
filesExtracted,
176+
totalFiles,
177+
});
178+
};
179+
161180
await taskRunner(
162-
entries.map((entry) => () => unzipFile(zipFile, entry, destinationPath)),
181+
entries.map((entry) => async () => {
182+
await unzipFile(zipFile, entry, destinationPath);
183+
184+
filesExtracted++;
185+
reportProgress();
186+
}),
163187
);
164188
} finally {
165189
zipFile.close();
@@ -173,14 +197,21 @@ export async function unzipToDirectory(
173197
*
174198
* @param archivePath
175199
* @param destinationPath
200+
* @param progress
176201
*/
177202
export async function unzipToDirectorySequentially(
178203
archivePath: string,
179204
destinationPath: string,
205+
progress?: UnzipProgressCallback,
180206
): Promise<void> {
181-
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
182-
for (const task of tasks) {
183-
await task();
184-
}
185-
});
207+
return unzipToDirectory(
208+
archivePath,
209+
destinationPath,
210+
progress,
211+
async (tasks) => {
212+
for (const task of tasks) {
213+
await task();
214+
}
215+
},
216+
);
186217
}

0 commit comments

Comments
 (0)