C # about ZIP compression decompression help class package with source download _c# Tutorial

Source: Internet
Author: User
C # Compression decompression, mainly with the third party class library for encapsulation. ICSharpCode.SharpZipLib.dll class Library, link address for your official download link. Compression is mainly compressed in a streaming manner.

Compress files and folders. File compression is simple, the file to be compressed is read into memory in a stream, and then placed in the compressed stream. It's OK. folder is a bit of a hassle. Because you want to compress the folder to be compressed after decompression to retain the folder file hierarchy. So my implementation is to recursively traverse the files in the folder. calculates its relative position in a compressed stream.

The code is as follows

Copy Code code as follows:

<summary>
Compress files or folders
</summary>
<param name= "_depositpath" > compressed file storage path such as C:\\windows\abc.zip</param>
<returns></returns>
public bool Compressionzip (string _depositpath)
{
bool result = true;
FileStream fs = null;
Try
{
Zipoutputstream Comstream = new Zipoutputstream (File.create (_depositpath));
Comstream.setlevel (9); Compression level
foreach (string path in absolutepaths)
{
If it is a directory
if (directory.exists (path))
{
Zipfloder (path, comstream, path);
}
else if (file.exists (path))//If the file
{
FS = File.openread (path);
byte[] BTS = new BYTE[FS. Length];
Fs. Read (BTS, 0, BTS. Length);
ZipEntry ze = new ZipEntry (new FileInfo (path). Name);
Comstream.putnextentry (Ze); Provides a container for compressed file streams
Comstream.write (BTS, 0, BTS.  Length); Write bytes
}
}
Comstream.finish (); End compression
Comstream.close ();
}
catch (Exception ex)
{
if (fs!= null)
{
Fs. Close ();
}
ErrorMsg = ex. message;
result = false;
}
return result;
}
Compressed folder
private void Zipfloder (String _ofloderpath, Zipoutputstream Zos, String _floderpath)
{
foreach (FileSystemInfo item in New DirectoryInfo (_floderpath). Getfilesysteminfos ())
{
if (directory.exists (item). FullName))
{
Zipfloder (_ofloderpath, Zos, item. FullName);
}
else if (file.exists (item). FullName))//If it is a file
{
DirectoryInfo odir = new DirectoryInfo (_ofloderpath);
String fullName2 = new FileInfo (item. FullName). FullName;
String path = Odir.name + fullname2.substring (ODir.FullName.Length, fullname2.length-odir.fullname.length);//Get relative directory
FileStream fs = File.openread (fullName2);
byte[] BTS = new BYTE[FS. Length];
Fs. Read (BTS, 0, BTS. Length);
ZipEntry ze = new ZipEntry (path);
Zos.             Putnextentry (Ze); Provides a container for compressed file streams
Zos. Write (BTS, 0, BTS.  Length); Write bytes
}
}
}

About decompression decompression is much simpler. There are files extracted files, there is a folder traversal, extract the files in them. The extracted file already contains a hierarchical relationship with the folder.

Copy Code code as follows:

<summary>
Extract
</summary>
<param name= "_depositpath" > Compressed file path </param>
<param name= "_floderpath" > Decompression path </param>
<returns></returns>
public bool Decompressionzip (string _depositpath, String _floderpath)
{
bool result = true;
FileStream Fs=null;
Try
{
Zipinputstream Inpstream = new Zipinputstream (File.openread (_depositpath));
ZipEntry ze = inpstream.getnextentry ()//Get every file in the compressed file
Directory.CreateDirectory (_floderpath);//Create Unzip folder
while (ze!= null)//If the extract is finished ze is null
{
if (Ze. Isfile)//compressed zipinputstream inside the file is stored. File with folder name is folder \ file name
{
String[] Strs=ze. Name.split (' \ \ ');//If the file name contains ' \ \ ' It indicates a folder
if (STRs. Length > 1)
{
Two-tier loops for creating folders on one level
for (int i = 0; i < STRs. Length-1; i++)
{
String Floderpath=_floderpath;
for (int j = 0; J < i; J + +)
{
Floderpath = Floderpath + "\" + strs[j];
}
floderpath=floderpath+ "\" +strs[i];
Directory.CreateDirectory (Floderpath);
}
}
FS = new FileStream (_floderpath+ "\" +ze). Name, FileMode.OpenOrCreate, FileAccess.Write);//Create File
Looping through files to the file stream
while (true)
{
byte[] BTS = new byte[1024];
int i= inpstream.read (BTS, 0, BTS. Length);
if (i > 0)
{
Fs. Write (BTS, 0, I);
}
Else
{
Fs. Flush ();
Fs. Close ();
Break
}
}
}
Ze = Inpstream.getnextentry ();
}
}
catch (Exception ex)
{
if (fs!= null)
{
Fs. Close ();
}
ErrorMsg = ex. message;
result = false;
}
return result;
}

Finally, make a summary. C # As a high-level language, its powerful class libraries and third-party-provided class libraries. Can do a lot of things. But also has the disadvantage, uses the third party class library performance is not very high. I compress something for hundreds of M. The CPU ran to more than 50% seconds. Is far worse than the 360 compression and zip compression performance. So this class also applies to compress relatively small things.

Complete example Download address

Related Article

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.