We live in a wasteful digital world. Our data stream were originally designed with little in the way of reducing latency or in optimizing network bandwidth/storage. But in a world where we need almost instant responses for our data transfers, we often implement compression. One of the most popular standards for compressing data streams is gzip, and which is a combination of LZ77 (Lempel Ziv 1977) and Huffman coding. These types of compression methods are all about finding the same repeated sequences in the data and referring to them.
GZip |
Background
The following is a sample run:
Input: applecore Compressed: eJxLLCjISU3OL0oFABKlA7w=
And node.js code is:
// Run as node a_13.js Hello zip // If you want to "Deflate" (compress) using the argument of "zip" var zlib = require('zlib'); var test="hello"; var flag="zip" var args = process.argv; if (args.length>1) test=args[2]; if (args.length>2) flag=args[3]; console.log("Input: ",test); if (flag=="zip") { var input = new Buffer.from(test) zlib.deflate(input, function(err, buf) { var res=buf.toString('base64'); console.log("Compressed: " ,res ); // console.log("Compressed: " ,buf ); }); } else { var input = new Buffer.from(test,'base64') zlib.inflate(input, function(err, buf) { if (buf==undefined) console.log("Incorrect format in processing Base64!"); else console.log("Uncompressed:", buf.toString("utf8") ); }); }