C#实现文件压缩解压类
2014-04-18 14:58
141 查看
C#实现文件压缩解压类
首先需要在项目里引用ICSharpCode.SharpZipLib.dll。
下载网址:http://ncu.dl.sourceforge.net/project/sharpdevelop/SharpZipLib/0.86/SharpZipLib_0860_Bin.zip
注:本类引用的是 0.86 版
压缩 与 解压类如下 (注:仅支持 .zip 的压缩与解压)using System; using System.Collections.Generic; using System.Linq; using System.Web; using ICSharpCode.SharpZipLib.Zip; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using System.Text; namespace 文件压缩与解压 { public class ZipClass { #region 压缩文件 /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">待压缩的文件</param> /// <param name="ZipedFile">压缩后的文件</param> /// <param name="password">压缩密码</param> private void ZipFile(string FileToZip, string ZipedFile,string password) { //如果文件没有找到,则报错 if (!System.IO.File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("文件 " + FileToZip + "不存在!"); } //取得待压缩文件流 System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); //创建压缩后的文件 System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile); //创建新的ZIP输出流 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); // 每一个被压缩的文件都用ZipEntry表示,需要为每一个压缩后的文件设置名称 ZipEntry ZipEntry = new ZipEntry("ZippedFile"); //设置每一个ZipEntry对象 ZipStream.PutNextEntry(ZipEntry); // 为后续的 DEFLATED 条目设置压缩级别。 0 -9 ZipStream.SetLevel(6); //设置解压密码 ZipStream.Password = password; //每次写入1024个字节 byte[] buffer = new byte[1024]; int size = 0; //已写入压缩流的字节数 try { //如果没有写入完成 while (size < StreamToZip.Length) { //将文件内容写入buffer int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); //将字节写入压缩流 ZipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { throw ex; } //完成写入 ZIP 输出流的内容,无需关闭底层流。 ZipStream.Finish(); //关闭 ZIP 输出流和正在过滤的流。 ZipStream.Close(); //关闭文件流 StreamToZip.Close(); } #endregion 压缩文件 #region 压缩文件夹(私有不被外部调用) /// <summary> /// 压缩文件夹 /// </summary> /// <param name="FileToZip">待压缩的文件夹</param> /// <param name="ZipStream">压缩文件流</param> /// <param name="ParentFolderName">压缩父目录</param> private void ZipFolder(string FolderToZip, ZipOutputStream ZipStream, string ParentFolderName) { //如果文件夹没有找到,则报错 if (!System.IO.Directory.Exists(FolderToZip)) { throw new System.IO.FileNotFoundException("文件路径 " + FolderToZip + "不存在!"); } //校验类 Crc32 crc = new Crc32(); //取得路径下所有文件全路径 string[] filenames = Directory.GetFiles(FolderToZip); //循环压缩文件 foreach (string file in filenames) { //打开要的压缩文件 if (!System.IO.File.Exists(file)) //判断文件是否存在 { throw new System.IO.FileNotFoundException("待压缩的文件 " + file + "不存在!"); } //取得待压缩文件流 FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; //将文件写入字节数组 fs.Read(buffer, 0, buffer.Length); //压缩文件后的目录 string ZipPath = Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)); ZipEntry entry = new ZipEntry(ZipPath); entry.DateTime = DateTime.Now; //设置size entry.Size = fs.Length; //关闭文件流 fs.Close(); // 将 CRC-32 重置为初始值。 crc.Reset(); // 使用指定的字节数组更新校验和。 crc.Update(buffer); // 设置 CRC-32 值。 entry.Crc = crc.Value; //设置每一个ZipEntry对象 ZipStream.PutNextEntry(entry); //将字节写入压缩流 ZipStream.Write(buffer, 0, buffer.Length); } //取得路径下所有文件夹全路径 string[] folders = Directory.GetDirectories(FolderToZip); //循环压缩文件夹下层目录的文件 foreach (string folder in folders) { ZipFolder(folder, ZipStream, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))); } } #endregion 压缩文件夹 #region 压缩文件夹 (可被外部应用) /// <summary> /// 压缩文件夹 /// </summary> /// <param name="FileToZip">待压缩的文件夹</param> /// <param name="ZipStream">压缩后的文件</param> /// <param name="password">压缩密码</param> public void ZipFolder(string FolderToZip, string ZipedFile, string password) { // 解决中文乱码 Encoding gbk = Encoding.GetEncoding("gbk"); ZipConstants.DefaultCodePage = gbk.CodePage; //创建压缩后的文件 System.IO.FileStream zipFile = System.IO.File.Create(ZipedFile); //创建新的ZIP输出流 ZipOutputStream ZipStream = new ZipOutputStream(zipFile); //设置解压密码 ZipStream.Password = password; // 为后续的 DEFLATED 条目设置压缩级别。 0 -9 ZipStream.SetLevel(6); //压缩文件夹 ZipFolder(FolderToZip, ZipStream, ""); //完成写入 ZIP 输出流的内容,无需关闭底层流。 ZipStream.Finish(); //关闭 ZIP 输出流和正在过滤的流。 ZipStream.Close(); } #endregion 压缩文件夹 #region 压缩文件(根据路径 自动判定是文件夹还是文件) /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">待压缩的文件(夹)路径</param> /// <param name="ZipStream">压缩后的文件</param> /// <param name="password">压缩密码</param> public void ZipFileMain(string FileToZip, string ZipedFile, string password) { //为压缩文件夹 if (Directory.Exists(FileToZip)) { //压缩文件夹 ZipFolder(FileToZip, ZipedFile, password); } //为压缩文件 else if (File.Exists(FileToZip)) { // //压缩文件 ZipFile(FileToZip, ZipedFile, password); } else { throw new System.IO.FileNotFoundException("待压缩的文件目录 " + FileToZip + "出错!"); } } #endregion 压缩文件(根据路径 自动判定是文件夹还是文件) #region 解压文件 /// <summary> /// 解压 /// </summary> /// <param name="args"> // args[0] = Server.MapPath("ZIP") + "\\f12Zip.zip"; //待解压的文件 // args[1]=Server.MapPath("UPZIP\\");//解压后放置的目标目录 // 或 // args[0] = "D:\\f12Zip.zip"; //待解压的文件 // args[1]="D:\\UPZIP\\"); //解压后放置的目标目录 //</param> /// <summary> /// 解压 /// </summary> /// <param name="UpZipFile">待解压文件</param> /// <param name="ZipToFile">解压后放置的目标目录</param> /// <param name="password">解压密码</param> public void UnZip(string UpZipFile, string ZipToFile,string password) { if (!System.IO.File.Exists(UpZipFile)) { throw new System.IO.FileNotFoundException("文件 " + UpZipFile + "不存在!"); } //创建新的ZIP输入流 ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(UpZipFile)); //设置解压密码 ZipStream.Password = password; ZipEntry theEntry; while ((theEntry = ZipStream.GetNextEntry()) != null) { //取得解压后的目录 string directoryName = Path.GetDirectoryName(ZipToFile); //取得解压文件下的文件名 string fileName = Path.GetFileName(theEntry.Name); //取得子目录 string filepath = Path.GetDirectoryName(theEntry.Name); //取得解压文件名 //如 xxxxx.zip string ZipFile = Path.GetFileName(UpZipFile); //去掉文件名后缀 xxxxx string zipfile = ZipFile.Split('.')[0]; //创建解压后文件目录 string filePath = directoryName + "\\" + zipfile + "\\" + filepath + "\\"; Directory.CreateDirectory(filePath); if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(filePath + "\\" + fileName); int size = 2048; byte[] data = new byte[2048]; while (true) { size = ZipStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } ZipStream.Close(); } #endregion 解压文件 } }
调用方式:
string filepath = Server.MapPath("freezip");
string strtxtPath = filepath; //压缩文件夹
string strzipPath = Server.MapPath("ZIP") + "\\f12Zip.zip";
// strtxtPath= "C:\\unzipped\\";//待压缩文件目录
// strzipPath="C:\\zip\\a.zip"; //压缩后的目标文件
ZipClass zc = new ZipClass();
zc.ZipFileMain(strtxtPath,strzipPath,""); //压缩文件
解压相似。
相关文章推荐
- c#非递归实现后序遍历
- 《设计模式:基于C#的工程化实现及扩展》学习笔记 02 准备篇 -- Namespace(命名空间)
- C#中实现VB中的CreateObject方法
- C# 自己实现的一套ORM程序附带实体生成工具
- 简单计算器的面向过程实现和面向对象实现对比 分类: C# 2012-06-21 10:45 2783人阅读 评论(0) 收藏
- C# 聊天室的框架设计实现源码
- 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )
- C#实现对SQL数据库中的表的查询、添加、修改、删除数据
- 浅谈C# 多态的魅力(虚方法,抽象,接口实现)
- 详解C# 网络编程系列:实现类似QQ的即时通信程序
- C#SocketAsyncEventArgs实现高效能多并发TCPSocket通信 (服务器实现)
- 4、替换空格 C#实现
- C#实现图片到数据库的存取
- unity3d学习笔记(十八)--利用C#的代理和事件结合NGUI的按钮实现施放
- C#基于websocket实时通信的实现—GoEasy
- C#实现启用或停用本地网络连接
- 用C#和vs2015编写了一个简单音乐播放器,实现功能,多条音乐导入,播放、暂停、上下曲。采用vs2105自带的一个播放器控件
- c#实现科学计算器 实现篇之文法分析器
- ASP.NET的模板实现(c#)
- C#实现所有经典排序算法