Skip to content

Instantly share code, notes, and snippets.

@joepuzzo
Last active January 20, 2021 00:57
Show Gist options
  • Save joepuzzo/bc087db8330441a776d79a6daffb1bc7 to your computer and use it in GitHub Desktop.
Save joepuzzo/bc087db8330441a776d79a6daffb1bc7 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const zlib = require('zlib');
// Read in a file
const pFile = fs.readFileSync(`${__dirname}/bigFile`);
// Log the byte size
console.log('Original Size:', Buffer.byteLength(pFile, 'utf8'));
// Compress it
const deflated = zlib.deflateSync(pFile).toString('base64');
// Log the byte size
console.log('Deflated Size:', Buffer.byteLength(deflated, 'utf8'));
// Write out that file
fs.writeFile(`${__dirname}/bigFileCompressed`, deflated, function(err, data) {
if (err) {
return console.log(err);
}
});
// Uncompress it
const inflated = zlib.inflateSync(Buffer.from(deflated, 'base64')).toString();
// Log the byte size
console.log('Inflated Size:', Buffer.byteLength(inflated, 'utf8'));
// Write out a new file
fs.writeFile(`${__dirname}/bigFileDecompressed`, inflated, function(err, data) {
if (err) {
return console.log(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment