Here's a basic functional abstraction for extracting archives with 7z
using Deno:
./process.ts
:
const decoder = new TextDecoder();
export type ProcessOutput = {
status: Deno.ProcessStatus;
stderr: string;
stdout: string;
};
/**
* Convenience wrapper around subprocess API.
* Requires permission `--allow-run`.
*/
export async function getProcessOutput(cmd: string[]): Promise<ProcessOutput> {
const process = Deno.run({ cmd, stderr: "piped", stdout: "piped" });
const [status, stderr, stdout] = await Promise.all([
process.status(),
decoder.decode(await process.stderrOutput()),
decoder.decode(await process.output()),
]);
process.close();
return { status, stderr, stdout };
}
./7z.ts
:
import { getProcessOutput, type ProcessOutput } from "./process.ts";
export { type ProcessOutput };
// Ref: https://sevenzip.osdn.jp/chm/cmdline/index.htm
export type ExtractOptions = {
/**
* Extract nested files and folders to the same output directory.
* Default: `false`
*/
flat?: boolean;
/**
* Destination directory for extraction of archive contents.
* 7-Zip replaces the `"*"` character with the name of the archive.
* Default: (the current working directory)
*/
outputDir?: string;
/** Overwrite existing files. Default: `true` */
overwrite?: boolean;
};
/**
* Extract the contents of an archive to the filesystem using `7z`.
* Requires `7z` to be in your `$PATH`.
* Requires permission `--allow-run=7z`.
*
* @param archivePath - Path to the target archive file
* @param options - Extraction options
*/
export function extractArchive(
archivePath: string,
options: ExtractOptions = {},
): Promise<ProcessOutput> {
const {
flat = false,
outputDir,
overwrite = true,
} = options;
const cmd = ["7z"];
// https://sevenzip.osdn.jp/chm/cmdline/commands/extract.htm
// https://sevenzip.osdn.jp/chm/cmdline/commands/extract_full.htm
cmd.push(flat ? "e" : "x");
// https://sevenzip.osdn.jp/chm/cmdline/switches/overwrite.htm
cmd.push(overwrite ? "-aoa" : "-aos");
// https://sevenzip.osdn.jp/chm/cmdline/switches/output_dir.htm
if (outputDir) cmd.push(`-o${outputDir}`);
// Disable interaction
// https://sevenzip.osdn.jp/chm/cmdline/switches/yes.htm
cmd.push("-y");
cmd.push(archivePath);
return getProcessOutput(cmd);
}
Example usage:
./main.ts
:
import { extractArchive, type ExtractOptions } from "./7z.ts";
async function main() {
const { status: { code, success }, stdout, stderr } = await extractArchive(
"example.zip",
);
if (!success) { // Something went wrong
console.error(`7-Zip exited with code: ${code}`);
console.error(stderr);
Deno.exit(1);
}
// Extraction was successful
console.log(stdout);
}
if (import.meta.main) main();
> deno run --allow-run=7z main.ts
Check file:///C:/Users/deno/so-71445897/main.ts
7-Zip 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26
Scanning the drive for archives:
1 file, 774 bytes (1 KiB)
Extracting archive: example.zip
--
Path = example.zip
Type = zip
Physical Size = 774
Everything is Ok
Folders: 3
Files: 2
Size: 38
Compressed: 774