日期:2011-11-26  浏览次数:21163 次

首先,在 http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 下载源码,找到“ZipConstants.cs”修改

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



如此就可支持中文名称了
以下是我写的压缩与解压缩的代码:

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>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
public string ServerDir;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.ServerDir = Page.MapPath(".");
this.ZipFile("01.txt*02.txt*000.zip"); //只是示例,具体的大家自己去实现
this.ZipFile("01.txt*02.txt*001.zip");
this.UnZipFile("000.zip*001.zip");
}


public string ShortDir(string s)
{
//将文件的绝对路径转为相对路径
string d=s.Replace(ServerDir,"");
return d;
}



//压缩文件 p 为客户端传回来的文件列表:文件名+压缩包的名称
public void ZipFile(string p)
{
string[] tmp = p.Split(new char[]{'*'}); //分离文件列表
if(tmp[tmp.Length-1]!="") //压缩包名称不为空
{
ZipOutputStream u = new ZipOutputStream(File.Create(ServerDir+tmp[tmp.Length-1])); //新建压缩文件流 “ZipOutputStream”
for(int i =0;i<tmp.Length-1;i++)
{
if(tmp[i]!="") //分离出来的文件名不为空
{
this.AddZipEntry(tmp[i],u,out u); //向压缩文件流加入内容
}
}
u.Finish(); // 结束压缩
u.Close();
}
}



//添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream; out j为已添加“ZipEntry”的“ZipOutputStream”
public void AddZipEntry(string p,ZipOutputStream u,out ZipOutputStream j)
{
string s =ServerDir+p;

if(Directory.Exists(s)) //文件夹的处理
{
DirectoryInfo di = new DirectoryInfo(s);

//***********以下内容是修订后添加的***********

if(di.GetDirectories().Length<=0) //没有子目录
{
ZipEntry z = new ZipEntry(p+"\\"); //末尾“\\”用于文件夹的标记
u.PutNextEntry(z);
}

//***************以上内容是修订后添加的***************


foreach(DirectoryInfo tem in di.GetDirectories()) //获取子目录
{
ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName)+"\\"); //末尾“\\”用于文件夹的标记
u.PutNextEntry(z