feat(archive): add 7z and tar.7z converters
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -98,17 +98,56 @@ async function createTarBz2(srcDir, outputPath, quality) {
|
||||
}
|
||||
}
|
||||
|
||||
function sevenZipLevel(quality) {
|
||||
return quality ?? 5;
|
||||
}
|
||||
|
||||
async function extract7z(inputPath, destDir) {
|
||||
await _7z.unpack(inputPath, destDir);
|
||||
}
|
||||
|
||||
async function create7z(srcDir, outputPath, quality) {
|
||||
await _7z.cmd(['a', '-r', `-mx=${sevenZipLevel(quality)}`, outputPath, path.join(srcDir, '*')]);
|
||||
}
|
||||
|
||||
async function extractTar7z(inputPath, destDir) {
|
||||
const decompressDir = await fs.mkdtemp(path.join(os.tmpdir(), 'archive-7z-'));
|
||||
try {
|
||||
await _7z.unpack(inputPath, decompressDir);
|
||||
const [tarName] = await fs.readdir(decompressDir);
|
||||
await tar.extract({ file: path.join(decompressDir, tarName), cwd: destDir });
|
||||
} finally {
|
||||
await fs.rm(decompressDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function createTar7z(srcDir, outputPath, quality) {
|
||||
const buildDir = await fs.mkdtemp(path.join(os.tmpdir(), 'archive-7z-'));
|
||||
try {
|
||||
const tarPath = path.join(buildDir, 'archive.tar');
|
||||
const entries = await fs.readdir(srcDir);
|
||||
await tar.create({ file: tarPath, cwd: srcDir }, entries);
|
||||
await _7z.cmd(['a', `-mx=${sevenZipLevel(quality)}`, outputPath, tarPath]);
|
||||
} finally {
|
||||
await fs.rm(buildDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
const EXTRACTORS = {
|
||||
zip: extractZip,
|
||||
tar: extractTarLike,
|
||||
'tar.gz': extractTarLike,
|
||||
'tar.bz2': extractTarBz2,
|
||||
'7z': extract7z,
|
||||
'tar.7z': extractTar7z,
|
||||
};
|
||||
const CREATORS = {
|
||||
zip: createZip,
|
||||
tar: createTar,
|
||||
'tar.gz': createTarGz,
|
||||
'tar.bz2': createTarBz2,
|
||||
'7z': create7z,
|
||||
'tar.7z': createTar7z,
|
||||
};
|
||||
|
||||
async function convert(inputPath, outputPath, options, sourceFormat, targetFormat) {
|
||||
|
||||
Reference in New Issue
Block a user