Skip to content

Instantly share code, notes, and snippets.

@jwchang0206
Last active December 15, 2015 04:29
Show Gist options
  • Save jwchang0206/5202084 to your computer and use it in GitHub Desktop.
Save jwchang0206/5202084 to your computer and use it in GitHub Desktop.
Zlib Compression / Decompression
zlib = require "zlib"
exports.deflate = (str = "", callback) ->
buffer = new Buffer str, "utf8"
zlib.deflate buffer, (err, str) ->
str = str.toString "binary" if str?
callback?(err, str)
exports.inflate = (str = "", callback) ->
buffer = new Buffer str, "binary"
zlib.inflate buffer, (err, str) ->
str = str.toString "utf8" if str?
callback?(err, str)
var zlib = require("zlib");
exports.deflate = function(str, callback) {
if (str == null) {
str = "";
}
var buffer = new Buffer(str, "utf8");
zlib.deflate(buffer, function(err, str) {
if (str != null) {
str = str.toString("binary");
}
if (typeof callback === "function") {
callback(err, str);
}
});
};
exports.inflate = function(str, callback) {
if (str == null) {
str = "";
}
var buffer = new Buffer(str, "binary");
zlib.inflate(buffer, function(err, str) {
if (str != null) {
str = str.toString("utf8");
}
if (typeof callback === "function") {
callback(err, str);
}
});
};
{deflate} = require "./compress"
str = "<p></p><p></p><br /><br /><img src='test.img' alt='' />"
deflate str, (err, deflated) ->
console.log deflated
console.log "================================"
console.log "original: #{str.length}"
console.log "compressed: #{deflated.length}"
{deflate, inflate} = require "./compress"
str = "<p></p><p></p><br /><br /><img src='test.img' alt='' />"
deflate str, (err, deflated) ->
inflate deflated, (err, infalted) ->
console.log inflated
console.log "================================"
console.log "original: #{str.length}"
console.log "compressed: #{deflated.length}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment