SharpZipLib is a free C# compression library. The code below shows how to use it to gzip staff in C#.
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.GZip;
public static byte[] Compress(byte[] buffer) {
MemoryStream ms = new MemoryStream();
Stream stream = new GZipOutputStream(ms, buffer.Length);
try {
stream.Write(buffer, 0, buffer.Length);
}
finally {
stream.Close();
}
return ms.ToArray();
}
public static byte[] Decompress(byte[] buffer) {
MemoryStream ms = new MemoryStream();
byte[] data = new byte[4096];
Stream stream = new GZipInputStream(new MemoryStream(buffer));
try {
while (true) {
int bytes = stream.Read(data, 0, data.Length);
if (bytes < 1) {
break;
}
ms.Write(data, 0, bytes);
}
}
finally {
stream.Close();
}
return ms.ToArray();
}
No comments:
Post a Comment