Last active
December 15, 2015 04:29
-
-
Save jwchang0206/5202084 to your computer and use it in GitHub Desktop.
Zlib Compression / Decompression
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
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) |
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 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); | |
} | |
}); | |
}; |
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
{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}" |
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
{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