feat(archive): add tar.bz2 converter via 7za

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-01 12:52:29 +02:00
co-authored by Claude Sonnet 5
parent c9251e66e2
commit a76b10a43f
4 changed files with 85 additions and 2 deletions
+40 -2
View File
@@ -5,6 +5,7 @@ import os from 'node:os';
import AdmZip from 'adm-zip';
import { ZipArchive } from 'archiver';
import * as tar from 'tar';
import _7z from '7zip-min';
import { register } from './registry.js';
const MAX_EXTRACTED_BYTES = 2 * 1024 * 1024 * 1024;
@@ -70,8 +71,45 @@ async function createTarGz(srcDir, outputPath, quality) {
await tar.create({ file: outputPath, cwd: srcDir, gzip: { level: quality ?? 6 } }, entries);
}
const EXTRACTORS = { zip: extractZip, tar: extractTarLike, 'tar.gz': extractTarLike };
const CREATORS = { zip: createZip, tar: createTar, 'tar.gz': createTarGz };
function bzip2Level(quality) {
return Math.max(1, quality ?? 9);
}
async function extractTarBz2(inputPath, destDir) {
const decompressDir = await fs.mkdtemp(path.join(os.tmpdir(), 'archive-bz2-'));
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 createTarBz2(srcDir, outputPath, quality) {
const buildDir = await fs.mkdtemp(path.join(os.tmpdir(), 'archive-bz2-'));
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', '-tbzip2', `-mx=${bzip2Level(quality)}`, outputPath, tarPath]);
} finally {
await fs.rm(buildDir, { recursive: true, force: true });
}
}
const EXTRACTORS = {
zip: extractZip,
tar: extractTarLike,
'tar.gz': extractTarLike,
'tar.bz2': extractTarBz2,
};
const CREATORS = {
zip: createZip,
tar: createTar,
'tar.gz': createTarGz,
'tar.bz2': createTarBz2,
};
async function convert(inputPath, outputPath, options, sourceFormat, targetFormat) {
const { quality } = options ?? {};