Practical experience in sharpzip compression and decompression

Source: Internet
Author: User
This article was originally a post I posted on the csdn Forum (http://search.csdn.net/expert/topicview1.asp? Id = 2377844), and now it is "Shun" (whole) into a chapter.

 


First, download the source code in the http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx, find the "zipconstants. cs" Change

Public static string converttostring (byte [] data)
{
Return encoding. getencoding ("gb2312"). getstring (data, 0, Data. Length );
// Return encoding. ASCII. getstring (data, 0, Data. Length );
}

Public static byte [] converttoarray (string Str)
{
Return encoding. getencoding ("gb2312"). getbytes (STR );
// Return encoding. ASCII. getbytes (STR );
}

Chinese names are supported.
The following is my compressed and decompressed code:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using icsharpcode. sharpziplib. Zip;

Namespace OA
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Public String serverdir;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
This. serverdir = page. mappath (".");
This. zipfile ("01.txt * 02.txt * 000.zip"); // It is just an example. You can implement it yourself.
This. zipfile ("01.txt * 02.txt * 001.zip ");
This. unzipfile ("000.zip * 001.zip ");
}

Public String includir (string S)
{
// Convert the absolute path of the object to the relative path
String d = S. Replace (serverdir ,"");
Return D;
}

// Compressed file P is the list of files uploaded by the client: file name + compressed package name
Public void zipfile (string P)
{
String [] TMP = P. Split (New char [] {'*'}); // list of Separated Files
If (TMP [TMP. Length-1]! = "") // The compressed package name is not blank
{
Zipoutputstream u = new zipoutputstream (file. Create (serverdir + TMP [TMP. Length-1]); // create a compressed file stream "zipoutputstream"
For (INT I = 0; I <TMP. Length-1; I ++)
{
If (TMP [I]! = "") // The separated file name is not empty.
{
This. addzipentry (TMP [I], U, out U); // add content to the compressed file stream
}
}
U. Finish (); // end Compression
U. Close ();
}
}

// Add a compression project: P is the file or folder to be compressed; U is the existing source zipoutputstream; out J is the "zipoutputstream" that has been added with "zipentry"
Public void addzipentry (string P, zipoutputstream U, out zipoutputstream J)
{
String S = serverdir + P;

If (directory. exists (s) // process the folder
{
Directoryinfo di = new directoryinfo (s );

// *********** The following content is added after the amendment ***********

If (Di. getdirectories (). Length <= 0) // No subdirectory
{
Zipentry z = new zipentry (p + "\"); // The End "\" is used for folder tag
U. putnextentry (z );
}

// ************** The above content is added after the revision ***************

Foreach (directoryinfo TEM in Di. getdirectories () // obtain the subdirectory
{
Zipentry z = new zipentry (this. Partial Dir (TEM. fullname) + "\"); // The End "\" is used for folder tag
U. putnextentry (z); // This sentence is indispensable; otherwise, the empty directory will not be added.
S = This. Partial Dir (TEM. fullname );
This. addzipentry (S, U, out U); // Recursion
}
Foreach (fileinfo temp in Di. getfiles () // get the files in this directory
{
S = This. Partial Dir (temp. fullname );
This. addzipentry (S, U, out U); // Recursion
}
}
Else if (file. exists (s) // File Processing
{
U. setlevel (9); // compression level
Filestream F = file. openread (s );
Byte [] B = new byte [F. Length];
F. Read (B, 0, B. Length); // Add the file stream to the buffer byte
Zipentry z = new zipentry (this. Partial Dir (s ));
U. putnextentry (z); // provides a container for the compressed file stream
U. Write (B, 0, B. Length); // write bytes
F. Close ();
}
J = u; // return the "zipoutputstream" of the added data"
}

Public void unzipfile (string p) // Extract
{
String [] un_tmp = P. Split (New char [] {'*'});
Int I2 = 0; // a parameter that prevents Name Conflict
For (Int J = 0; j <un_tmp.length; j ++)
{
If (un_tmp [J]! = "")
{
String un_time = system. datetime. now. toshortdatestring () + "-" + system. datetime. now. hour. tostring () + "-" + system. datetime. now. minute. tostring () + "-" + (system. datetime. now. second + I2 ). tostring ();
String un_dir = serverdir + "Unzip-" + un_time;
Directory. createdirectory (un_dir); // create a folder named after the extract time
Zipinputstream F = new zipinputstream (file. openread (serverdir + un_tmp [J]); // read the compressed file and use this file stream to create a "zipinputstream" object

A: zipentry ZP = f. getnextentry (); // obtain the project in the decompressed file stream. Note (my understanding): Each file in the compressed package exists in the form of "zipentry", including the directory information for storing the file. If the empty directory is compressed, a "file" named empty, with a size of 0 and a CRC attribute of 00000000 will appear in the directory ". This file is only a tag and will not be decompressed.

While (ZP! = NULL)
{
String un_tmp2;
If (ZP. Name. indexof ("\")> = 0) // obtain the directory information of the object
{
Int tmp1 = ZP. Name. lastindexof ("\\");
Un_tmp2 = ZP. Name. substring (0, tmp1 );
Directory. createdirectory (un_dir + "\" + un_tmp2 + "\"); // you must first create a directory; otherwise, decompression fails --- (a) is related to the following steps (B)
}
If (! ZP. isdirectory & ZP. CRC! = 00000000l) // This "zipentry" is not "Tag file"
{
Int I = 2048;
Byte [] B = new byte [I]; // buffer 2048 bytes each time
Filestream S = file. Create (un_dir + "\" + ZP. Name); // (B)-create a file stream
While (true) // read bytes continuously until a "zipentry" byte is read
{
I = f. Read (B, 0, B. Length); // read the byte in "zipentry"
If (I> 0)
{
S. Write (B, 0, I); // write bytes to the new file stream
}
Else
{
Break; // The read byte is 0.
}
}
S. Close ();
}
Goto A; // enter the next "zipentry"
}
F. Close ();
I2 ++;
}
}
}
}
}

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 267150

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.