Extract unzipFile function
This commit is contained in:
@@ -1,12 +1,5 @@
|
||||
import { availableParallelism } from "os";
|
||||
import { dirname, join } from "path";
|
||||
import { createWriteStream, ensureDir } from "fs-extra";
|
||||
import {
|
||||
copyStream,
|
||||
openZip,
|
||||
openZipReadStream,
|
||||
readZipEntries,
|
||||
} from "./unzip";
|
||||
import { openZip, readZipEntries, unzipFile } from "./unzip";
|
||||
import PQueue from "p-queue";
|
||||
|
||||
export async function unzipToDirectoryConcurrently(
|
||||
@@ -28,30 +21,7 @@ export async function unzipToDirectoryConcurrently(
|
||||
|
||||
await queue.addAll(
|
||||
entries.map((entry) => async () => {
|
||||
const path = join(destinationPath, entry.fileName);
|
||||
|
||||
if (/\/$/.test(entry.fileName)) {
|
||||
// Directory file names end with '/'
|
||||
|
||||
await ensureDir(path);
|
||||
} else {
|
||||
// Ensure the directory exists
|
||||
await ensureDir(dirname(path));
|
||||
|
||||
const readable = await openZipReadStream(zipFile, entry);
|
||||
|
||||
let mode: number | undefined = entry.externalFileAttributes >>> 16;
|
||||
if (mode <= 0) {
|
||||
mode = undefined;
|
||||
}
|
||||
|
||||
const writeStream = createWriteStream(path, {
|
||||
autoClose: true,
|
||||
mode,
|
||||
});
|
||||
|
||||
await copyStream(readable, writeStream);
|
||||
}
|
||||
await unzipFile(zipFile, entry, destinationPath);
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
|
||||
@@ -51,7 +51,7 @@ export function readZipEntries(zipFile: ZipFile): Promise<ZipEntry[]> {
|
||||
});
|
||||
}
|
||||
|
||||
export function openZipReadStream(
|
||||
function openZipReadStream(
|
||||
zipFile: ZipFile,
|
||||
entry: ZipEntry,
|
||||
): Promise<Readable> {
|
||||
@@ -86,7 +86,7 @@ export async function openZipBuffer(
|
||||
});
|
||||
}
|
||||
|
||||
export async function copyStream(
|
||||
async function copyStream(
|
||||
readable: Readable,
|
||||
writeStream: WriteStream,
|
||||
): Promise<void> {
|
||||
@@ -102,6 +102,44 @@ export async function copyStream(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Unzips a single file from a zip archive.
|
||||
*
|
||||
* @param zipFile
|
||||
* @param entry
|
||||
* @param rootDestinationPath
|
||||
*/
|
||||
export async function unzipFile(
|
||||
zipFile: ZipFile,
|
||||
entry: ZipEntry,
|
||||
rootDestinationPath: string,
|
||||
): Promise<void> {
|
||||
const path = join(rootDestinationPath, entry.fileName);
|
||||
|
||||
if (/\/$/.test(entry.fileName)) {
|
||||
// Directory file names end with '/'
|
||||
|
||||
await ensureDir(path);
|
||||
} else {
|
||||
// Ensure the directory exists
|
||||
await ensureDir(dirname(path));
|
||||
|
||||
const readable = await openZipReadStream(zipFile, entry);
|
||||
|
||||
let mode: number | undefined = entry.externalFileAttributes >>> 16;
|
||||
if (mode <= 0) {
|
||||
mode = undefined;
|
||||
}
|
||||
|
||||
const writeStream = createWriteStream(path, {
|
||||
autoClose: true,
|
||||
mode,
|
||||
});
|
||||
|
||||
await copyStream(readable, writeStream);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sequentially unzips all files from a zip archive. Please use
|
||||
* `unzipToDirectoryConcurrently` if you can. This function is only
|
||||
@@ -124,30 +162,7 @@ export async function unzipToDirectorySequentially(
|
||||
const entries = await readZipEntries(zipFile);
|
||||
|
||||
for (const entry of entries) {
|
||||
const path = join(destinationPath, entry.fileName);
|
||||
|
||||
if (/\/$/.test(entry.fileName)) {
|
||||
// Directory file names end with '/'
|
||||
|
||||
await ensureDir(path);
|
||||
} else {
|
||||
// Ensure the directory exists
|
||||
await ensureDir(dirname(path));
|
||||
|
||||
const readable = await openZipReadStream(zipFile, entry);
|
||||
|
||||
let mode: number | undefined = entry.externalFileAttributes >>> 16;
|
||||
if (mode <= 0) {
|
||||
mode = undefined;
|
||||
}
|
||||
|
||||
const writeStream = createWriteStream(path, {
|
||||
autoClose: true,
|
||||
mode,
|
||||
});
|
||||
|
||||
await copyStream(readable, writeStream);
|
||||
}
|
||||
await unzipFile(zipFile, entry, destinationPath);
|
||||
}
|
||||
} finally {
|
||||
zipFile.close();
|
||||
|
||||
Reference in New Issue
Block a user