fix: Remove unzipper as it cannot handle zip within zip (#6162)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { mkdirp } from "fs-extra";
|
||||
import JSZip from "jszip";
|
||||
import tmp from "tmp";
|
||||
import yauzl from "yauzl";
|
||||
import { bytesToHumanReadable } from "@shared/utils/files";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { trace } from "@server/logging/tracing";
|
||||
@@ -20,6 +23,7 @@ export default class ZipHelper {
|
||||
/**
|
||||
* Write a zip file to a temporary disk location
|
||||
*
|
||||
* @deprecated Use `extract` instead
|
||||
* @param zip JSZip object
|
||||
* @returns pathname of the temporary file where the zip was written to disk
|
||||
*/
|
||||
@@ -87,4 +91,75 @@ export default class ZipHelper {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a zip file to a disk location
|
||||
*
|
||||
* @param filePath The file path where the zip is located
|
||||
* @param outputDir The directory where the zip should be extracted
|
||||
*/
|
||||
public static extract(filePath: string, outputDir: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
Logger.debug("utils", "Opening zip file", { filePath });
|
||||
|
||||
yauzl.open(
|
||||
filePath,
|
||||
{ lazyEntries: true, autoClose: true },
|
||||
function (err, zipfile) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
try {
|
||||
zipfile.readEntry();
|
||||
zipfile.on("entry", function (entry) {
|
||||
Logger.debug("utils", "Extracting zip entry", entry);
|
||||
if (/\/$/.test(entry.fileName)) {
|
||||
// directory file names end with '/'
|
||||
mkdirp(
|
||||
path.join(outputDir, entry.fileName),
|
||||
function (err: Error) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
zipfile.readEntry();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// file entry
|
||||
zipfile.openReadStream(entry, function (err, readStream) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// ensure parent directory exists
|
||||
mkdirp(
|
||||
path.join(outputDir, path.dirname(entry.fileName)),
|
||||
function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
readStream.pipe(
|
||||
fs.createWriteStream(
|
||||
path.join(outputDir, entry.fileName)
|
||||
)
|
||||
);
|
||||
readStream.on("end", function () {
|
||||
zipfile.readEntry();
|
||||
});
|
||||
readStream.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
zipfile.on("close", resolve);
|
||||
zipfile.on("error", reject);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user