首先,在 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); //此句不可少,否则空目录不会被添加
s = this.shortdir(tem.fullname);
this.addzipentry(s,u,out u); //递归
}
foreach(fileinfo temp in di.getfiles()) //获取此目录的文件
{
s = this.shortdir(temp.fullname);
this.addzipentry(s,u,out u); //递归
}
}
else if(file.exists(s)) //文件的处理
{
u.setlevel(9); //压缩等级
filestream f = file.openread(s);
byte[] b = new byte[f.length];
f.read(b,0,b.length); //将文件流加入缓冲字节中
zipentry z = new zipentry(this.shortdir(s));
u.putnextentry(z); //为压缩文件流提供一个容器
u.write(b,0,b.length); //写入字节
f.close();
}
j=u; //返回已添加数据的“zipoutputstream”
}
public void unzipfile(string p) //解压缩
{
string[] un_tmp = p.split(new char[]{*});
int i2=0; //防止名称冲突的参数
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); //创建以解压时间为名称的文件夹
zipinputstream f = new zipinputstream(file.openread(serverdir+un_tmp[j])); //读取压缩文件,并用此文件流新建 “zipinputstream”对象
a: zipentry zp = f.getnextentry(); //获取解压文件流中的项目。 另注(我的理解):在压缩包里每个文件都以“zipentry”形式存在,其中包括存放文件的目录信息。如果空目录被压缩,该目录下将出现一个名称为空、大小为 0 、“crc”属性为 00000000 的“文件”。此文件只是个标记,不会被解压。
while(zp!=null)
{
string un_tmp2;
if(zp.name.indexof("\\")>=0) //获取文件的目录信息
{
int tmp1 = zp.name.lastindexof("\\");
un_tmp2 = zp.name.substring(0,tmp1);
directory.createdirectory(un_dir+"\\"+un_tmp2+"\\"); //必须先创建目录,否则解压失败 — (a) 关系到下面的步骤(b)
}
if(!zp.isdirectory&&zp.crc!=00000000l) //此“zipentry”不是“标记文件”
{
int i =2048;
byte[] b = new byte[i]; //每次缓冲 2048 字节
filestream s= file.create(un_dir+"\\"+zp.name); //(b)-新建文件流
while(true) //持续读取字节,直到一个“zipentry”字节读完
{
i = f.read(b,0,b.length); //读取“zipentry”中的字节
if(i>0)
{
s.write(b,0,i); //将字节写入新建的文件流
}
else
{
break; //读取的字节为 0 ,跳出循环
}
}
s.close();
}
goto a; //进入下一个“zipentry”
}
f.close();
i2++;
}
}
}
}
}
