Skip to content

Commit

Permalink
Feature: proper .tar.gz support (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Oct 14, 2022
1 parent bbcb52c commit 3de74c1
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 28 deletions.
179 changes: 175 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"dependencies": {
"@fast-csv/format": "^4.3.5",
"@types/tar": "^6.1.3",
"7zip-min": "^1.4.3",
"async": "^3.2.4",
"async-mutex": "^0.4.0",
Expand All @@ -66,6 +67,7 @@
"reflect-metadata": "^0.1.13",
"semver": "^7.3.8",
"simple-statistics": "^7.7.6",
"tar": "^6.1.11",
"term-size": "^3.0.2",
"trash": "^8.1.0",
"xml2js": "^0.4.23",
Expand Down
16 changes: 8 additions & 8 deletions src/types/archives/archiveFactory.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import path from 'path';

import Archive from './archive.js';
import Rar from './rar.js';
import SevenZip from './sevenZip.js';
import Tar from './tar.js';
import Zip from './zip.js';

export default class ArchiveFactory {
static archiveFrom(filePath: string): Archive {
const extension = path.extname(filePath).toLowerCase();

if (Zip.SUPPORTED_EXTENSIONS.indexOf(extension) !== -1) {
if (Zip.SUPPORTED_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext))) {
return new Zip(filePath);
} if (Rar.SUPPORTED_EXTENSIONS.indexOf(extension) !== -1) {
} if (Tar.SUPPORTED_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext))) {
return new Tar(filePath);
} if (Rar.SUPPORTED_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext))) {
return new Rar(filePath);
} if (SevenZip.SUPPORTED_EXTENSIONS.indexOf(extension) !== -1) {
} if (SevenZip.SUPPORTED_EXTENSIONS.some((ext) => filePath.toLowerCase().endsWith(ext))) {
return new SevenZip(filePath);
}

Expand All @@ -23,8 +22,9 @@ export default class ArchiveFactory {
static isArchive(filePath: string): boolean {
return [
...Zip.SUPPORTED_EXTENSIONS,
...Tar.SUPPORTED_EXTENSIONS,
...Rar.SUPPORTED_EXTENSIONS,
...SevenZip.SUPPORTED_EXTENSIONS,
].indexOf(path.extname(filePath)) !== -1;
].some((ext) => filePath.toLowerCase().endsWith(ext));
}
}
21 changes: 10 additions & 11 deletions src/types/archives/sevenZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import Archive from './archive.js';

export default class SevenZip extends Archive {
// p7zip `7za i`
// WARNING: tar+compression doesn't work, you'll be left with a tar file output
static readonly SUPPORTED_EXTENSIONS = [
'.7z', // 7z
'.bz2', '.bzip2', '.tbz2', '.tbz', // bzip2
'.bz2', '.bzip2', // bzip2
'.cab', // cab
'.gz', '.gzip', '.tgz', '.tpz', // gzip
'.gz', '.gzip', // gzip
'.lzma', // lzma
'.lzma86', // lzma86
'.pmd', // ppmd
'.001', // split
'.tar', '.ova', // tar
'.xz', '.txz', // xz
'.z', '.taz', // z
'.xz', // xz
'.z', // z
'.zip', '.z01', '.zipx', // zip
'.zst', '.tzstd', // zstd
'.lz4', '.tlz4', // lz4
'.lz5', '.tlz5', // lz5
'.liz', '.tliz', // lizard
'.zst', // zstd
'.lz4', // lz4
'.lz5', // lz5
'.liz', // lizard
];

private static readonly LIST_MUTEX = new Mutex();
Expand Down Expand Up @@ -63,8 +64,6 @@ export default class SevenZip extends Archive {
tempDir: string,
callback: (localFile: string) => (T | Promise<T>),
): Promise<T> {
const localFile = path.join(tempDir, entryPath);

await new Promise<void>((resolve, reject) => {
_7z.unpack(this.getFilePath(), tempDir, (err) => {
if (err) {
Expand All @@ -75,6 +74,6 @@ export default class SevenZip extends Archive {
});
});

return callback(localFile);
return callback(path.join(tempDir, entryPath));
}
}
Loading

0 comments on commit 3de74c1

Please sign in to comment.