当压缩文件过多的时候,我们不能手动一个个去解压,所以我们需要一个解压程序来实现快速压缩,本文就演示如果使用C#调用7zip来解压文件。
来解压程序前,你需要安装7-zip,这是一个免费的解压缩软件,是国际通用的。然后我们就可以开始我们的程序了。
首先我们需要一个Helper程序来实现压缩,看下面的代码
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace DailyBackup { public class ZipHelper { // Fields private string _7zInstallPath = ""; // Methods public ZipHelper(string str7zInstallPath) { this._7zInstallPath = str7zInstallPath; } ////// 压缩文件夹目录 /// /// 指定需要压缩的目录,如C:\test\,将压缩test目录下的所有文件 /// 压缩后压缩文件的存放目录 public void CompressDirectory(string strInDirectoryPath, string strOutFilePath) { Process process = new Process(); process.StartInfo.FileName = this._7zInstallPath; process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInDirectoryPath + " -r"; //隐藏DOS窗口 process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); process.WaitForExit(); process.Close(); } ////// 压缩文件 /// /// 指定需要压缩的文件,如C:\test\demo.xlsx,将压缩demo.xlsx文件 /// 压缩后压缩文件的存放目录 public void CompressFile(string strInFilePath, string strOutFilePath) { Process process = new Process(); process.StartInfo.FileName = this._7zInstallPath; process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInFilePath + ""; //隐藏DOS窗口 process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); process.WaitForExit(); process.Close(); } ////// 解压缩 /// /// 压缩文件的路径 /// 解压缩后文件的路径 public void DecompressFileToDestDirectory(string strInFilePath, string strOutDirectoryPath) { Process process = new Process(); process.StartInfo.FileName = this._7zInstallPath; process.StartInfo.Arguments = " x " + strInFilePath + " -o" + strOutDirectoryPath + " -r "; //隐藏DOS窗口 process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); process.WaitForExit(); process.Close(); } } }
其中的构造函数参数_7zInstallPath就是7zip的安装目录下,7z.exe文件所在的路径。
然后我们还需要一个中间类来包装调用7zip的接口。
using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; namespace DailyBackup { public static class UZip { public static void Unzip(string compressFilePhysicalPath, string uZipFilePhysicalPath) { DirectoryInfo DirecInfo = new DirectoryInfo(compressFilePhysicalPath); if (DirecInfo.Exists) { foreach (FileInfo fileInfo in DirecInfo.GetFiles("*.zip")) { try { var zipHleper = new ZipHelper("7zip安装目录的7z.exe路径"); zipHleper.DecompressFileToDestDirectory(fileInfo.FullName, uZipFilePhysicalPath); } catch(Exception e) { } } } } } }
上面的代码我们写了一个压缩程序的静态类,Unzip方法的参数分别是压缩文件所在的目录,和把压缩文件解压后放置的目录。
所以我们在主程序可以这样用了:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.DirectoryServices; using System.Diagnostics; using Microsoft.Win32; using System.Configuration; using System.IO; using System.Globalization; using System.Threading; using DailyBackup; namespace Backup { ////// 备份IIS网站以及数据库 /// class Program { ////// 压缩文件存放路径 /// public static readonly string compressFilePhysicalPath = ConfigurationManager.AppSettings["CompressFilePhysicalPath"]; ///// 解压文件存放路径 /// public static readonly string uZipFilePhysicalPath = ConfigurationManager.AppSettings["UZipFilePhysicalPath"]; ////// Mian method. /// /// static void Main(string[] args) { try { UZip.Unzip(compressFilePhysicalPath, uZipFilePhysicalPath); } catch(Exception e) { Log.LogInfo(e.Message); } } } }