I have been looking for client side conversion to gzip of file (in browser) and found pako.
Thing is that i am my running example for this conversion by using Node.js functions which are server side (at least this is my understanding of Node.js from what i read).
I would like to replace both "fs"(for writing to file) and "buffer" (for saving to format which is writable to the new function responsible for writing to file) to some native javascript functions.
This is the code.
var pako = require('pako'), data = "sample text";
var fs = require('fs');
data = pako.gzip(data);
var buffer = new Buffer(data.length);
for (var i = 0; i < data.length; i++) {
buffer.writeUInt8(data[i], i);
}
var wstream = fs.createWriteStream('output.gz');
wstream.write(buffer);
wstream.end();
You didn't really say what your end goal is, but I'm guessing you want to generate a file that the user can download from the browser. Using the Blob API that's simple. The below is based on (read: pretty much identical to) an example in the MDN docs:
const data = 'sample text';
const gzipped = pako.gzip(data); // => Uint8Array
const blob = new Blob([gzipped], {type: 'application/gzip'});
const blobUrl = URL.createObjectURL(blob);
console.log('Generated Blob; object URL: %s', blobUrl);
// render link
const link = document.createElement('a');
link.setAttribute('href', blobUrl);
link.innerHTML = 'Download file';
document.body.appendChild(link);
<script src="https://unpkg.com/pako@1.0.4/dist/pako_deflate.js"></script>