企业网站 cms,微网站建设正规公司,网站空间域名每年都得交吗,网站建设思维官方网站#xff1a;http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述#xff1a; ICSharpCode.SharpZipLib.dll 是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩, SharpZipLib 的许可是经过修改的GPL… 官方网站http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述 ICSharpCode.SharpZipLib.dll 是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩, SharpZipLib 的许可是经过修改的GPL底线是允许用在不开源商业软件中意思就是免费使用。 一、在ThinksKing的Plugins里面找到已经解压好的SharpZipLib使用net-20文件夹中的ICSharpCode.SharpZipLib.dll 。添加至项目引用中。 二、操作指南 1.1 创建zip文件并添加文件 using (ZipFile zip ZipFile.Create(”E:\test.zip”))
{zip.BeginUpdate();zip.Add(”E:\文件1.txt”);zip.Add(”E:\文件2.txt”);zip.CommitUpdate();
} 1.2 将文件夹压缩为文件 (new FastZip()).CreateZip(”E:\test.zip”, ”E:\test\”, true, “”); 最后一个参数是使用正则表达式表示的过滤文件规则。CreateZip方法有3个重载版本其中有目录过滤参数、文件过滤参数及用于指定是否进行子目录递归的一个bool类型的参数。 1.3 将文件添加到已有zip文件中 using (ZipFile zip new ZipFile(”E:\test.zip”))
{zip.BeginUpdate();zip.Add(”E:\test.doc”);zip.CommitUpdate();
} 1.4 列出zip文件中文件 using (ZipFile zip new ZipFile(”E:\test.zip”))
{string list string.Empty;foreach (ZipEntry entry in zip){list entry.Name “\r\n”;}MessageBox.Show(list);
} 1.5 删除zip文件中的一个文件 using (ZipFile zip new ZipFile(”E:\test.zip”))
{zip.BeginUpdate();zip.Delete(”test.doc”);zip.Delete(”test22.txt”);zip.CommitUpdate();
} 1.6 解压zip文件中文件到指定目录下 (new FastZip()).ExtractZip(”E:\test.zip”, ”E:\test\”, “”); 1.7 常用类 ZipInputStream、GZipInputStream用于解压缩Deflate、GZip格式流ZipOutputStream、GZipOutputStream用于压缩Deflate、GZip格式流。 StreamUtil类包含了几个Stream处理辅助方法 1) Copy(Stream, Stream, Byte[])用于从一个Stream对象中复制数据到另一Stream对象。有多个重写。 2) ReadFully(Stream, Byte [])用于从Stream对象中读取所有的byte数据。有多个重写。 三、帮助文档 在SharpZipLib中有SharpZipLib_0860.chm官方帮助文档。 参考链接在路上的博文《ICSharpCode.SharpZipLib 插件使用示例》 在处理后台附件上载由于文件较多,需要每个文件单独上传关键是有些文件数据量比较少 也需要单独上传,这样导致后台数据流量较大而且用户操作麻烦. 在处理这方面业务时,可以简化:首先验证用户上传文件的大小,设定不超过1M文件为限制并记录,当用户点击一次操作时后台程序把所有小数据量文件进行压缩成一个单独文件来上传,这样简化用户操作难度 增强用户体验,在获得上载文件时同样把这个文件进行解压本地即可...使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类: 完整代码如下A:ICSharpCode.SharpZipLib.DLL组件下载地址,如果要实现必须在项目中引用该组件DLL下载地址:http://good.gd/203866.htmB:完整的操作类代码实例: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using the Compent Comspac
using System.IO;
using System.Text;
using System.Threading;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
namespace TestJqueryAjax{/**//// summary/// 使用ICSharpZipCode.Dll实现解压缩/// Author:chenkai Time:2009年7月13日22:03:27/// Version:Beta1.0.0-(测试版)/// /summarypublic class ICSharpZipCodeTest{ /**//// summary/// 实现压缩功能/// /summary/// param namefilenameToZip要压缩文件(绝对文件路径)/param/// param nameZipedfiledname压缩(绝对文件路径)/param/// param nameCompressionLevel压缩比/param/// param namepassword加密密码/param/// param namecomment压缩文件描述/param/// returns异常信息/returnspublic static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel, string password, string comment){try{//使用正则表达式-判断压缩文件路径System.Text.RegularExpressions.Regex newRegex new System.Text.RegularExpressions.Regex(^(([a-zA-Z]:)|(\\{2}\w)\$?)(\\(\w[\w ]*.*))); if (!newRegex.Match(Zipedfiledname).Success){File.Delete(Zipedfiledname);return 压缩文件的路径有误!;} //创建ZipFileOutPutStreamZipOutputStream newzipstream new ZipOutputStream(File.Open(Zipedfiledname,FileMode.OpenOrCreate));//判断Passwordif (password ! null password.Length 0){newzipstream.Password password;} if (comment ! null comment.Length 0){newzipstream.SetComment(comment);} //设置CompressionLevelnewzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression //执行压缩foreach (string filename in filenameToZip){FileStream newstream File.OpenRead(filename);//打开预压缩文件//判断路径if (!newRegex.Match(Zipedfiledname).Success){File.Delete(Zipedfiledname);return 压缩文件目标路径不存在!;} byte[] setbuffernew byte[newstream.Length];newstream.Read(setbuffer,0,setbuffer.Length);//读入文件//新建ZipEntrityZipEntry newEntry new ZipEntry(filename); //设置时间-长度newEntry.DateTime DateTime.Now;newEntry.Size newstream.Length; newstream.Close(); newzipstream.PutNextEntry(newEntry);//压入newzipstream.Write(setbuffer,0,setbuffer.Length);}//重复压入操作newzipstream.Finish();newzipstream.Close();}catch (Exception e){//出现异常File.Delete(Zipedfiledname);return e.Message.ToString();}return ;}/**//// summary/// 实现解压操作/// /summary/// param namezipfilename要解压文件Zip(物理路径)/param/// param nameUnZipDir解压目的路径(物理路径)/param/// param namepassword解压密码/param/// returns异常信息/returnspublic static string UnMakeZipFile(string zipfilename,string UnZipDir,string password){//判断待解压文件路径if (!File.Exists(zipfilename)){File.Delete(UnZipDir);return 待解压文件路径不存在!;}//创建ZipInputStreamZipInputStream newinStream new ZipInputStream(File.OpenRead(zipfilename));//判断Passwordif (password ! null password.Length 0){newinStream.Password password;}//执行解压操作try{ZipEntry theEntry;//获取Zip中单个Filewhile ((theEntry newinStream.GetNextEntry()) ! null){//判断目的路径if (Directory.Exists(UnZipDir)){Directory.CreateDirectory(UnZipDir);//创建目的目录}//获得目的目录信息string Driectoryname Path.GetDirectoryName(UnZipDir);string pathname Path.GetDirectoryName(theEntry.Name);//获得子级目录string filename Path.GetFileName(theEntry.Name);//获得子集文件名//处理文件盘符问题pathname pathname.Replace(:, $);//处理当前压缩出现盘符问题Driectoryname Driectoryname \\ pathname;//创建Directory.CreateDirectory(Driectoryname);//解压指定子目录if (filename ! string.Empty){FileStream newstream File.Create(Driectoryname \\ pathname);int size 2048;byte[] newbyte new byte[size];while (true){size newinStream.Read(newbyte, 0, newbyte.Length);if (size 0){//写入数据newstream.Write(newbyte, 0, size);}else{break;}newstream.Close();}}}newinStream.Close();}catch (Exception se){return se.Message.ToString();}finally{newinStream.Close();}
return ;}}
} 参考链接陈凯的博文《使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类》 转载于:https://www.cnblogs.com/rainbow70626/p/4559691.html