#region Compiler Directives
using System;
using System.IO;
using System.IO.Compression;
using Corp.Framework.ExceptionCommon;
using Corp.Framework.Common;
#endregion
namespace MyApp
{
public class MyZipper
{
#region private utils
///
/// Private - Convert a Stream to a Byte Array
///
/// The input Stream
private static byte[] StreamtoByte(Stream stream)
{
//Chunk Size
int initialLength = 32768;
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
/* If we've reached the end of our buffer, check to see if there's
any more information */
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
/* End of stream? If so, we're done */
if (nextByte == -1)
{
return buffer;
}
/* Nope. Resize the buffer, put in the byte we've just
read, and continue */
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
/* Buffer is now too big. Shrink it. */
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
#endregion
#region public static methods for Byte[] Buffers
///
/// Unzips a Gzipped Byte Array and returns the result as a Byte Array
///
/// The Gzipped Byte Array
public static byte[] GZipBytetoByte(Byte[] GzippedBuffer)
{
MemoryStream ms = new MemoryStream(GzippedBuffer);
GZipStream gs = new GZipStream(ms, CompressionMode.Decompress, true);
byte[] buffer = StreamtoByte(gs, -1);
gs.Close();
ms.Close();
gs.Dispose();
ms.Dispose();
return buffer;
}
///
/// Zips a Byte Array and returns the Gzipped result as a Byte Array
///
/// The Byte Array to be Gzipped
public static byte[] BytetoGZipByte(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true);
gs.Write(buffer, 0, buffer.Length);
gs.Close();
gs.Dispose();
return ms.ToArray();
}
#endregion
#region public static methods for files.
///
/// Unzips a Gzipped file and returns a Byte Array containing the unzipped data
///
/// The path to the gzip file
public static Byte[] GzipfiletoByte(string filepath)
{
FileStream infile;
byte[] buffer = new byte[0];
try
{
// Open the file as a FileStream object.
infile = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[infile.Length];
// Read the file to ensure it is readable.
int count = infile.Read(buffer, 0, buffer.Length);
infile.Close();
//unzip the buffer
buffer = GZipBytetoByte(buffer);
}
catch (System.Exception ex)
{
bool rethrow = ExceptionHelper.HandleException(ex);
if (rethrow) { throw ex; }
}
return buffer;
}
///
/// Zips a Byte Array using GZip and write the zipped data to a file
///
/// The path to the gzip file that will be created.
/// The byte array containing the data to be zipped
public static void BytetoGZipFile(string filepath, byte[] buffer)
{
// Takes an in memory buffer (byte[]) compresses it and write the result to a file.
FileStream outfile;
try
{
byte[] zipbyte = BytetoGZipByte(buffer);
// Open the file as a FileStream object.
outfile = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write);
outfile.Write(zipbyte, 0, zipbyte.Length);
outfile.Close();
}
catch (System.Exception ex)
{
bool rethrow = ExceptionHelper.HandleException(ex);
if (rethrow) { throw ex; }
}
}
#endregion
}
}