Last active
June 15, 2021 21:41
-
-
Save vankasteelj/ceaf706881be02452ac2 to your computer and use it in GitHub Desktop.
gzip without header and base 64 encode, in Node
This file contains 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
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 | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some extra reading:
zlib
: https://nodejs.org/api/zlib.htmlfs
: https://nodejs.org/api/fs.htmltoString()
&new Buffer()
): https://nodejs.org/api/buffer.html