1. 程式人生 > 實用技巧 >C# 壓縮原始檔(匯出原始檔word檔案)

C# 壓縮原始檔(匯出原始檔word檔案)

說明 1 : 在webUI 公共類,存放ZipHelper 類

說明 2 :判斷檔案路徑是否存在,不存在則建立資料夾

說明 3 : 引用類方法,判斷壓縮檔案,返回的,是true/false

  
引用常用公共壓縮方法類
public class CLeopardZip { /// <summary> /// 適用與ZIP壓縮 /// </summary> public class ZipHelper { #region 壓縮 /// <summary>
/// 遞迴壓縮資料夾的內部方法 /// </summary> /// <param name="folderToZip">要壓縮的資料夾路徑</param> /// <param name="zipStream">壓縮輸出流</param> /// <param name="parentFolderName">此資料夾的上級資料夾</param> /// <returns></returns>
private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName) { bool result = true; string[] folders, files; ZipEntry ent = null; FileStream fs = null; Crc32 crc
= new Crc32(); try { ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); zipStream.PutNextEntry(ent); zipStream.Flush(); //從複製後的路徑,獲取的當前資料夾的,所有原始檔 files = Directory.GetFiles(folderToZip); foreach (string file in files) { fs = System.IO.File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "//" + Path.GetFileName(file))); ent.DateTime = DateTime.Now; ent.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); ent.Crc = crc.Value; zipStream.PutNextEntry(ent); zipStream.Write(buffer, 0, buffer.Length); } } catch { result = false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } folders = Directory.GetDirectories(folderToZip); //此,不需要遍歷其他的檔案 //遍歷doc中的所有檔案, //foreach (string folder in folders) // if (!ZipDirectory(folder, zipStream, folderToZip,HtmlFiles)) // return false; return result; } /// <summary> /// 壓縮資料夾 /// </summary> /// <param name="folderToZip">要壓縮的資料夾路徑</param> /// <param name="zipedFile">壓縮檔案完整路徑</param> /// <param name="password">密碼</param> /// <returns>是否壓縮成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile, string password,string HtmlFiles) { bool result = false; if (!Directory.Exists(folderToZip)) return result; ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipedFile)); zipStream.SetLevel(6); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; result = ZipDirectory(folderToZip, zipStream, ""); zipStream.Finish(); zipStream.Close(); return result; } /// <summary> /// 壓縮資料夾 /// </summary> /// <param name="folderToZip">要壓縮的資料夾路徑</param> /// <param name="zipedFile">壓縮檔案完整路徑</param> /// <returns>是否壓縮成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile,string HtmlFiles) { bool result = ZipDirectory(folderToZip, zipedFile, null,HtmlFiles); return result; } /// <summary> /// 壓縮檔案 /// </summary> /// <param name="fileToZip">要壓縮的檔案全名</param> /// <param name="zipedFile">壓縮後的檔名</param> /// <param name="password">密碼</param> /// <returns>壓縮結果</returns> public static bool ZipFile(string fileToZip, string zipedFile, string password) { bool result = true; ZipOutputStream zipStream = null; FileStream fs = null; ZipEntry ent = null; if (!System.IO.File.Exists(fileToZip)) return false; try { fs = System.IO.File.OpenRead(fileToZip); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs = System.IO.File.Create(zipedFile); zipStream = new ZipOutputStream(fs); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; ent = new ZipEntry(Path.GetFileName(fileToZip)); zipStream.PutNextEntry(ent); zipStream.SetLevel(6); zipStream.Write(buffer, 0, buffer.Length); } catch { result = false; } finally { if (zipStream != null) { zipStream.Finish(); zipStream.Close(); } if (ent != null) { ent = null; } if (fs != null) { fs.Close(); fs.Dispose(); } } GC.Collect(); GC.Collect(1); return result; } /// <summary> /// 壓縮檔案 /// </summary> /// <param name="fileToZip">要壓縮的檔案全名</param> /// <param name="zipedFile">壓縮後的檔名</param> /// <returns>壓縮結果</returns> public static bool ZipFile(string fileToZip, string zipedFile) { bool result = ZipFile(fileToZip, zipedFile, null); return result; } /// <summary> /// 壓縮檔案或資料夾 /// </summary> /// <param name="fileToZip">要壓縮的路徑</param> /// <param name="zipedFile">壓縮後的檔名</param> /// <param name="password">密碼</param> /// <param name="HtmlFiles">比對檔案</param> /// <returns>壓縮結果</returns> public static bool Zip(string fileToZip, string zipedFile, string password) { bool result = false; if (Directory.Exists(fileToZip)) result = ZipDirectory(fileToZip, zipedFile, password); else if (System.IO.File.Exists(fileToZip)) result = ZipFile(fileToZip, zipedFile, password); return result; } /// <summary> /// 壓縮檔案或資料夾 /// </summary> /// <param name="fileToZip">要壓縮的路徑</param> /// <param name="zipedFile">壓縮後的檔名</param> /// <returns>壓縮結果</returns> public static bool Zip(string fileToZip, string zipedFile) { bool result = Zip(fileToZip, zipedFile, null); return result; } #endregion } }


說明 2 判斷檔案路徑是否存在,不存在則建立資料夾

1
2 3 if (!System.IO.Directory.Exists(newPath)) 4 { 5 System.IO.Directory.CreateDirectory(newPath); 6 }

 
說明 3 : 引用類方法,判斷返回的,是true/false

1
#region FilesZIP 2 public string FilesZIP(List<Files> _list) 3 { 4 string password = ""; 5 var Path = HttpContext.Current.Server.MapPath("~/doc/"); 6 string newPath = Path + Guid.NewGuid().ToString(); 7 if (!System.IO.Directory.Exists(newPath)) 8 { 9 System.IO.Directory.CreateDirectory(newPath); 10 } 11 foreach (Exx.QMS.Model.File.FileModel item in _list) 12 { 13 var _files = Directory.GetFiles(Path); 14 if (_files.Contains(Path + item.FileContent)) 15 { 16 var ext = item.FileContent.Split('.')[1];
//第一個引數:要複製的路徑 ; 第二個引數 給複製的路徑檔案,重新命名 eg:原始檔名稱:DHFDSJF.docx 命名後的:統計說明.docx
17 System.IO.File.Copy(Path + item.FileContent, newPath + "/" + item.FileName+"."+ ext, true); 18 } 19 } 20 string fileToZip = newPath; 21 var zipedFile = newPath + ".zip"; 22 var fileZip = Common.CLeopardZip.ZipHelper.Zip(fileToZip, zipedFile, password); 23 var IsHtml = fileZip == true ? zipedFile : "Error"; 24 return IsHtml; 25 } 26 #endregion