Last active
January 20, 2021 00:57
-
-
Save joepuzzo/bc087db8330441a776d79a6daffb1bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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