Instantly share code, notes, and snippets.

What would you like to do?
gzip without header and base 64 encode, in Node
var fs = require('fs');
var zlib = require('zlib');
// Read local subtitle, gunzip it, encode to base64
var toUpload; // future gunzipped/base64 encoded string
var path = 'foo/'; // path to subtitle
fs.readFile(path, function(err, data) { // read subtitle
if (err) throw err; // handle reading file error
zlib.deflate(data, function(err, buffer) { // gunzip it
if (err) throw err; // handle compression error
toUpload = buffer.toString('base64'); // encode to base64
// Decode base64 string, decompress content, write in file
var data = 'xxx'; // base64 encoded string recieved from OpenSubtitles
var path = 'foo/'; // path to future subtitle file
var buf = new Buffer(data, 'base64'); // read the base64 string
zlib.unzip(buf, function (err, buffer) { // decompress the content
if (err) throw err; // handle decompression error
var content = buffer.toString('utf8'); // encode in utf-8
fs.writeFile(path, content, function (err) { // write file to path
if (err) throw err; // handle writing to file error

This comment has been minimized.

Show comment
Hide comment

vankasteelj commented Feb 21, 2016

Some extra reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment