.NET中用SharpZip压缩与解压缩

注:本文主要参考了http://www.cnblogs.com/zhangweiguo3984/articles/314329.html,对其中的方法进行了稍微的修改和封装

using System; using System.IO; using ICSharpCode.SharpZipLib.Zip; /// <summary> ///SharpZipLib 的摘要说明 ///下载程序集(http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx ),在项目中添加引用 /// ///前台调用示例: ///SharpZipLib szl = new SharpZipLib(); ///string ServerDir= Server.MapPath("../WordTemplate")+"//"; ///szl.ZipFile("test.txt*test.docx*测试.zip",ServerDir); ///szl.UnZipFile("测试.zip", ServerDir); ///Response.Redirect("../WordTemplate/测试.zip");//下载压缩文件 /// /// </summary> public class SharpZipLib { public SharpZipLib() { } #region 将带路径的文件转为文件名 /// <summary> /// 将带路径的文件转为文件名 /// </summary> /// <param name="s">绝对路径</param> /// <param name="ServerDir">父级文件夹绝对路径</param> /// <returns>string</returns> public string ShortDir(string s, string ServerDir) { string d = s.Replace(ServerDir, ""); return d; } #endregion #region 压缩文件 /// <summary> /// 压缩文件 /// </summary> /// <param name="p">文件列表+压缩文件名(如“test.txt*test.docx*000.zip”)</param> /// <param name="ServerDir">父级文件夹绝对路径</param> public void ZipFile(string p, string ServerDir) { 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, ServerDir); //向压缩文件流加入内容 } } u.Finish(); // 结束压缩 u.Close(); } } /// <summary> /// 添加压缩项目 /// </summary> /// <param name="p">需压缩的文件或文件夹</param> /// <param name="u">现有的源ZipOutputStream</param> /// <param name="j">已添加“ZipEntry”的“ZipOutputStream”</param> /// <param name="ServerDir">父级文件夹绝对路径</param> public void AddZipEntry(string p, ZipOutputStream u, out ZipOutputStream j, string ServerDir) { 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, ServerDir) + "//"); //末尾“//”用于文件夹的标记 u.PutNextEntry(z); //此句不可少,否则空目录不会被添加 s = this.ShortDir(tem.FullName, ServerDir); this.AddZipEntry(s, u, out u, ServerDir); //递归 } foreach (FileInfo temp in di.GetFiles()) //获取此目录的文件 { s = this.ShortDir(temp.FullName, ServerDir); this.AddZipEntry(s, u, out u, ServerDir); //递归 } } 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, ServerDir)); u.PutNextEntry(z); //为压缩文件流提供一个容器 u.Write(b, 0, b.Length); //写入字节 f.Close(); } j = u; //返回已添加数据的“ZipOutputStream” } #endregion #region 解压缩 /// <summary> /// 解压缩 /// </summary> /// <param name="p">需解压缩的文件或文件夹</param> /// <param name="ServerDir">父级文件夹绝对路径</param> public void UnZipFile(string p, string ServerDir) { 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.ToString("yyyy_MM_dd") + "/" + un_tmp[j].Substring(0, un_tmp[j].Length - 4) + "_" + i2; 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(); //获取解压文件流中的项目。 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++; } } } #endregion }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值