Extract common functionality between unzip implementations

This commit is contained in:
Koen Vlaswinkel
2023-12-19 15:00:08 +01:00
parent 7c00768c90
commit 7a1f157225
2 changed files with 42 additions and 35 deletions

View File

@@ -1,30 +1,16 @@
import { availableParallelism } from "os";
import { openZip, readZipEntries, unzipFile } from "./unzip";
import { unzipToDirectory } from "./unzip";
import PQueue from "p-queue";
export async function unzipToDirectoryConcurrently(
archivePath: string,
destinationPath: string,
): Promise<void> {
const zipFile = await openZip(archivePath, {
autoClose: false,
strictFileNames: true,
lazyEntries: true,
const queue = new PQueue({
concurrency: availableParallelism(),
});
try {
const entries = await readZipEntries(zipFile);
const queue = new PQueue({
concurrency: availableParallelism(),
});
await queue.addAll(
entries.map((entry) => async () => {
await unzipFile(zipFile, entry, destinationPath);
}),
);
} finally {
zipFile.close();
}
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
await queue.addAll(tasks);
});
}

View File

@@ -109,7 +109,7 @@ async function copyStream(
* @param entry
* @param rootDestinationPath
*/
export async function unzipFile(
async function unzipFile(
zipFile: ZipFile,
entry: ZipEntry,
rootDestinationPath: string,
@@ -140,6 +140,37 @@ export async function unzipFile(
}
}
/**
* Unzips all files from a zip archive. Please use
* `unzipToDirectoryConcurrently` or `unzipToDirectorySequentially` instead
* of this function.
*
* @param archivePath
* @param destinationPath
* @param taskRunner A function that runs the tasks (either sequentially or concurrently).
*/
export async function unzipToDirectory(
archivePath: string,
destinationPath: string,
taskRunner: (tasks: Array<() => Promise<void>>) => Promise<void>,
): Promise<void> {
const zipFile = await openZip(archivePath, {
autoClose: false,
strictFileNames: true,
lazyEntries: true,
});
try {
const entries = await readZipEntries(zipFile);
await taskRunner(
entries.map((entry) => () => unzipFile(zipFile, entry, destinationPath)),
);
} finally {
zipFile.close();
}
}
/**
* Sequentially unzips all files from a zip archive. Please use
* `unzipToDirectoryConcurrently` if you can. This function is only
@@ -152,19 +183,9 @@ export async function unzipToDirectorySequentially(
archivePath: string,
destinationPath: string,
): Promise<void> {
const zipFile = await openZip(archivePath, {
autoClose: false,
strictFileNames: true,
lazyEntries: true,
});
try {
const entries = await readZipEntries(zipFile);
for (const entry of entries) {
await unzipFile(zipFile, entry, destinationPath);
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
for (const task of tasks) {
await task();
}
} finally {
zipFile.close();
}
});
}