Practical experience in using Sharpzip compression and decompression

Source: Internet
Author: User
Compression first, in http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx download source code, find "ZipConstants.cs" modified

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



So you can support the Chinese name.
Here's what I wrote about the compression and decompression 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 description of the 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 page
This. Serverdir = Page.mappath (".");
This. ZipFile ("01.txt*02.txt*000.zip"); Just examples, the specific people to achieve
This. ZipFile ("01.txt*02.txt*001.zip");
This. Unzipfile ("000.zip*001.zip");
}


public string Shortdir (string s)
{
To convert a file's absolute path to a relative path
String D=s.replace (Serverdir, "");
return D;
}



Compressed file p Returns a list of files for the client: filename + name of the compressed package
public void ZipFile (string p)
{
string[] tmp = p.split (New char[]{' * '}); Detach 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<tmp. length-1;i++)
{
if (tmp[i]!= "")//separated file name is not empty
{
This. Addzipentry (Tmp[i],u,out u); Adding content to a compressed file stream
}
}
U.finish (); End compression
U.close ();
}
}



Add Compression item: p for files or folders that need to be compressed; U for existing source Zipoutputstream out J for "Zipoutputstream" added "ZipEntry"
public void Addzipentry (string p,zipoutputstream u,out Zipoutputstream j)
{
string S =serverdir+p;

if (directory.exists (s))//folder processing
{
DirectoryInfo di = new DirectoryInfo (s);

The following is the *********** added after revision

if (di. GetDirectories (). length<=0)//No subdirectories
{
ZipEntry z = new ZipEntry (p+ "\"); End "\" is used for folder tags
U.putnextentry (z);
}

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


foreach (DirectoryInfo tem in DI. GetDirectories ())//Get subdirectories
{
ZipEntry z = new ZipEntry (this. Shortdir (TEM. FullName) + "\ \"); End "\" is used for folder tags
U.putnextentry (z); This sentence must not be small, otherwise the empty directory will not be added
s = this. Shortdir (TEM. FullName);
This. Addzipentry (S,u,out u); Recursion
}
foreach (FileInfo temp in di. GetFiles ())//Get files for this directory
{
s = this. Shortdir (temp. FullName);
This. Addzipentry (S,u,out u); Recursion
}
}
else if (file.exists (s))//File processing
{



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.