feat: add gzip support to readDir

This commit is contained in:
Ben Irvin 2024-01-16 14:51:03 +01:00
parent e6f04743d6
commit cff60542f3

View File

@ -158,13 +158,16 @@ const readDir = async (archive, dir) => {
* @type {string[]} * @type {string[]}
*/ */
const files = []; const files = [];
const isGzipped = archive.endsWith('.tar.gz');
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
pipeline( const streams = [fs.createReadStream(archive)];
[ if (isGzipped) {
// Source: Archive stream streams.push(zlib.createGunzip());
fs.createReadStream(archive), }
// Transform: tar parser
// Add the tar parser
streams.push(
new tar.Parse({ new tar.Parse({
// Match tar entry with the given filename // Match tar entry with the given filename
filter: (filePath, entry) => entry.type === 'File' && dir === path.dirname(filePath), filter: (filePath, entry) => entry.type === 'File' && dir === path.dirname(filePath),
@ -175,10 +178,10 @@ const readDir = async (archive, dir) => {
// Consume the entry anyway to avoid blocking the tar parser // Consume the entry anyway to avoid blocking the tar parser
await rawCollector(entry); await rawCollector(entry);
}, },
}), })
],
(err) => (err ? reject(err) : resolve())
); );
pipeline(streams, (err) => (err ? reject(err) : resolve()));
}); });
return files; return files;