首先,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx下载源码,找到“ZipConstants.cs”修改
publicstaticstringConvertToString(byte[] data)
{
returnEncoding.GetEncoding("gb2312").GetString(data, 0, data.Length);
//return Encoding.ASCII.GetString(data,0, data.Length);
}
publicstaticbyte[] ConvertToArray(stringstr)
{
returnEncoding.GetEncoding("gb2312").GetBytes(str);
//return Encoding.ASCII.GetBytes(str);
}
如此就可支持中文名称了
以下是我写的压缩与解压缩的代码:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingICSharpCode.SharpZipLib.Zip;
namespace OA
{
/// <summary>
///WebForm1 的摘要说明。
/// </summary>
publicclass WebForm1 : System.Web.UI.Page
{
publicstringServerDir;
privatevoidPage_Load(objectsender, 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");
}
publicstringShortDir(strings)
{
//将文件的绝对路径转为相对路径
stringd=s.Replace(ServerDir,"");
returnd;
}
//压缩文件 p 为客户端传回来的文件列表:文件名+压缩包的名称
publicvoidZipFile(stringp)
{
string[] tmp = p.Split(newchar[]{'*'});//分离文件列表
if(tmp[tmp.Length-1]!="")//压缩包名称不为空
{
ZipOutputStream u =newZipOutputStream(File.Create(ServerDir+tmp[tmp.Length-1]));//新建压缩文件流 “ZipOutputStream”
for(inti =0;i<tmp.Length-1;i++)
{
if(tmp[i]!="")//分离出来的文件名不为空
{
this.AddZipEntry(tmp[i],u,outu);//向压缩文件流加入内容
}
}
u.Finish();// 结束压缩
u.Close();
}
}
//添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream;outj为已添加“ZipEntry”的“ZipOutputStream”
publicvoidAddZipEntry(stringp,ZipOutputStream u,outZipOutputStream j)
{
strings =ServerDir+p;
if(Directory.Exists(s))//文件夹的处理
{
DirectoryInfo di =newDirectoryInfo(s);
//***********以下内容是修订后添加的***********
if(di.GetDirectories().Length<=0)//没有子目录
{
ZipEntry z =newZipEntry(p+"""");//末尾“""”用于文件夹的标记
u.PutNextEntry(z);
}
//***************以上内容是修订后添加的***************
foreach(DirectoryInfo temindi.GetDirectories())//获取子目录
{
ZipEntry z =newZipEntry(this.ShortDir(tem.FullName)+"""");//末尾“""”用于文件夹的标记
u.PutNextEntry(z);//此句不可少,否则空目录不会被添加
s =this.ShortDir(tem.FullName);
this.AddZipEntry(s,u,outu);//递归
}
foreach(FileInfo tempindi.GetFiles())//获取此目录的文件
{
s =this.ShortDir(temp.FullName);
this.AddZipEntry(s,u,outu); //递归
}
}
elseif(File.Exists(s))//文件的处理
{
u.SetLevel(9);//压缩等级
FileStream f = File.OpenRead(s);
byte[] b =newbyte[f.Length];
f.Read(b,0,b.Length); //将文件流加入缓冲字节中
ZipEntry z =newZipEntry(this.ShortDir(s));
u.PutNextEntry(z); //为压缩文件流提供一个容器
u.Write(b,0,b.Length);//写入字节
f.Close();
}
j=u;//返回已添加数据的“ZipOutputStream”
}
publicvoidUnZipFile(stringp)//解压缩
{
string[] un_tmp = p.Split(newchar[]{'*'});
inti2=0;//防止名称冲突的参数
for(intj=0;j<un_tmp.Length;j++)
{
if(un_tmp[j]!="")
{
stringun_time=System.DateTime.Now.ToShortDateString()+"-"+System.DateTime.Now.Hour.ToString()+"-"+System.DateTime.Now.Minute.ToString()+"-"+(System.DateTime.Now.Second+i2).ToString();
stringun_dir =ServerDir+"Unzip-"+un_time;
Directory.CreateDirectory(un_dir); //创建以解压时间为名称的文件夹
ZipInputStream f =newZipInputStream(File.OpenRead(ServerDir+un_tmp[j]));//读取压缩文件,并用此文件流新建 “ZipInputStream”对象
A: ZipEntry zp = f.GetNextEntry(); //获取解压文件流中的项目。 另注(我的理解):在压缩包里每个文件都以“ZipEntry”形式存在,其中包括存放文件的目录信息。如果空目录被压缩,该目录下将出现一个名称为空、大小为 0 、“Crc”属性为 00000000
的“文件”。此文件只是个标记,不会被解压。
while(zp!=null)
{
stringun_tmp2;
if(zp.Name.IndexOf("""")>=0)//获取文件的目录信息
{
inttmp1 = 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”不是“标记文件”
{
inti =2048;
byte[] b =newbyte[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();
}
gotoA;//进入下一个“ZipEntry”
}
f.Close();
i2++;
}
}
}
}
}
分享到:
相关推荐
本程序完全用C#编写,实现了解压缩、压缩单个文件、压缩文件夹功能
C#压缩和解压缩文件的经典源码,分享给大家。非常好的资源,留着备用吧。
c# 压缩和解压缩自己写的一个例子,并非用到SharpZipLib来压缩和解压文件,而是自己引写老外的,然后改写了一个,也没用到.NET自带的压缩与解压缩。 比他们都好的压缩!!!!
这里描述了如何使用 C#实现文件的压缩和解压缩
在C#中利用SharpZipLib进行文件的压缩和解压缩调用C#源码
c# 利用WinRAR压缩解压缩文件 本例运用c#调用WinRAR进行压缩和解压缩
基于哈弗曼编码实现高压缩和解压缩的C#源码,压缩效率高,源码呈现给大家,共同学习
c#写的压缩和解压缩的源码包括例子,学习压缩存储技术或者项目打包可以参考的.支持大多数格式,包括zip,rar....
C#利用Winrar来压缩和解压缩 C#利用Winrar来压缩和解压缩
visual studio 2005 写的一个简单文件压缩和解压缩程序。
本文实例讲述了C#实现的文件压缩和解压缩类。分享给大家供大家参考。具体分析如下: 这个C#代码包含了几个类,封装了文件压缩和解压缩常用的方法,包括直接通过代码进行压缩,也有调用winrar对文件进行压缩的 using...
c#的压缩和解压缩的源代码 压缩和解压缩
本文主要主要介绍了利用ICSharpCode.SharpZipLib第三方的DLL库实现在线压缩和解压缩的功能,并做了相关的代码演示。
主要介绍了C#文件流进行压缩和解压缩的方法,涉及C#文件流操作的相关技巧,需要的朋友可以参考下
是一种游长的压缩和解压缩算法,是针对bitmap 图像rgb有损的压缩方法,特点是速度快,使用资源少,图像损失小,压缩比较高,适合图像在局域网内传输, 可以将c#语言改成verilog,做实时压缩和传输
可以实现对文件的压缩和解压缩