這里借助了第三方程序集ICSharpCode,請自行檢索下載。
下面上碼
/// <summary>/// 制作壓縮包(多個文件壓縮到一個壓縮包,支持加密、注釋)/// </summary>/// <param name="fileNames">要壓縮的文件</param>/// <param name="topDirectoryName">壓縮文件目錄</param>/// <param name="zWord">密碼</param>/// <param name="comment">注釋</param>public static void ZipFiles(string[] fileNames, string topDirectoryName, string zipedFileName, int? compresssionLevel, string password="", string comment=""){ using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) { if (compresssionLevel.HasValue) { zos.SetLevel(compresssionLevel.Value);//設置壓縮級別 } if (!string.IsNullOrEmpty(password)) { zos.Password = password;//設置zip包加密密碼 } if (!string.IsNullOrEmpty(comment)) { zos.SetComment(comment);//設置zip包的注釋 } foreach (string file in fileNames) { string fileName = string.Format("{0}/{1}", topDirectoryName, file); if (File.Exists(fileName)) { FileInfo item = new FileInfo(fileName); FileStream fs = File.OpenRead(item.FullName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(item.Name); zos.PutNextEntry(entry); zos.Write(buffer, 0, buffer.Length); } } }}新聞熱點
疑難解答