Earlier, I was looking for some wrappers around the .NET zip class as they were a bit cumbersome to use, so I found a ground wrapper, which I've improved upon with some more additional methods. You may find them useful, particularly when consuming stuff from the internet which is compressed over HTTP.
Using GZip Compression:
public static class GZipHelper
{
public static long Compress(Stream input, Stream output)
{
// Create a buffer to transfer the data
byte[] buffer = new byte[1024];
int bytesRead = 0;
long totalBytes = 0;
// Get the stream that will perform the decompression
using (Stream zip = new GZipStream(output, CompressionMode.Compress))
{
// Use the buffer to move data to the compression stream until complete
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytesRead;
zip.Write(buffer, 0, bytesRead);
}
// Close the compression stream
zip.Close();
}
// Return the total number of bytes of compressed data
return totalBytes;
}
public static long Decompress(Stream input, Stream output)
{
// Create a buffer to transfer the data
byte[] buffer = new byte[1024];
int bytesRead = 0;
long totalBytes = 0;
// Get the stream that will perform the decompression
using (Stream zip = new GZipStream(input, CompressionMode.Decompress, true))
{
// Use the buffer to move data to the compression stream until complete
while ((bytesRead = zip.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytesRead;
output.Write(buffer, 0, bytesRead);
}
// Close the compression stream
zip.Close();
}
// Returns the total number of bytes of decompressed data
return totalBytes;
}
public static byte[] Compress(byte[] data)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream(), input = new MemoryStream(data))
{
// Process the compression
Compress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// Convert the output stream to a byte array
return final;
}
public static byte[] Decompress(byte[] gzipData)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream(), input = new MemoryStream(gzipData))
{
// Process the compression
Decompress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// Convert the output stream to a byte array
return final;
}
public static string Compress(string data)
{
// get a byte array of the data and pass to the Compress method.
return Convert.ToBase64String(Compress(Encoding.Default.GetBytes(data)));
}
public static string Decompress(string gzipString)
{
// Decompress and convert data to a string
return Encoding.Default.GetString(Decompress(Convert.FromBase64String(gzipString)));
}
public static string Decompress(Stream input)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream())
{
// Process the compression
Decompress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// we get the string
return Encoding.Default.GetString(final);
}
}
Using Deflate Compression:
public static class DeflateHelper
{
public static long Compress(Stream input, Stream output)
{
// Create a buffer to transfer the data
byte[] buffer = new byte[1024];
int bytesRead = 0;
long totalBytes = 0;
// Get the stream that will perform the decompression
using (Stream zip = new DeflateStream(output, CompressionMode.Compress))
{
// Use the buffer to move data to the compression stream until complete
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytesRead;
zip.Write(buffer, 0, bytesRead);
}
// Close the compression stream
zip.Close();
}
// Return the total number of bytes of compressed data
return totalBytes;
}
public static long Decompress(Stream input, Stream output)
{
// Create a buffer to transfer the data
byte[] buffer = new byte[1024];
int bytesRead = 0;
long totalBytes = 0;
// Get the stream that will perform the decompression
using (Stream zip = new DeflateStream(input, CompressionMode.Decompress, true))
{
// Use the buffer to move data to the compression stream until complete
while ((bytesRead = zip.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytesRead;
output.Write(buffer, 0, bytesRead);
}
// Close the compression stream
zip.Close();
}
// Returns the total number of bytes of decompressed data
return totalBytes;
}
public static byte[] Compress(byte[] data)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream(), input = new MemoryStream(data))
{
// Process the compression
Compress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// Convert the output stream to a byte array
return final;
}
public static byte[] Decompress(byte[] deflateData)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream(), input = new MemoryStream(deflateData))
{
// Process the compression
Decompress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// Convert the output stream to a byte array
return final;
}
public static string Compress(string data)
{
// get a byte array of the data and pass to the Compress method.
return Convert.ToBase64String(Compress(Encoding.Default.GetBytes(data)));
}
public static string Decompress(string deflateString)
{
// Decompress and convert data to a string
return Encoding.Default.GetString(Decompress(Convert.FromBase64String(deflateString)));
}
public static string Decompress(Stream input)
{
byte[] final;
// Create a stream to hold the output
using (MemoryStream output = new MemoryStream())
{
// Process the compression
Decompress(input, output);
// Get the resultant data
final = output.ToArray();
// Close the underlying streams
input.Close();
output.Close();
}
// we get the string
return Encoding.Default.GetString(final);
}
}