. NET File compression and decompression

Source: Internet
Author: User

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 ();

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.