Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions extensions/ql-vscode/src/common/unzip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Entry as ZipEntry, open, Options as ZipOptions, ZipFile } from "yauzl";
import { Readable } from "stream";
import { dirname, join } from "path";
import { WriteStream } from "fs";
import { createWriteStream, ensureDir } from "fs-extra";

// We can't use promisify because it picks up the wrong overload.
export function openZip(
Expand Down Expand Up @@ -82,3 +85,63 @@ export async function openZipBuffer(
});
});
}

async function copyStream(
readable: Readable,
writeStream: WriteStream,
): Promise<void> {
return new Promise((resolve, reject) => {
readable.on("error", (err) => {
reject(err);
});
readable.on("end", () => {
resolve();
});

readable.pipe(writeStream);
});
}

export async function unzipToDirectory(
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) {
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);
}
}
} finally {
zipFile.close();
}
}
11 changes: 0 additions & 11 deletions extensions/ql-vscode/src/common/zip.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "./shared/variant-analysis";
import { DisposableObject, DisposeHandler } from "../common/disposable-object";
import { EventEmitter } from "vscode";
import { unzipFile } from "../common/zip";
import { unzipToDirectory } from "../common/unzip";
import { readRepoTask, writeRepoTask } from "./repo-tasks-store";

type CacheKey = `${number}/${string}`;
Expand Down Expand Up @@ -106,7 +106,7 @@ export class VariantAnalysisResultsManager extends DisposableObject {
VariantAnalysisResultsManager.RESULTS_DIRECTORY,
);

await unzipFile(zipFilePath, unzippedFilesDirectory);
await unzipToDirectory(zipFilePath, unzippedFilesDirectory);

this._onResultDownloaded.fire({
variantAnalysisId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe(VariantAnalysisResultsManager.name, () => {
});

afterEach(async () => {
if (fs.existsSync(variantAnalysisStoragePath)) {
fs.rmSync(variantAnalysisStoragePath, { recursive: true });
if (await fs.pathExists(variantAnalysisStoragePath)) {
await fs.remove(variantAnalysisStoragePath);
}
});

Expand Down