Last active
October 12, 2016 15:09
-
-
Save WilliamBerryiii/bd3ca5ca2a59cc201dc1f45ae42f0a2c to your computer and use it in GitHub Desktop.
GZip Examples
This file contains hidden or 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
public static IEnumerable<string> Compress(this IEnumerable<string> stream) | |
{ | |
foreach (var message in stream) | |
{ | |
// this string compression code is lifted & modified from | |
// http://madskristensen.net/post/Compress-and-decompress-strings-in-C | |
var buffer = Encoding.UTF8.GetBytes(message); | |
var ms = new MemoryStream(); | |
using (var zip = new GZipStream(ms, CompressionMode.Compress, true)) | |
{ | |
zip.Write(buffer, 0, buffer.Length); | |
} | |
ms.Position = 0; | |
var compressed = new byte[ms.Length]; | |
ms.Read(compressed, 0, compressed.Length); | |
var gzBuffer = new byte[compressed.Length + 4]; | |
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length); | |
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4); | |
yield return Convert.ToBase64String(gzBuffer); | |
} | |
} |
This file contains hidden or 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
//http://madskristensen.net/post/Compress-and-decompress-strings-in-C | |
let compress (data : string) = | |
let buffer = Encoding.UTF8.GetBytes(data) | |
let ms = new MemoryStream() | |
( use zip = new GZipStream(ms, CompressionMode.Compress, true) | |
zip.Write(buffer, 0, buffer.Length) ) | |
ms.Position <- 0L | |
let compressed = Array.zeroCreate<byte> (int(ms.Length)) | |
ms.Read(compressed, 0, compressed.Length) |> ignore | |
let gzipBuffer = Array.zeroCreate<byte> (int(compressed.Length) + 4) | |
Buffer.BlockCopy(compressed, 0, gzipBuffer, 4, compressed.Length) | |
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzipBuffer, 0, 4) | |
Convert.ToBase64String gzipBuffer | |
let decompress (data : string) = | |
let gzipBuffer = Convert.FromBase64String(data) | |
( use memoryStream = new MemoryStream() | |
let dataLength = BitConverter.ToInt32(gzipBuffer, 0) | |
memoryStream.Write(gzipBuffer, 4, gzipBuffer.Length - 4) | |
let buffer = Array.zeroCreate<byte> (int(dataLength)) | |
memoryStream.Position <- 0L | |
( use zip = new GZipStream(memoryStream, CompressionMode.Decompress) | |
zip.Read(buffer, 0, buffer.Length) |> ignore) | |
Encoding.UTF8.GetString(buffer) | |
) |
This file contains hidden or 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
function compress(message) { | |
var data = Buffer.from(message).toString(); | |
var self = this; | |
zlib.gzip(data, function(err, compressed){ | |
if(!err){ | |
// add message data. | |
var target = Buffer.alloc(compressed.length + 4) | |
compressed.copy(target, 4, 0, compressed.length) | |
// add header data | |
var compressedLength = compressed.length.toString() | |
var compLen = Buffer.from(compressedLength); | |
compLen.copy(target, 0, 0, 4) | |
// send message | |
return new Uint8Array(Buffer.from(target.toString('base64'))); | |
}else{ | |
throw new Error('Error running gzip compression for data blob: %s', data); | |
} | |
}); | |
} | |
function decompress(input){ | |
var buffer = Buffer.from(input, 'base64').slice(4); | |
zlib.gunzip(buffer, (err, buffer) => { | |
if (!err) { | |
var decompressed = buffer.toString('utf-8'); | |
} else { | |
context.log(err);// handle error | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment