【转】C#执行rar,zip文件压缩的几种方法及我遇到的坑总结
工作項目中需要用到zip壓縮解壓縮文件,一開始看上了Ionic.Zip.dll這個類庫,操作方便,寫法簡單
對應有個ziphelper類
?
using Ionic.Zip;public static class ZipHelper{public static void UnZip(string zipPath, string outPath){try{using (ZipFile zip = ZipFile.Read(zipPath)){foreach (ZipEntry entry in zip){entry.Extract(outPath, ExtractExistingFileAction.OverwriteSilently);}}}catch(Exception ex){File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("/1.txt"),ex.Message + "\r\n" + ex.StackTrace);}}/// <summary>/// 遞歸子目錄時調用/// ZipHelper.Zip(files, path + model.CName + "/" + siteid + ".zip", path + model.CName + "/");/// ZipHelper.ZipDir( path + model.CName + "/" + siteid + ".zip", path + model.CName + "/", path + model.CName + "/");/// </summary>/// <param name="savefileName">要保存的文件名</param>/// <param name="childPath">要遍歷的目錄</param>/// <param name="startPath">壓縮包起始目錄結尾必須反斜杠</param>public static void ZipDir(string savefileName, string childPath, string startPath){DirectoryInfo di = new DirectoryInfo(childPath);if (di.GetDirectories().Length > 0) //有子目錄{foreach (DirectoryInfo dirs in di.GetDirectories()){string[] n = Directory.GetFiles(dirs.FullName, "*");Zip(n, savefileName, startPath);ZipDir(savefileName, dirs.FullName, startPath);}}}/// <summary>/// 壓縮zip/// </summary>/// <param name="fileToZips">文件路徑集合</param>/// <param name="zipedFile">想要壓成zip的文件名</param>/// <param name="fileDirStart">文件夾起始目錄結尾必須反斜杠</param>public static void Zip(string[] fileToZips, string zipedFile,string fileDirStart){using (ZipFile zip = new ZipFile(zipedFile, Encoding.UTF8)){foreach (string fileToZip in fileToZips){ string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);zip.AddFile(fileToZip, fileToZip.Replace(fileDirStart, "").Replace(fileName, ""));//zip.AddFile(fileToZip, fileToZip.Replace(fileDirStart, "").Replace(fileName, ""));//using (var fs = new FileStream(fileToZip, FileMode.Open, FileAccess.ReadWrite))//{// var buffer = new byte[fs.Length];// fs.Read(buffer, 0, buffer.Length);// string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);// zip.AddEntry(fileName, buffer);//}}zip.Save();}}public static void ZipOneFile(string from, string zipedFile, string to){using (ZipFile zip = new ZipFile(zipedFile, Encoding.UTF8)){zip.AddFile(from, to);zip.Save();}}}使用方法:
? ? string path = Request.MapPath("/");
? ? string[] files = Directory.GetFiles(path, "*");
? ? ZipHelper.Zip(files, path + "1.zip", path);//壓縮path下的所有文件
? ? ZipHelper.ZipDir(path + "1.zip", path, path);//遞歸壓縮path下的文件夾里的文件
?? ?ZipHelper.UnZip(Server.MapPath("/") + "lgs.zip", Server.MapPath("/"));//解壓縮
正常情況下這個使用是不會有問題的,我一直在用,不過我遇到了一個變態問題,服務器端為了安全需求,禁用了File.Move方法,然后這個類庫解壓縮時采用了重命名方案,然后很尷尬的執行失敗,困擾了我大半年時間,一直不知道原因,不過因為這個bug時隱時現,在 個別服務器上出現,所以一直沒有解決,總算最近發現問題了。
于是我想干脆不用zip壓縮了,直接調用服務器上的WinRar,這個問題顯而易見,服務器如果沒有安裝,或者不給權限同樣無法使用,我就遇到了,不過給個操作方法代碼
public class WinRarHelper {public WinRarHelper(){}static WinRarHelper(){//判斷是否安裝了WinRar.exeRegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");_existSetupWinRar = !string.IsNullOrEmpty(key.GetValue(string.Empty).ToString());//獲取WinRar.exe路徑_winRarPath = key.GetValue(string.Empty).ToString();}static bool _existSetupWinRar;/// <summary>/// 獲取是否安裝了WinRar的標識/// </summary>public static bool ExistSetupWinRar{get { return _existSetupWinRar; }}static string _winRarPath;/// <summary>/// 獲取WinRar.exe路徑/// </summary>public static string WinRarPath{get { return _winRarPath; }}#region 壓縮到.rar,這個方法針對目錄壓縮/// <summary>/// 壓縮到.rar,這個方法針對目錄壓縮/// </summary>/// <param name="intputPath">輸入目錄</param>/// <param name="outputPath">輸出目錄</param>/// <param name="outputFileName">輸出文件名</param>public static void CompressRar(string intputPath, string outputPath, string outputFileName){//rar 執行時的命令、參數string rarCmd;//啟動進程的參數ProcessStartInfo processStartInfo = new ProcessStartInfo();//進程對象Process process = new Process();try{if (!ExistSetupWinRar){throw new ArgumentException("請確認服務器上已安裝WinRar應用!");}//判斷輸入目錄是否存在if (!Directory.Exists(intputPath)){throw new ArgumentException("指定的要壓縮目錄不存在!");}//命令參數 uxinxin修正參數壓縮文件到當前目錄,而不是從盤符開始rarCmd = " a " + outputFileName + " " + "./" + " -r";//rarCmd = " a " + outputFileName + " " + outputPath + " -r";//創建啟動進程的參數//指定啟動文件名processStartInfo.FileName = WinRarPath;//指定啟動該文件時的命令、參數processStartInfo.Arguments = rarCmd;//指定啟動窗口模式:隱藏processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;//指定壓縮后到達路徑processStartInfo.WorkingDirectory = outputPath;//創建進程對象//指定進程對象啟動信息對象process.StartInfo = processStartInfo;//啟動進程process.Start();//指定進程自行退行為止process.WaitForExit();//Uxinxin增加的清理關閉,不知道是否有效process.Close();process.Dispose();}catch (Exception ex){throw ex;}finally{process.Close();process.Dispose();}}#endregion#region 解壓.rar/// <summary>/// 解壓.rar/// </summary>/// <param name="inputRarFileName">輸入.rar</param>/// <param name="outputPath">輸出目錄</param>public static void UnCompressRar(string inputRarFileName, string outputPath){//rar 執行時的命令、參數string rarCmd;//啟動進程的參數ProcessStartInfo processStartInfo = new ProcessStartInfo();//進程對象Process process = new Process();try{if (!ExistSetupWinRar){throw new ArgumentException("請確認服務器上已安裝WinRar應用!");}//如果壓縮到目標路徑不存在if (!Directory.Exists(outputPath)){//創建壓縮到目標路徑Directory.CreateDirectory(outputPath);}rarCmd = "x " + inputRarFileName + " " + outputPath + " -y";processStartInfo.FileName = WinRarPath;processStartInfo.Arguments = rarCmd;processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;processStartInfo.WorkingDirectory = outputPath;process.StartInfo = processStartInfo;process.Start();process.WaitForExit();process.Close();process.Dispose();}catch (Exception ex){throw ex;}finally{process.Close();process.Dispose();}}#endregion#region 將傳入的文件列表壓縮到指定的目錄下/// <summary>/// 將傳入的文件列表壓縮到指定的目錄下/// </summary>/// <param name="sourceFilesPaths">要壓縮的文件路徑列表</param>/// <param name="compressFileSavePath">壓縮文件存放路徑</param>/// <param name="compressFileName">壓縮文件名(全名)</param>public static void CompressFilesToRar(List<string> sourceFilesPaths, string compressFileSavePath, string compressFileName){//rar 執行時的命令、參數string rarCmd;//啟動進程的參數ProcessStartInfo processStartInfo = new ProcessStartInfo();//創建進程對象//進程對象Process process = new Process();try{if (!ExistSetupWinRar){throw new ArgumentException("Not setuping the winRar, you can Compress.make sure setuped winRar.");}//判斷輸入文件列表是否為空if (sourceFilesPaths == null || sourceFilesPaths.Count < 1){throw new ArgumentException("CompressRar'arge : sourceFilesPaths cannot be null.");}rarCmd = " a -ep1 -ap " + compressFileName;//-ep1 -ap表示壓縮時不保留原有文件的路徑,都壓縮到壓縮包中即可,調用winrar命令內容可以參考我轉載的另一篇文章:教你如何在DOS(cmd)下使用WinRAR壓縮文件foreach (object filePath in sourceFilesPaths){rarCmd += " " + filePath.ToString(); //每個文件路徑要與其他的文件用空格隔開}//rarCmd += " -r";//創建啟動進程的參數//指定啟動文件名processStartInfo.FileName = WinRarPath;//指定啟動該文件時的命令、參數processStartInfo.Arguments = rarCmd;//指定啟動窗口模式:隱藏processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;//指定壓縮后到達路徑processStartInfo.WorkingDirectory = compressFileSavePath;//指定進程對象啟動信息對象process.StartInfo = processStartInfo;//啟動進程process.Start();//指定進程自行退行為止process.WaitForExit();process.Close();process.Dispose();}catch (Exception ex){throw ex;}finally{process.Close();process.Dispose();}}#endregion }調用方法:
if (WinRarHelper.ExistSetupWinRar)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? WinRarHelper.CompressRar(Server.MapPath("/"), Server.MapPath("/"), "1.zip");
? ? ? ? ? ? ? ? Response.Write("壓縮完成!" + DateTime.Now);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Win32Exception e1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Write(e1.Message + "<br>" + "服務器端禁止是我們網站使用WinRar應用執行!<br>");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Write(e1.Message + "<br>" + e1.StackTrace);
? ? ? ? ? ? }
? ? ? ? if (WinRarHelper.ExistSetupWinRar)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? WinRarHelper.UnCompressRar(Server.MapPath("/") + "lgs.zip", Server.MapPath("/"));
? ? ? ? ? ? ? ? Response.Write("解壓縮完成!" + DateTime.Now);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Win32Exception e1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Write(e1.Message + "<br>" + "服務器端禁止是我們網站使用WinRar應用執行!<br>");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Write(e1.Message);
? ? ? ? ? ? }
? ? ? ? }
最后我找到了一個解壓的時候不用重命名方法的,還好服務器對解壓沒限制
ICSharpCode.SharpZipLib.dll ?用到這個文件
參考來自http://www.cnblogs.com/yuangang/p/5581391.html
具體我也不寫了,就看參考網站吧,我也沒改代碼,不錯,記錄一下,花了我整整一天折騰這玩意兒!
總結
以上是生活随笔為你收集整理的【转】C#执行rar,zip文件压缩的几种方法及我遇到的坑总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《操作系统真象还原》-阅读笔记(下)
- 下一篇: C#多线程和线程池