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="PathAndFi leName">The full path and filename to a text
file</param>
/// <returns>The contents of the file</returns>
public static string ReadTextFile(st ring PathAndFileName )
{
System.IO.FileS tream objStream = System.IO.File. OpenRead(PathAn dFileName);
int intLen = ((int)((objStre am.Length > int.MaxValue) ? int.MaxValue :
objStream.Lengt h));
byte[] bytTxt = new byte[intLen];
objStream.Read( bytTxt, 0, intLen);
objStream.Flush ();
objStream.Close ();
return System.Text.ASC IIEncoding.ASCI I.GetString(byt Txt);
}
/// <summary>
/// Write text to a text file
/// </summary>
/// <param name="PathAndFi leName">The full path and filename to a text
file</param>
/// <param name="DataToWri te">The contents to write to the file</param>
/// <returns>The length of the file after the write</returns>
public static long WriteTextFile(s tring PathAndFileName , string DataToWrite)
{
byte[] bytTxt = System.Text.ASC IIEncoding.ASCI I.GetBytes(Data ToWrite);
System.IO.FileS tream objStream = System.IO.File. Create(PathAndF ileName);
objStream.Write (bytTxt, 0, bytTxt.Length);
objStream.Flush ();
objStream.Close ();
System.IO.FileI nfo f = new System.IO.FileI nfo(PathAndFile Name);
return f.Length;
}
/// <summary>
/// Write text to a zip file using GZip
/// </summary>
/// <param name="PathAndFi lename">The full path and filename to a
file</param>
/// <param name="DataToWri te">The contents to write to the file</param>
/// <returns>The length of the file after the write</returns>
public static long WriteZipFile(st ring PathAndFilename , string DataToWrite)
{
byte[] bytTxt = System.Text.ASC IIEncoding.ASCI I.GetBytes(Data ToWrite);
System.IO.FileS tream objStream = System.IO.File. Create(PathAndF ilename);
System.IO.Compr ession.GZipStre am objZS = new
System.IO.Compr ession.GZipStre am(objStream,
System.IO.Compr ession.Compress ionMode.Compres s);
objZS.Write(byt Txt, 0, bytTxt.Length);
objZS.Flush();
objZS.Close();
System.IO.FileI nfo f = new System.IO.FileI nfo(PathAndFile name);
return f.Length;
}
/// <summary>
/// Read text from a GZipped text file
/// </summary>
/// <param name="PathAndFi leName">The full path and filename to a
file</param>
/// <returns>The contents of the file</returns>
public static string ReadZipFile(str ing PathAndFileName )
{
System.IO.FileS tream objStream = System.IO.File. OpenRead(PathAn dFileName);
System.IO.Compr ession.GZipStre am objZS = new
System.IO.Compr ession.GZipStre am(objStream,
System.IO.Compr ession.Compress ionMode.Decompr ess);
int intLen = ((int)((objStre am.Length > int.MaxValue) ? int.MaxValue :
objStream.Lengt h));
byte[] bytTxt = new byte[intLen];
objZS.Read(bytT xt, 0, intLen);
objZS.Flush();
objZS.Close();
return System.Text.ASC IIEncoding.ASCI I.GetString(byt Txt);
}
/// <summary>
/// Write text to a file using Deflate
/// </summary>
/// <param name="PathAndFi lename">The full path and filename to a
file</param>
/// <param name="DataToWri te"></param>
/// <returns>The length of the file after contents have been
written</returns>
public static long WriteDeflateFil e(string PathAndFilename , string
DataToWrite)
{
byte[] bytTxt = System.Text.ASC IIEncoding.ASCI I.GetBytes(Data ToWrite);
System.IO.FileS tream objStream = System.IO.File. Create(PathAndF ilename);
System.IO.Compr ession.DeflateS tream objZS = new
System.IO.Compr ession.DeflateS tream(objStream ,
System.IO.Compr ession.Compress ionMode.Compres s);
objZS.Write(byt Txt, 0, bytTxt.Length);
objZS.Flush();
objZS.Close();
System.IO.FileI nfo f = new System.IO.FileI nfo(PathAndFile name);
return f.Length;
}
/// <summary>
/// Read text from a Deflated text file
/// </summary>
/// <param name="PathAndFi leName">The full path and filename to a
file</param>
/// <returns>The contents of the file</returns>
public static string ReadDeflateFile (string PathAndFileName )
{
System.IO.FileS tream objStream = System.IO.File. OpenRead(PathAn dFileName);
System.IO.Compr ession.DeflateS tream objZS = new
System.IO.Compr ession.DeflateS tream(objStream ,
System.IO.Compr ession.Compress ionMode.Decompr ess);
int intLen = ((int)((objStre am.Length > int.MaxValue) ? int.MaxValue :
objStream.Lengt h));
byte[] bytTxt = new byte[intLen];
objZS.Read(bytT xt, 0, intLen);
objZS.Flush();
objZS.Close();
return System.Text.ASC IIEncoding.ASCI I.GetString(byt Txt);
}
}
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:nb******** ***********@new s4.e.nsc.no...
That is awesome!
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Dennis Myrén <de****@oslokb. no> wrote:V2.0 contains some support, although I don't know what it's like
offhand. That is very interesting.
Would you mind telling me from where you have got that information?
http://msdn2.microsoft.com/library/S...mpression.aspx
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too