Use gunzip on a folder

297 Views Asked by At

Right so I have a folder full of other folders, which are compressed into .gz files. Inside these folders is text files.

I want to have a program that loops through these text files to see if they contain a specific string, but to do so I need to uncompress them first. I don't want to start messing about with files (unless I can just make them temporarily and delete after), i just want to perform operations on the contents of the .gz folder. I've tried zlib.Gunzip()._outBuffer.toString() which gives a load of gibberish when used on a compressed folder.

How should I proceed?

1

There are 1 best solutions below

2
On

Had to to something quite similar recently, here's what worked for me: Basically you just read in the file into a buffer which you then can pass to the gunzip function. This will return another buffer on which you can invoke toString('utf8') in order the get the contents as a string, which is exactly what you need:

const util = require('util');
let {gunzip} = require('zlib');
const fs = require('fs');
gunzip = util.promisify(gunzip);

async function getStringFromGzipFile(inputFilePath) {
    const sourceBuffer = await fs.promises.readFile(inputFilePath);
    return await gunzip(sourceBuffer);
}


(async () => {
   const stringContent = await getStringFromGzipFile('/path/to/file');
   console.log(stringContent);
})()

EDIT:

If you want to gunzip and extract a directory, you can use tar-fs which will extract the contents to a specified directory. Once your done processing the files in it you could just remove the directory. Here's how you would gunzip and extract a .tar.gz:

function gunzipFolder(sourceDir, destination) {
    fs.createReadStream(sourceDir)
        .pipe(zlib.createGunzip())
        .pipe(tar.extract(destination));
}