Skip to content

Instantly share code, notes, and snippets.

@vankasteelj
Last active June 15, 2021 21:41
Show Gist options
  • Save vankasteelj/ceaf706881be02452ac2 to your computer and use it in GitHub Desktop.
Save vankasteelj/ceaf706881be02452ac2 to your computer and use it in GitHub Desktop.
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/bar.srt'; // 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/bar.srt'; // 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
});
});
@vankasteelj
Copy link
Author

Some extra reading:

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