I wrote this up a while ago, includes sample code:
How to use the compression routines in Visual Studio 2005
This is sort of an impractical example, but I whipped these up to see the
comparison between clear-text, gzip and deflate. This is a bit lame
because
the Write() function takes an integer, so these can only read/write as
many
bytes as an integer can hold.
Anyhow, if nothing else, these are quick, clean examples of how to
read/write zipped files - which can compress a text file upto 98% or 40:1
public class FileOper
{
/// <summary>
/// Private because all members are static
/// </summary>
private FileOper()
{
}
/// <summary>
/// Read the entire contents of a text file
/// </summary>
/// <param name="PathAndFileName">The full path and filename to a text
file</param>
/// <returns>The contents of the file</returns>
public static string ReadTextFile(string PathAndFileName)
{
System.IO.FileStream objStream = System.IO.File.OpenRead(PathAndFileName);
int intLen = ((int)((objStream.Length > int.MaxValue) ? int.MaxValue :
objStream.Length));
byte[] bytTxt = new byte[intLen];
objStream.Read(bytTxt, 0, intLen);
objStream.Flush();
objStream.Close();
return System.Text.ASCIIEncoding.ASCII.GetString(bytTxt);
}
/// <summary>
/// Write text to a text file
/// </summary>
/// <param name="PathAndFileName">The full path and filename to a text
file</param>
/// <param name="DataToWrite">The contents to write to the file</param>
/// <returns>The length of the file after the write</returns>
public static long WriteTextFile(string PathAndFileName, string
DataToWrite)
{
byte[] bytTxt = System.Text.ASCIIEncoding.ASCII.GetBytes(DataToWrite);
System.IO.FileStream objStream = System.IO.File.Create(PathAndFileName);
objStream.Write(bytTxt, 0, bytTxt.Length);
objStream.Flush();
objStream.Close();
System.IO.FileInfo f = new System.IO.FileInfo(PathAndFileName);
return f.Length;
}
/// <summary>
/// Write text to a zip file using GZip
/// </summary>
/// <param name="PathAndFilename">The full path and filename to a
file</param>
/// <param name="DataToWrite">The contents to write to the file</param>
/// <returns>The length of the file after the write</returns>
public static long WriteZipFile(string PathAndFilename, string
DataToWrite)
{
byte[] bytTxt = System.Text.ASCIIEncoding.ASCII.GetBytes(DataToWrite);
System.IO.FileStream objStream = System.IO.File.Create(PathAndFilename);
System.IO.Compression.GZipStream objZS = new
System.IO.Compression.GZipStream(objStream,
System.IO.Compression.CompressionMode.Compress);
objZS.Write(bytTxt, 0, bytTxt.Length);
objZS.Flush();
objZS.Close();
System.IO.FileInfo f = new System.IO.FileInfo(PathAndFilename);
return f.Length;
}
/// <summary>
/// Read text from a GZipped text file
/// </summary>
/// <param name="PathAndFileName">The full path and filename to a
file</param>
/// <returns>The contents of the file</returns>
public static string ReadZipFile(string PathAndFileName)
{
System.IO.FileStream objStream = System.IO.File.OpenRead(PathAndFileName);
System.IO.Compression.GZipStream objZS = new
System.IO.Compression.GZipStream(objStream,
System.IO.Compression.CompressionMode.Decompress);
int intLen = ((int)((objStream.Length > int.MaxValue) ? int.MaxValue :
objStream.Length));
byte[] bytTxt = new byte[intLen];
objZS.Read(bytTxt, 0, intLen);
objZS.Flush();
objZS.Close();
return System.Text.ASCIIEncoding.ASCII.GetString(bytTxt);
}
/// <summary>
/// Write text to a file using Deflate
/// </summary>
/// <param name="PathAndFilename">The full path and filename to a
file</param>
/// <param name="DataToWrite"></param>
/// <returns>The length of the file after contents have been
written</returns>
public static long WriteDeflateFile(string PathAndFilename, string
DataToWrite)
{
byte[] bytTxt = System.Text.ASCIIEncoding.ASCII.GetBytes(DataToWrite);
System.IO.FileStream objStream = System.IO.File.Create(PathAndFilename);
System.IO.Compression.DeflateStream objZS = new
System.IO.Compression.DeflateStream(objStream,
System.IO.Compression.CompressionMode.Compress);
objZS.Write(bytTxt, 0, bytTxt.Length);
objZS.Flush();
objZS.Close();
System.IO.FileInfo f = new System.IO.FileInfo(PathAndFilename);
return f.Length;
}
/// <summary>
/// Read text from a Deflated text file
/// </summary>
/// <param name="PathAndFileName">The full path and filename to a
file</param>
/// <returns>The contents of the file</returns>
public static string ReadDeflateFile(string PathAndFileName)
{
System.IO.FileStream objStream = System.IO.File.OpenRead(PathAndFileName);
System.IO.Compression.DeflateStream objZS = new
System.IO.Compression.DeflateStream(objStream,
System.IO.Compression.CompressionMode.Decompress);
int intLen = ((int)((objStream.Length > int.MaxValue) ? int.MaxValue :
objStream.Length));
byte[] bytTxt = new byte[intLen];
objZS.Read(bytTxt, 0, intLen);
objZS.Flush();
objZS.Close();
return System.Text.ASCIIEncoding.ASCII.GetString(bytTxt);
}
}
Dennis Myrén said:
That is awesome!
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
http://msdn2.microsoft.com/library/System.IO.Compression.aspx