对文件压缩解压操作

 
  1. using System;
  2. using Microsoft.Win32;
  3. using System.Windows.Forms;
  4. using System.Resources;
  5. using System.Reflection;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Threading;
  9. namespace LocServices.Helper
  10. {
  11.     /// <summary>
  12.     /// Summary description for CompressionUtility.
  13.     /// </summary>
  14.     public class CompressionUtility
  15.     {
  16.         private const string WINZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/winzip32.exe" ;
  17.         private const string WZZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/wzzip.exe" ;
  18.         private const string WZUNZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/wzunzip.exe" ;
  19.         private const string WINZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/winzip32.exe";
  20.         private const string WZZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/wzzip.exe";
  21.         private const string WZUNZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/wzunzip.exe";
  22.         private string winZipExe = string.Empty;
  23.         private string winUnZipExe = string.Empty;
  24.         private Process zipProcess;
  25.         public delegate void ProcessStartedDelegate(int processId);
  26.         public event ProcessStartedDelegate ProcessStarted;
  27.         /// <summary>
  28.         /// Initialize a new instance of the compression class
  29.         /// </summary>
  30.         public CompressionUtility()
  31.         {
  32.             //Check if winzip is installed
  33.             RegistryKey key = Registry.LocalMachine.OpenSubKey(WINZIP_LOCATION);
  34.             RegistryKey key64 = Registry.LocalMachine.OpenSubKey(WINZIP_LOCATION_64);
  35.             
  36.             //if not found prompt for installation  
  37.             if (key == null && key64 == null)
  38.             {
  39.                 string winZipNotInstalledMessage = string.Format(
  40.                     "WinZip not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
  41.                     WINZIP_LOCATION, WINZIP_LOCATION_64);
  42.                 throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
  43.             }
  44.             //key.Close();
  45.             //key64.Close();
  46.             // Check if the command line version of winzip is installed
  47.             RegistryKey wzZipKey = Registry.LocalMachine.OpenSubKey(WZZIP_LOCATION);
  48.             RegistryKey wzZipKey64 = Registry.LocalMachine.OpenSubKey(WZZIP_LOCATION_64);
  49.             RegistryKey wzUnZipKey = Registry.LocalMachine.OpenSubKey(WZUNZIP_LOCATION);
  50.             RegistryKey wzUnZipKey64 = Registry.LocalMachine.OpenSubKey(WZUNZIP_LOCATION_64);
  51.             // if not found prompt for installation
  52.             if (wzZipKey == null && wzZipKey64 == null)
  53.             {
  54.                 string winZipNotInstalledMessage = string.Format(
  55.                     "WinZip command line utility not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
  56.                     WZZIP_LOCATION, WZZIP_LOCATION_64);
  57.                 throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
  58.             }
  59.             // again if this is not there prompt for installation
  60.             if (wzUnZipKey == null && wzUnZipKey64 == null)
  61.             {
  62.                 string winZipNotInstalledMessage = string.Format(
  63.                     "WinZip command line utility not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
  64.                     WZUNZIP_LOCATION, WZUNZIP_LOCATION_64);
  65.                 throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
  66.             }
  67.             // find the exe path for the command line utilities
  68.             winZipExe = wzZipKey != null ? wzZipKey.GetValue(string.Empty).ToString() : wzZipKey64.GetValue(string.Empty).ToString();
  69.             winUnZipExe = wzUnZipKey != null ? wzUnZipKey.GetValue(string.Empty).ToString() : wzUnZipKey64.GetValue(string.Empty).ToString();
  70.             //wzZipKey.Close();
  71.             //wzZipKey64.Close();
  72.             //wzUnZipKey.Close();
  73.             //wzUnZipKey64.Close();
  74.         }
  75.     
  76.         /// <summary>
  77.         /// Create a zip file of all the files in the passed folder
  78.         /// and name it foldername.zip
  79.         /// </summary>
  80.         /// <param name="folderName"></param>
  81.         public void ZipFile(string zipFileName, string folderName, out string[] stdOutput)
  82.         {
  83.             try
  84.             {
  85.                 //if the zip file exists, delete it
  86.                 if (File.Exists(zipFileName))
  87.                 {
  88.                     File.Delete(zipFileName);
  89.                 }
  90.                 //now create the zip file that contains all the files in the specified folder
  91.      
  92.                 zipProcess = new Process();
  93.                 zipProcess.StartInfo.Arguments = "-rp /"" + zipFileName + "/" /"" + folderName + "/"";
  94.                 zipProcess.StartInfo.FileName = winZipExe;
  95.                 zipProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  96.                 zipProcess.StartInfo.RedirectStandardOutput = true;
  97.                 zipProcess.StartInfo.UseShellExecute = false;
  98.                 zipProcess.StartInfo.CreateNoWindow = true;
  99.                 zipProcess.Start();
  100.                 //pass the id of the process back to the callee
  101.                 if(ProcessStarted != null)
  102.                 {
  103.                     ProcessStarted(zipProcess.Id);
  104.                 }
  105.                 string results = zipProcess.StandardOutput.ReadToEnd();
  106.                 zipProcess.WaitForExit();
  107.                 zipProcess.StandardOutput.Close();
  108.                 if(zipProcess.ExitCode != 0)
  109.                 {
  110.                     throw(new Exception("Zip Creation Failed"));
  111.                 }
  112.                 results = results.Trim().Replace('/r''/0');
  113.                 stdOutput = results.Split('/n');
  114.             }
  115.             catch
  116.             {
  117.                 throw;
  118.             }
  119.         }
  120.         
  121.         /// <summary>
  122.         /// Unzip a zip file and put all the contents in the
  123.         /// specified folder
  124.         /// </summary>
  125.         /// <param name="folderName"></param>
  126.         public void UnZipFile(string zipFileName, string outFolderName, out string[] stdOutput)
  127.         {
  128.             //see if the zipFile exists
  129.             if(!File.Exists(zipFileName))
  130.             {
  131.                 throw new FileNotFoundException("Zipfile not found!", zipFileName);
  132.             }
  133.             //check if the out directory exists, if not create it
  134.             if(!Directory.Exists(outFolderName))
  135.             {
  136.                 Directory.CreateDirectory(outFolderName);
  137.             }
  138.             //now unzip the file to the directory    
  139.             Process zipProcess = new Process();
  140.             zipProcess.StartInfo.Arguments = "-d -o -ybc /"" + zipFileName + "/" /"" + outFolderName + "/"";
  141.             zipProcess.StartInfo.FileName = winUnZipExe;
  142.             zipProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  143.             zipProcess.StartInfo.RedirectStandardOutput = true;
  144.             zipProcess.StartInfo.UseShellExecute = false;
  145.             zipProcess.StartInfo.CreateNoWindow = true;
  146.             zipProcess.Start();
  147.         
  148.             //pass the id of the process back to the callee
  149.             if (ProcessStarted != null)
  150.             {
  151.                 ProcessStarted(zipProcess.Id);
  152.             }
  153.             string results = zipProcess.StandardOutput.ReadToEnd();
  154.             zipProcess.WaitForExit();
  155.             zipProcess.StandardOutput.Close();
  156.             if (zipProcess.ExitCode != 0)
  157.             {
  158.                 throw(new Exception("Zip Creation Failed"));
  159.             }
  160.             results = results.Trim().Replace('/r''/0');
  161.             stdOutput = results.Split('/n');
  162.         }
  163.     }
  164.     
  165.     /// <summary>
  166.     /// Create a new exception class for winzip not installed
  167.     /// </summary>
  168.     public class WinZipNotInstalledException : ApplicationException
  169.     {
  170.      
  171.         public WinZipNotInstalledException(string message)
  172.             : base(message)
  173.         {
  174.         }
  175.         public WinZipNotInstalledException(string message, Exception inner)
  176.             : base(message, inner)
  177.         {
  178.         }
  179.     }
  180. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值