Zip Libraries in Framework 1.1

  • Thread starter Thread starter BuddyWork
  • Start date Start date
B

BuddyWork

Hello,

Are there a class within the Framework that support zip
which Microsoft has written?

I'm aware of the open source SharpLibZip.

Thanks
 
BuddyWork said:
Are there a class within the Framework that support zip
which Microsoft has written?

I'm aware of the open source SharpLibZip.

There's nothing in V1.0 or V1.1. V2.0 contains some support, although I
don't know what it's like offhand.
 
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?
 
Hi,

Nop, nothing.

Use SharpLibZip, it's the best thatI know of.


Cheers,
 
Well, that was a stupid question.
I guess it is in the BETA.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
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?
 
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);

}

}
 
Thank you Drebin.
Currently i am using SharpZipLib but (with all respect to SharpZipLib)
built-in compression routines are preferred.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Drebin said:
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
 
Dennis Myrén said:
Currently i am using SharpZipLib but (with all respect to SharpZipLib)
built-in compression routines are preferred.

Looking at the docs Jon linked to and the code Drebin posted, it doesn't
seem directly comparable. SharpZipLib as I recall, creates proper Zip
files, just like PKZip or Winzip (multiple files with internal table of
contents). The framework's functions appear to just handle the compressing
of text (which, granted, is the hard part, but still leave you some work to
do).

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Correct, the compression stuff in 2.0 is for compressing actual files only,
NOT folders.. and you can't open these files with something like WinZip..
 
James Curran said:
Looking at the docs Jon linked to and the code Drebin posted, it doesn't
seem directly comparable. SharpZipLib as I recall, creates proper Zip
files, just like PKZip or Winzip (multiple files with internal table of
contents). The framework's functions appear to just handle the compressing
of text (which, granted, is the hard part, but still leave you some work to
do).

I don't see anything text-specific there - it's all stream-based as far
as I can see.

You're right that it doesn't deal with zip files though. Personally
that isn't an issue for me, but it looks like SharpZipLib will still be
useful for some people.

For those interested in binary diffs, by the way, I recently
implemented a VCDIFF decoder. The combination of xdelta3 (or a similar
VCDIFF compressor) and then gzip gives a really good way of
distributing updates in bandwidth-limited situations. The VCDIFF
decoder is available at
http://www.pobox.com/~skeet/csharp/miscutil
 
James Curran said:
Yes, that's correct. The "text" in my message was a misspeak. I'm not
sure what I meant there, but "text" wasn't it. "Content" is probably more
accurate, or, I guess "data".

Right - no problem.

Thinking about it, while it could be confusing, I'm not even sure
you're wrong to call it text within the vocabular of compression.
Certainly in encryption the original message is "plain-text" whether
it's encrypted or not.

Anyway, we all know what we're talking about :)
 
Actually, in my particular case, all i need is the core data
compressor/decompressor.
I use it when generating/parsing binary file fragments rather than
compressing existing files or folders
into ZIP libraries.

But of course, still i surely would like all the features of SharpZipLib
to be provided by System.IO.Compression.
 
Back
Top