在实际项目应用中会偶尔使用文件的压缩上传以及服务器端的加压处理,故写此文记录以备不时之需.
1.自己编写的ZipHelper类.
1 public static class ZipHelper 2 { 3 private static string pathExe = AppDomain.CurrentDomain.BaseDirectory + @"Resource\WinRAR.exe"; 4 /// <summary> 5 /// 使用Gzip方法压缩文件 6 /// </summary> 7 /// <param name="sourcefilename"></param> 8 /// <param name="zipfilename"></param> 9 /// <returns></returns> 10 public static bool GZipFile(string sourcefilename, string zipfilename) 11 { 12 bool isSucc = false; 13 //拼接压缩命令参数 14 string args = string.Format("a -as -r -afzip -ed -ibck -inul -m5 -mt5 -ep1 {0} {1}", zipfilename, sourcefilename); 15 16 //启动压缩进程 17 isSucc = ProcessHelper.StartProcess(pathExe,args); 18 return isSucc; 19 } 20 21 /// <summary> 22 /// 使用GZIP解压文件的方法 23 /// </summary> 24 /// <param name="zipfilename"></param> 25 /// <param name="unzipfilename"></param> 26 /// <returns></returns> 27 public static bool UnGzipFile(string zipfilename, string unzipfilename) 28 { 29 bool isSucc = false; 30 if (!Directory.Exists(unzipfilename)) 31 { 32 Directory.CreateDirectory(unzipfilename); 33 } 34 //拼接解压命令参数 35 string args = string.Format("x -ibck -inul -y -mt5 {0} {1}", zipfilename, unzipfilename); 36 37 //启动解压进程 38 isSucc = ProcessHelper.StartProcess(pathExe, args); 39 return isSucc; 40 } 41 }
2.用到的ProcessHelper类.
1 public class ProcessHelper 2 { 3 /// <summary> 4 /// 启动进程执行exe 5 /// </summary> 6 /// <param name="exePath">exe路径</param> 7 /// <param name="exeArgs">exe所需参数</param> 8 /// <returns></returns> 9 public static bool StartProcess(string exePath,string exeArgs) 10 { 11 bool isHidden = true; 12 bool isSucc = true; 13 Process process = new Process(); 14 process.StartInfo.FileName = exePath; 15 process.StartInfo.Arguments = exeArgs; 16 if (isHidden) 17 { 18 process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 19 process.StartInfo.CreateNoWindow = true; 20 } 21 else 22 { 23 process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; 24 process.StartInfo.CreateNoWindow = false; 25 } 26 process.Start(); 27 int idx = 1; 28 while (!process.HasExited) 29 { 30 idx++; 31 process.WaitForExit(1000); 32 if (idx == 50) 33 { 34 process.Kill(); 35 isSucc = false; 36 } 37 } 38 process.Close(); 39 process.Dispose(); 40 return isSucc; 41 } 42 }
3.WinRar相关命令解释:
c#调用 WinRAR.exe以命令行形式实现文件、文件夹的解压缩,布布扣,bubuko.com
c#调用 WinRAR.exe以命令行形式实现文件、文件夹的解压缩
原文:http://www.cnblogs.com/zhoub/p/3841276.html