C# 调用7zip压缩

 
更多
public class ZipHelper
{
    // Fields
    private string _7zInstallPath = @"C:\Program Files\7-Zip\7z.exe";

    // Methods
    public ZipHelper(string str7zInstallPath)
    {
        this._7zInstallPath = str7zInstallPath;
        if (!File.Exists(this._7zInstallPath))
        {
            Console.WriteLine(String.Format("{0} 不存在.", this._7zInstallPath));
        }
    }

    /// <summary>
    /// 压缩文件夹目录
    /// </summary>
    /// <param name="strInDirectoryPath">指定需要压缩的目录,如C:\test\,将压缩test目录下的所有文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressDirectory(string strInDirectoryPath, string strOutFilePath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInDirectoryPath, strPassword);
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="strInFilePath">指定需要压缩的文件,如C:\test\demo.xlsx,将压缩demo.xlsx文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressFile(string strInFilePath, string strOutFilePath, string strPassword="",Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInFilePath, strPassword);
        //process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInFilePath + "";
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;   
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 解压缩
    /// </summary>
    /// <param name="strInFilePath">压缩文件的路径</param>
    /// <param name="strOutDirectoryPath">解压缩后文件的路径</param>
    public void DecompressFileToDestDirectory(string strInFilePath, string strOutDirectoryPath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        Console.WriteLine(this._7zInstallPath+String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword));
        process.StartInfo.Arguments = String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword);
        //隐藏DOS窗口
        //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

}public class ZipHelper
{
    // Fields
    private string _7zInstallPath = @"C:\Program Files\7-Zip\7z.exe";

    // Methods
    public ZipHelper(string str7zInstallPath)
    {
        this._7zInstallPath = str7zInstallPath;
        if (!File.Exists(this._7zInstallPath))
        {
            Console.WriteLine(String.Format("{0} 不存在.", this._7zInstallPath));
        }
    }

    /// <summary>
    /// 压缩文件夹目录
    /// </summary>
    /// <param name="strInDirectoryPath">指定需要压缩的目录,如C:\test\,将压缩test目录下的所有文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressDirectory(string strInDirectoryPath, string strOutFilePath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInDirectoryPath, strPassword);
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="strInFilePath">指定需要压缩的文件,如C:\test\demo.xlsx,将压缩demo.xlsx文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressFile(string strInFilePath, string strOutFilePath, string strPassword="",Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInFilePath, strPassword);
        //process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInFilePath + "";
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;   
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 解压缩
    /// </summary>
    /// <param name="strInFilePath">压缩文件的路径</param>
    /// <param name="strOutDirectoryPath">解压缩后文件的路径</param>
    public void DecompressFileToDestDirectory(string strInFilePath, string strOutDirectoryPath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        Console.WriteLine(this._7zInstallPath+String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword));
        process.StartInfo.Arguments = String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword);
        //隐藏DOS窗口
        //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

}public class ZipHelper
{
    // Fields
    private string _7zInstallPath = @"C:\Program Files\7-Zip\7z.exe";

    // Methods
    public ZipHelper(string str7zInstallPath)
    {
        this._7zInstallPath = str7zInstallPath;
        if (!File.Exists(this._7zInstallPath))
        {
            Console.WriteLine(String.Format("{0} 不存在.", this._7zInstallPath));
        }
    }

    /// <summary>
    /// 压缩文件夹目录
    /// </summary>
    /// <param name="strInDirectoryPath">指定需要压缩的目录,如C:\test\,将压缩test目录下的所有文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressDirectory(string strInDirectoryPath, string strOutFilePath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInDirectoryPath, strPassword);
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="strInFilePath">指定需要压缩的文件,如C:\test\demo.xlsx,将压缩demo.xlsx文件</param>
    /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
    public void CompressFile(string strInFilePath, string strOutFilePath, string strPassword="",Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        process.StartInfo.Arguments = String.Format(" a -t7z {0} {1} {2}", strOutFilePath, strInFilePath, strPassword);
        //process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInFilePath + "";
        //隐藏DOS窗口
        process.StartInfo.WindowStyle = displayRunWindow ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;   
        process.Start();
        process.WaitForExit();
        process.Close();
    }

    /// <summary>
    /// 解压缩
    /// </summary>
    /// <param name="strInFilePath">压缩文件的路径</param>
    /// <param name="strOutDirectoryPath">解压缩后文件的路径</param>
    public void DecompressFileToDestDirectory(string strInFilePath, string strOutDirectoryPath, string strPassword = "", Boolean displayRunWindow = true)
    {
        Process process = new Process();
        process.StartInfo.FileName = this._7zInstallPath;
        strPassword = strPassword.Equals("") ? "" : String.Format("-p{0}", strPassword);
        Console.WriteLine(this._7zInstallPath+String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword));
        process.StartInfo.Arguments = String.Format(" x {0} -o{1} -r {2}", strInFilePath, strOutDirectoryPath, strPassword);
        //隐藏DOS窗口
        //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }

}
打赏

本文固定链接: https://www.cxy163.net/archives/1323 | 绝缘体

该日志由 绝缘体.. 于 2016年09月14日 发表在 首页 分类下,
原创文章转载请注明: C# 调用7zip压缩 | 绝缘体

报歉!评论已关闭.