diff --git a/src/converters/archive.js b/src/converters/archive.js index 86827f8..4170267 100644 --- a/src/converters/archive.js +++ b/src/converters/archive.js @@ -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) { diff --git a/test/converters/archive.test.js b/test/converters/archive.test.js index 217ad0f..72e3812 100644 --- a/test/converters/archive.test.js +++ b/test/converters/archive.test.js @@ -161,3 +161,70 @@ describe('archive converters — tar.bz2', () => { 20000 ); }); + +describe('archive converters — 7z and tar.7z', () => { + it('registers 7z and tar.7z as targets for each other and for zip', () => { + expect(listTargetFormats('zip')).toContain('7z'); + expect(listTargetFormats('zip')).toContain('tar.7z'); + expect(listTargetFormats('7z')).toContain('tar.7z'); + }); + + it( + 'converts zip -> 7z, preserving nested paths and content', + async () => { + const zipPath = path.join(tmpDir, 'for-7z.zip'); + buildZipFixture(zipPath); + const sevenZPath = path.join(tmpDir, 'output.7z'); + + await resolve('zip', '7z').convert(zipPath, sevenZPath, { quality: 9 }); + + const listDir = await fs.mkdtemp(path.join(os.tmpdir(), '7z-check-')); + await resolve('7z', 'zip').convert(sevenZPath, path.join(listDir, 'roundtrip.zip')); + const rtZip = new AdmZip(path.join(listDir, 'roundtrip.zip')); + expect(rtZip.readAsText('hello.txt')).toBe('hello world'); + expect(rtZip.readAsText('nested/inner.txt')).toBe('nested content'); + await fs.rm(listDir, { recursive: true, force: true }); + }, + 20000 + ); + + it( + 'converts zip -> tar.7z -> tar, preserving content', + async () => { + const zipPath = path.join(tmpDir, 'for-tar7z.zip'); + buildZipFixture(zipPath); + const tar7zPath = path.join(tmpDir, 'output.tar.7z'); + + await resolve('zip', 'tar.7z').convert(zipPath, tar7zPath, { quality: 5 }); + + const backToTarPath = path.join(tmpDir, 'from-tar7z.tar'); + await resolve('tar.7z', 'tar').convert(tar7zPath, backToTarPath); + + const listDir = await fs.mkdtemp(path.join(os.tmpdir(), 'tar7z-check-')); + await tar.extract({ file: backToTarPath, cwd: listDir }); + expect(await fs.readFile(path.join(listDir, 'hello.txt'), 'utf8')).toBe('hello world'); + await fs.rm(listDir, { recursive: true, force: true }); + }, + 20000 + ); + + it( + 'produces a smaller-or-equal 7z at a higher compression level', + async () => { + const zipPath = path.join(tmpDir, 'for-7z-level.zip'); + const zip = new AdmZip(); + zip.addFile('big.txt', Buffer.from('ab'.repeat(100000))); + zip.writeZip(zipPath); + + const lowPath = path.join(tmpDir, 'low.7z'); + const highPath = path.join(tmpDir, 'high.7z'); + + await resolve('zip', '7z').convert(zipPath, lowPath, { quality: 0 }); + await resolve('zip', '7z').convert(zipPath, highPath, { quality: 9 }); + + const [lowStat, highStat] = await Promise.all([fs.stat(lowPath), fs.stat(highPath)]); + expect(highStat.size).toBeLessThanOrEqual(lowStat.size); + }, + 20000 + ); +});