.NET 使用Process调用7_zip解压文件

对于我来说,注意用词!对于我!来说!市面上最好的文件解压缩软件就是7-zip,它不仅小而且相当强大。

7Zip官网【https://www.7-zip.org/】

7-zip支持的格式有

  • 解压缩: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM
  • 仅解压: AR, ARJ, CAB, CHM, CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, UDF, UEFI, VDI, VHD, VMDK, WIM, XAR and Z.

以下是使用代码,注意注释

using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;

namespace _7ZipTest
{
    class Program
    {
        static Stopwatch watch = new Stopwatch();
        static void Main(string[] args)
        {
           //var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7ZipInstaller\\01.rar");
            //var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7ZipInstaller\\02.7z");
            //var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7ZipInstaller\\03.zip");
            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7ZipInstaller\\04.rar");         
            watch.Start();
            try
            {
                _7ZipHelper.UnZipFile(new FileInfo(filePath));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("正在解压缩...\r\n");
            Console.Read();
        }
        public static void Process_Exited(object sender, EventArgs e)
        {
            watch.Stop();
            Console.WriteLine($"\r\n解压缩完成!耗时:{watch.ElapsedMilliseconds} (ms)");
            Console.ReadKey();
        }
    }
    public class _7ZipHelper
    {
        /// <summary>
        /// 7Zip官网【https://www.7-zip.org/】下载的7z管理器安装程序
        /// </summary>
        private static string _7ZipExePath => ConfigurationManager.AppSettings[nameof(_7ZipExePath)];
        private static string _7UnZipPath => ConfigurationManager.AppSettings[nameof(_7UnZipPath)];

        /// <summary>
        /// 参数 【x】 从压缩文件中解压缩,包含目录结构 
        /// 解压缩c.7z到E:\Program Files: 
        /// 7z x c.7z -oE:\"Program Files" (-o表示输出目录,其与目录路径之间没有空格)这样解压包含下级目录名,
        /// 但不会在E:\Program Files下新建一个c文件夹,
        /// 如果需要,就把输出目录设为E:\Program Files\c,这样会自动创建文件夹c。
        /// </summary>
        /// <param name="fileInfo"></param>
        public static void UnZipFile(FileInfo fileInfo)
        {
            if (fileInfo.Exists)
            {
                Process process = new Process();
                process.EnableRaisingEvents = true;

                // 将当前信息输入到此Process中必须禁用操作系统外壳程序  
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;

                process.StartInfo.FileName = _7ZipExePath;
                process.StartInfo.Arguments = $@" x {fileInfo.FullName} -o{_7UnZipPath}\{fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf('.'))}";
                process.Start();
                
                // 获取命令行内容  
                process.BeginOutputReadLine();
                process.OutputDataReceived += Process_OutputDataReceived;

                //7zip推出后执行事件
                process.Exited += Program.Process_Exited; ;
            }
            else
            {
                Console.WriteLine($"未找到文件:【{fileInfo.FullName}】");
            }
        }

        private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                Console.WriteLine(e.Data);
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xuxuzhaozhao/p/10341212.html