Use Sharpziplib to decompress files and folders

xiaoxiao2021-03-06  15

Main topic: Deliver 100 points and take out the use of SharPzip compression and unzipped actual experience to share with you! (Organized) author: CityHunter172 (Cold Yufeng) level: credit value: 100 Forum: .NET technology ASP.NET problem points: 100 Reply number: 104 Posted: 2003-10-21 10: 14: 03z

In this way, the Chinese name can be supported first, in http://www.icsharpcode.net/opensource/sharpziplib/default.aspx Download the source code, find "zipConstants.cs" to modify public static string convert7tring (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);} The following is the compression and decompression code I have written: use system; use 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 {///

/// 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 ");}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// Codegen: This call is required for the ASP.NET Web Form Designer. InitializeComponent (); base.onit (e);} ///

/// Designer Support required method - Do not use the code editor to modify the // / this method. /// private () {this.Load = new system.eventhandler (this.page_load);} # endRegion // Compressed files P for the client's back file list: file name compression package Name public void zipfile (string p) {string [] TMP = p.split (new char [] {'*'}; // Separate file list IF (TMP [Tmp.Length-1]! = "" " // Compressed package name is not empty {zipoutputstream u = new zipoutputstream (file.create (serverDir tmp [tmp.length)); // New compressed file stream "zipoutputstream" for (int i = 0; i < Tmp.Length-1; i ) {IF (TMP [i]! = "") // Separated file name is not empty {this.addzilentry (TMP [i], u, out u); / / compression Document stream add content}} u.finish (); // end compression u.close ();}}

// Add compression item: P is a compressed file or folder; u is an existing source zipoutputstream; Out J is "zipoutputstream" PUBLIC VOID ADDZIPOUTSTREAM (String P, Zipoutputstream U, Outzipoutputstream J) {String s = ServerDir P;

IF (Directory.exists (s)) // folder processing {DirectoryInfo Di = New DirectoryInfo (s);

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

Foreach (DirectoryInfo Tem in di.getdirectories ()) // Get subdirectory {zipentry z = new zipentry (this.shortdir (Tem.FullName) "//); //" // "for folders Tag u.putnextentry (z); // This sentence is essential, otherwise the empty directory will not be added s = this.shortdir; this.addzilename (S, U, OUT U); // Recurns} foreach (FILEINFO TEMP IN DI.GETFILES ()) // Gets files for this directory {s = this.shortdir (TEMP.FULLNAME); this.addzilenTry (S, U, OUT U); // Recurns}} else f . EXISTS (S)) // Document 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.shortdir (s)); u.putNextentry (z); / / provides a container U. .Write (b, 0, b.length); // Write byte f.close ();} j = u; // Returns "zipoutputstream"} public void unzipfile (string p) // Solution Compressed {string [] un_tmp = p.split (new char [] {'*'}; int i2 = 0; / / Prevent the parameter for the name conflict (int J = 0; j

A: ZIPENTRY ZP = f.getNextentry (); // Get projects in the decompressed file stream. Other Note (My Understand): Every file in the compressed package exists in a "zipentry", including the directory information of the file. If the empty directory is compressed, a "file" that will appear empty, size is 0, "CRC" attribute is 00000000. This file is just a tag, which is not unzipped. 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) related to the following 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 bytes FileStream S = file.create (un_dir "//" zp.name); // (b) - New file stream while // Continuous reading bytes until a "zipentry" byte Read {i = f.read (B, 0, B.Length); // Read the byte if (i> 0) {S.WRITE (B, 0, I); // will Bytes write new file streams} else {breaf; // read bytes are 0, jump out cycle}} S.close ();} goto a; // Go to the next "zipentry"} f.close ); I2 ;}}}}}

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

New Post(0)