Use SharPzip compression and unzipped actual combat experience

xiaoxiao2021-03-06  37

Use SharPzip compression and unzipped actual combat experience

Author: Han Yu Feng This article originally I 9CBS the forum of a post (http://search.9cbs.net/expert/topicview1.asp?id=2377844), now it "smooth" (full) Honest.

First of all,

http://www.icsharpcode.net/opensource/sharpziplib/default.aspx

Download the source code, find "zipConstants.cs" to modify public static string converTring (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);}

This supports Chinese names the following is the compression and decompression code I write: use system; use system.collections; use system.componentmodel; use system.data; use system.drawing; useing system.Web; use system. Web.SessionState; use system.web.ui; use system.web.ui.webcontrols; using system.web.ui.htmlcontrols; using iCsharpcode.sharpziplib.zip;

Namespace OA {///

/// WebForm1 summary description. /// 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 page this.serverdir = Page.mappath ("."); This.zipfile ("01.txt * 02.txt * 000.zip"); // is just example, the specific everyone will realize this.zipfile ("01.txt * 02 .txt * 001.zip "); this.unzipfile (" 000.zip * 001.zip ");} public string shortdir (String s) {// turn the absolute path of the file to a relative path String D = S.Replace (ServerDir, ""); return d;}

// Compressed file P lists a list of files received back: file name Compressed package name public void zipfile (string p) {string [] TMP = P.Split (new char [] {'*'}; / / Separation file list IF (TMP [Tmp.Length-1]! = "" // Compressed package name is not empty {zipoutputstream u = new zipoutputstream (file.create (serverdir tmp [tmp.length-1]))) ; // New compressed file stream "zipoutputstream" for (int i = 0; i

IF (Directory.exists (s)) // folder processing {DirectoryInfo di = new DirectoryInfo (s); // *********** The following content is added after the addition of the ***** ******

IF (di.getdirectories (). Length <= 0) // No subdirectory {zipentry z = new zipentry (p "//"); // Last "//" for folder U.putNextentry (z }

// ************** The above is amended after the addition of the addition of *************** foreach (DirectoryInfo Tem in Di.GetDirectories ()) // Get subdirectory {zipenTry z = new zipentry (this.shortdir (Tem.fullName) "//); //" // "for the folder U.putNextentry (// Not less, otherwise the empty directory will not be added s = this.shortdir; this.addzilenTry (S, U, OUT U); // Recursive} foreach (fileInfo temp in di.getfiles) / / Get this directory file {s = this.shortdir (TEMP.FULLNAME); this.addzilenTry (s, u, out u); // recursive}} Else If (file.exists (s)) // file processing {U.SetLevel (9); // Compressive Level FILESTREAM F = File.openread (s); byte [] b = new byte [f.Length]; F.Read (b, 0, b.length); // Add file stream to the buffer byte zipentry z = new ziPentry (this .Shortdir (s)); u.putnextentry (z); // provides a container U.WRITE (B, 0, B.Length); // writes byte f.close ();} j = u; // Returns "zipoutputstream"} added to the added data

Public void unzipfile (String P) // Unzip {string [] un_tmp = p.split (new char [] {'*'}; int i2 = 0; / / Prevent Name conflict parameters for (int J = 0 J

While (zp! = null) {string un_TMP2; if (zp.name.indexof ("

// ")> = 0

) // Get the directory information of the file {INT TMP1 = zp.name.lastIndexof ("//"); un_tmp2 = zp.name.substring (0, TMP1); Directory.createdIRectory (UN_DIR "

// " un_TMP2 " //

"); // must first create a directory, otherwise unzip failed --- (a) is related to step (b)} if (! Zp.isdirectory && zp.crc! = 00000000L) // This" ZiPentry "is not" tag file "{INT i = 2048; Byte [] b = new byte [i]; // Each time buffer 2048 byte fileStream s = file.create (un_dir " // " zp.name

转载请注明原文地址:https://www.9cbs.com/read-64779.html

New Post(0)