Skip to content Skip to sidebar Skip to footer

Use Gunzip On A Folder

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 file

Solution 1:

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);

asyncfunctiongetStringFromGzipFile(inputFilePath) {
    const sourceBuffer = await fs.promises.readFile(inputFilePath);
    returnawaitgunzip(sourceBuffer);
}


(async () => {
   const stringContent = awaitgetStringFromGzipFile('/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));
}

Post a Comment for "Use Gunzip On A Folder"