Files are often compressed and decompressed when a. NET Windows application or ASP. NET is released. However, related class libraries are not provided in Microsoft Framework 1.1. This
ICSharpCode can be referenced to quickly achieve the relevant purpose. This class library is developed in C # And is open-source. If you are interested, you can download it from here.
1. File compression
Add the downloaded ICSharpCode. SharpZipLib. dll to the reference and create a class named ZipClass (ZipClass. cs)
Using System;
Using System. IO;
Using ICSharpCode. SharpZipLib. Zip;
Namespace JohnSolution. Common
{
/** // <Summary>
/// ZipClass File compression class
/// </Summary>
Public class ZipClass
{
Public void StartZip ()
{
// Directory of the file to be compressed
String [] filenames = Directory. GetFiles (@ "e: \ test ");
// Generated file path
ZipOutputStream s = new ZipOutputStream (File. Create (@ "e: \ test.zip "));
S. SetLevel (5 );
Foreach (string file in filenames)
{
FileStream fs = File. OpenRead (file );
Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );
ZipEntry entry = new ZipEntry (file );
S. PutNextEntry (entry );
S. Write (buffer, 0, buffer. Length );
}
S. Finish ();
S. Close ();
}
}
}
1. decompress the file
Add the downloaded ICSharpCode. SharpZipLib. dll to the reference and create an UnZipClass class (UnZipClass. cs)
Using System;
Using System. IO;
Using System. Text;
Using ICSharpCode. SharpZipLib. Zip;
Namespace JohnSolution. Common
{
/** // <Summary>
/// UnZipClass decompress the file class
/// </Summary>
Public class UnZipClass
{
Public void StartUnZip ()
{
ZipInputStream s = new ZipInputStream (File. OpenRead (@ "e: \ test.zip "));
ZipEntry entry;
While (entry = s. GetNextEntry ())! = Null)
{
Int size = 2048;
Byte [] buffer = new byte [2048];
FileStream unZipFile = File. Create (@ "e: \" + entry. Name );
While (true)
{
Size = s. Read (buffer, 0, buffer. Length );
If (size> 0)
{
UnZipFile. Write (buffer, 0, size );
}
Else
{
Break;
}
}
UnZipFile. Flush ();
UnZipFile. Close ();
}
S. Close ();
}
}
}