因为项目需要打包文件,就在同事的建议下用ICSharpCode写了个打包函数.ICSharpCode的介绍就不说了.具体请到官方网站 http://www.icsharpcode.net/ 上了解.
首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar
代码实现多文件,自定义文件,整目录打包等功能.好了..奉上代码:
压缩打包代码
/// <summary> /// Zips the specified zip path. /// </summary> /// <param name="strZipPath">The zip path.</param> /// <param name="strZipTopDirectoryPath">The zip top directory path.</param> /// <param name="intZipLevel">The zip level.</param> /// <param name="strPassword">The password.</param> /// <param name="filesOrDirectoriesPaths">The files or directories paths.</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(1); } 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">The parent directory path.</param> /// <param name="AllFilesPath">All files path.</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
, length
, files
, 多文件打包
文件打包zip
icsharpcode 压缩、怎样打包压缩文件、如何将文件夹压缩打包、如何将文件压缩打包、文件怎么压缩打包,以便于您获取更多的相关知识。
时间: 2024-12-29 19:02:13