/// <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) // get all files path { for (int i = 0; i < filesOrDirectoriesPaths.Length; i++) { if (File.Exists(filesOrDirectoriesPaths[i])) { AllFilesPath.Add(filesOrDirectoriesPaths[i]); } else if (Directory.Exists(filesOrDirectoriesPaths[i])) { GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath); } } } if (AllFilesPath.Count > 0) { ZipOutputStream zipOutputStream = new ZipOutputStream(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 = 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); 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> /// Gets the directory files. /// </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) //empty folder { AllFilesPath.Add(strParentDirectoryPath); } } 调用 string strZipPath = @"D:\ConsultSystem\mgr\user.zip"; string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\"; int intZipLevel = 6; string strPassword = ""; string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" }; Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
相关推荐
十天学会ASP.NET教程十天学会ASP.NET教程.rar
十天学会ASP.NET教程.rar十天学会ASP.NET教程.rar
asp.net利用RAR实现文件压缩解压缩
如果服务器上安装了RAR程序,那么asp.net可以调用RAR实现文件压缩与解压缩。不过要注意的是,由于Web程序不能直接调用客户端的程序(除非用ActiveX,ActiveX几乎被废弃),所以如果要想实现让用户把本地文件用网页解压缩只有把文件上传到服务器上再调用服务器上的RAR压缩,同理要解压缩本地的RAR文件可以把文件上传到服务器解压再拿回来。本文讲怎么在服务器端的目录解压缩文件!
十天学会asp.net教程 rar压缩文件
十天学会asp.net教程,压缩文件,让你轻松上手 .net
ASP.NET生成压缩文件(rar打包)ICSharpCode.SharpZipLib.dll
<br />因为项目需要打包文件,就在同事的建议下用ICSharpCode写了个打包函数.ICSharpCode的介绍就不说了.具体请到官方网站 http://www.icsharpcode.net/ 上了解. <br /> 首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar<br /> 代码实现多文件,自定义文件,整目录打包等功能.好了..奉上代码:<
asp.net 实现文件的压缩和解压
如果该博客能给您带来帮助,请给博主一个评论谢谢!!话不多说下面请看具体的实现步骤。 1.首先在web项目中引用ICSharpCode.SharpZipLib.dll文件,可在博主的资源中下载。 2.具体的压缩和解压方法实现如下(代码中有详细的备注) /// <summary> /// 生成压缩文件 /// </summary> /// <param
在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
使用ICSharpCode.SharpZipLib.dll; 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx 下面是对#ZipLib进行.net下的解压缩的方法的介绍。 1.BZip2 加入ICSharpCode.SharpZipLib.dll的引用,在#Devel
ASP.NET生成压缩文件(rar打包)1【转】
// /// 生成压缩文件 /// /// 生成的zip文件的路径 /// 源文件的上级目录 /// T压缩等级 /// 压缩包解压密码 /// 源文件路径 /// private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] f
ASP.NET生成压缩文件(rar打包)2
using System.IO; using System.IO.Compression; public static void fileCompress(string f_name) { string filename =HttpContext.Current.Server.MapPath(f_name); FileStream infile = F
Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
(一). 实现功能 对文件及目录的压缩及解压功能(二). 运行图片示例 (三).代码 1. 压缩类 1/**//// 2/// 压缩类 3/// 4public class ZipClass 5{ 6 public static void ZipFile(string FileToZip, string Zi
菜鸟详听ASP中也能解压缩rar文件
菜鸟详听ASP中也能解压缩rar文件 文/陈程 有没有想过在线解压缩压缩文件呢?呵呵,有时上传一个压缩包以后,利用Asp程序解压缩的确很方便,尤其是经常更新网站的站长们,由于文本文件
Asp.net 2.0 C#实现压缩/解压功能
Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载) (一). 实现功能 对文件及目录的压缩及解压功能 (二). 运行图片示例 (三).代码 1. 压缩类 1/// 2/// 压缩类 3/// 4public class ZipClass 5{ 6 public s
使用C#压缩解压rar和zip格式文件
为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar、zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库。 在C#.NET中压缩解压rar文件 rar格式是一种具有专利文件的压缩格式,是一种商业压缩格式,不开源,对解码算法是公开的,但压缩算法是私有的,需要付费,如果需要在您的商业软件中使用rar格式进行
742


&spm=1001.2101.3001.11974&articleId=6886273&d=1&t=3&u=4e3cb898c11942f78616917df464bc53)

被折叠的 条评论
为什么被折叠?



