ASP.NET生成压缩文件(rar打包)1【转】
来源:互联网 发布:田士永 知乎 编辑:程序博客网 时间:2024/04/30 02:54
// <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" };
0 0
- ASP.NET生成压缩文件(rar打包)1【转】
- ASP.NET生成压缩文件(rar打包)
- ASP.NET生成压缩文件(rar打包)
- ASP.NET生成压缩文件(rar打包)ICSharpCode.SharpZipLib.dll
- ASP.NET生成压缩文件(rar打包)2
- C#操作压缩文件(rar,zip,gzip,winform/asp.net)
- C#操作压缩文件(rar,zip,gzip,winform/asp.net)
- C#操作压缩文件(rar,zip,gzip,winform/asp.net)
- Asp.net调用RAR压缩文件与解压文件源码
- .net中RAR压缩文件的方法
- Asp.net压缩文件
- asp.net上传压缩文件
- rar 打包并压缩文件,排除文件前缀目录
- sql 压缩文件(rar或zip)
- 解压(.rar及.zip格式)压缩文件
- ASP.NET 压缩和解压缩文件
- ASP.net实现RAR压缩
- ASP.NET 解压.rar文件
- 测试覆盖率插件cobertura-maven-plugin
- js 校验身份证号码
- AjaxPro使用说明【转载】
- Android 针对多种屏幕进行设计 (一)支持各种屏幕尺寸
- eclipse远程调试
- ASP.NET生成压缩文件(rar打包)1【转】
- 创建IRP_MJ_WRITE IRP参数的困惑
- maven常用命令
- 使用 libevent提高网络应用性能
- Linux简单起步
- NetFilter (2)
- quick-cocos2d-x 3.3 多个AssetManager同时下载的进度问题
- nova-scheduler调度系统科研进展
- Android 针对多种屏幕进行设计 (二)支持各种屏幕密度