asp.net 实现文件的压缩和解压
来源:互联网 发布:淘宝哪家童装店质量好 编辑:程序博客网 时间:2024/05/22 10:51
如果该博客能给您带来帮助,请给博主一个评论谢谢!!话不多说下面请看具体的实现步骤。
1.首先在web项目中引用ICSharpCode.SharpZipLib.dll文件,可在博主的资源中下载。
2.具体的压缩和解压方法实现如下(代码中有详细的备注)
/// <summary> /// 生成压缩文件 /// </summary> /// <param name="strZipPath">生成的zip文件的路径</param> /// <param name="strZipTopDirectoryPath">源文件的上级目录</param> /// <param name="intZipLevel">T压缩等级</param> /// <param name="strPassword">压缩包解压密码</param> /// <param name="filesOrDirectoriesPaths">源文件路径</param> /// <returns></returns> private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths) { try { List<string> AllFilesPath = new List<string>();//新建路径集合 if (filesOrDirectoriesPaths.Length > 0) //得到原路径的所有文件 { for (int i = 0; i < filesOrDirectoriesPaths.Length; i++) { if (System.IO.File.Exists(filesOrDirectoriesPaths[i]))//检测如果存在的是文件 { AllFilesPath.Add(filesOrDirectoriesPaths[i]);//添加到新路径集合中 } else if (Directory.Exists(filesOrDirectoriesPaths[i]))//检测如果存在的是路径 { GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);//将路径中的所有文件添加到新文件集合中 } } } if (AllFilesPath.Count > 0)//验证文件集合长度是否大于0 { ZipOutputStream zipOutputStream = new ZipOutputStream(System.IO.File.Create(strZipPath));//新建压缩文件流 zipOutputStream.SetLevel(intZipLevel);//设定压缩文件等级 zipOutputStream.Password = strPassword;//设定压缩包解压缩密码 for (int i = 0; i < AllFilesPath.Count; i++)//获取列表集合 { string strFile = AllFilesPath[i].ToString();//取出单个文件 try { if (strFile.Substring(strFile.Length - 1) == "") //folder检测文件长度 { string strFileName = strFile.Replace(strZipTopDirectoryPath, "");//取出文件名称 去除前面的文件路径 if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(1); } ZipEntry entry = new ZipEntry(strFileName);// entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); } else //file { FileStream fs = System.IO.File.OpenRead(strFile);//读取文件 byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string strFileName = strFile.Replace(strZipTopDirectoryPath, ""); if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(0); } ZipEntry entry = new ZipEntry(strFileName);//指定名称创建新的 ZIP 条目 entry.DateTime = DateTime.Now;//指定当前时间 zipOutputStream.PutNextEntry(entry);//为压缩文件流提供一个容器 zipOutputStream.Write(buffer, 0, buffer.Length);//写入文件 fs.Close(); fs.Dispose(); } } catch { continue; } } zipOutputStream.Finish();//结束压缩 zipOutputStream.Close();//关闭 return true; } else { return false; } } catch { return false; } } /// <summary> /// 文件解压 /// </summary> /// <param name="strZipPath">压缩包路径</param> /// <param name="strOldPath">解压文件源目录</param> /// <returns></returns> private bool UnZipFile(string strZipPath, string strOldPath) { string un_dir = strOldPath + "user"; Directory.CreateDirectory(un_dir); //创建以解压时间为名称的文件夹 ZipInputStream f = new ZipInputStream(System.IO.File.OpenRead(strZipPath)); //读取压缩文件,并用此文件流新建 “ZipInputStream”对象 ZipEntry zp = null; while ((zp = f.GetNextEntry()) != null) { int i = 2048; byte[] b = new byte[i]; //每次缓冲 2048 字节 FileStream s = System.IO.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 ,跳出循环 } } f.Close(); return true; } /// <summary> /// 得到存放目录下的所有文件 /// </summary> /// <param name="strParentDirectoryPath">源文件路径</param> /// <param name="AllFilesPath">所有文件路径</param> private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath) { string[] files = Directory.GetFiles(strParentDirectoryPath); for (int i = 0; i < files.Length; i++) { AllFilesPath.Add(files[i]); } string[] directorys = Directory.GetDirectories(strParentDirectoryPath); for (int i = 0; i < directorys.Length; i++) { GetDirectoryFiles(directorys[i], AllFilesPath); } if (files.Length == 0 && directorys.Length == 0) { AllFilesPath.Add(strParentDirectoryPath); } }//引用方式在main方法中添加如下代码(可被触发的方法就行) string strZipPath =@"D:\HttpHandlers\UploadFile\user.zip"; string strZipTopDirectoryPath = @"D:\HttpHandlers\UploadFile\"; int intZipLevel = 6; string strPassword = ""; string[] filesOrDirectoriesPaths = new string[] { @"D:\HttpHandlers\UploadFile\" }; Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);//压缩方法 //UnZipFile(strZipPath, strZipTopDirectoryPath);//解压方法
阅读全文
0 0
- asp.net 实现文件的压缩和解压
- 实现asp.net的文件压缩、解压、下载
- java实现的文件压缩和解压
- .net 文件压缩和解压
- C#/ASP.NET/WinForm 通过ICSharpCode.SharpZipLib.dll实现文件夹、文件的压缩与解压
- 文件的压缩和解压
- 文件的压缩和解压
- .net 压缩、解压文件
- 用ASP.NET实现文件的在线压缩和解压缩
- 用ASP.Net实现文件的在线压缩和解压缩
- 用ASP.Net实现文件的在线压缩和解压缩
- 用ASP.Net实现文件的在线压缩和解压缩
- 用ASP.Net实现文件的在线压缩和解压缩
- 用ASP.NET实现文件的在线压缩和解压缩
- ASP.Net实现文件的在线压缩和解压缩
- 用ASP.NET实现文件的在线压缩和解压缩
- .Net 实现 Zip 压缩和解压
- java中对文件解压和压缩的实现
- Ubuntu下串口通信之cutecom
- kafka中partition和消费者对应关系
- 2017年个人看书学习计划
- java中String、StringBuffer、StringBuilder的区别
- Jenkins 常用插件
- asp.net 实现文件的压缩和解压
- ubuntu server10.4下NIS服务的配置
- Shell脚本运行~01
- Deep Learning Symposium(LeCun、Bengio和Hinton)
- 普及篇:什么是瀑布模型?
- ajax----------3、GET请求示例
- 软件需求分析
- PHP中rand()的使用
- 在未引用的程序集中定义。必须添加对程序集“mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用