using log4net;
using Microsoft.Win32;
using SharpCompress.Archive;
using SharpCompress.Archive.Zip;
using SharpCompress.Common;
using SharpCompress.Reader;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace CPS.Helper
{
    class CompressHelper
    {
        #region 委托 事件

        public delegate void WinRarExitedEventHandler(bool flag, string dirPath);
        public event WinRarExitedEventHandler WinRarExited;

        private void OnWinRarExited(bool flag, string path)
        {
            WinRarExitedEventHandler handler = WinRarExited;
            if (null != handler)
            {
                handler(flag, path);
            }
        }

        #endregion

       

        /// <summary>
        /// 压缩为zip文件
        /// </summary>
        /// <param name="sourcePath">要压缩的文件夹路径</param>
        /// <param name="destinationPath">存放压缩后的文件的路径</param>
        public void CompressZIP(string sourcePath, string destinationPath)
        {
            using (ZipArchive archive = ZipArchive.Create())
            {
                archive.AddAllFromDirectory(sourcePath);
                //archive.SaveTo(destinationPath);
                archive.SaveTo(null, null);
            }
        }

        /// <summary>
        /// 解压缩zip文件
        /// </summary>
        /// <param name="sourcePath">要解压的文件的路径</param>
        /// <param name="destinationPath">存放解压后的文件的路径</param>
        public void DecompressZIP(string sourcePath, string destinationPath)
        {
            IArchive archive = ArchiveFactory.Open(sourcePath);
            foreach (IArchiveEntry entry in archive.Entries)
            {
                if (!entry.IsDirectory)
                {
                    entry.WriteToDirectory(destinationPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
                }
            }
        }

        /// <summary>
        /// 解压缩rar文件
        /// </summary>
        /// <param name="sourcePath">要解压的文件的路径</param>
        /// <param name="destinationPath">存放解压后的文件的路径</param>
        public bool DecompressRAR(string sourcePath, string destinationPath)
        {
            try
            {
                using (Stream stream = File.OpenRead(sourcePath))
                {
                    string[] temp = null;
                    string filePath = string.Empty;
                    IReader reader = ReaderFactory.Open(stream);
                    while (reader.MoveToNextEntry())
                    {
                        if (!reader.Entry.IsDirectory)
                        {
                            temp = reader.Entry.FilePath.Split('\\');
                            filePath = destinationPath + "\\" + temp[temp.Length - 1];
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                            reader.WriteEntryToFile(filePath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
           
        }

        /// <summary>
        /// 判断是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        private bool Exists()
        {
            RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            return !string.IsNullOrEmpty(reg.GetValue("").ToString());
        }

       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Process_Exited(object sender, EventArgs e)
        {
            Process process = sender as Process;

            try
            {
                if (process.HasExited && process.ExitCode == 0)
                {
                    OnWinRarExited(true, "");
                    return;
                }

                process.Kill();
            }
            catch (Exception ex)
            {
               
            }

            OnWinRarExited(false, "");
        }
    }
}

 

相关文章:

  • 2022-01-19
  • 2022-12-23
  • 2021-12-02
  • 2021-12-31
  • 2021-09-29
  • 2021-08-23
  • 2021-09-16
  • 2022-03-06
猜你喜欢
  • 2021-06-25
  • 2021-07-28
  • 2021-11-25
  • 2022-03-09
  • 2021-09-06
  • 2021-12-05
相关资源
相似解决方案