- using System;
- using Microsoft.Win32;
- using System.Windows.Forms;
- using System.Resources;
- using System.Reflection;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- namespace LocServices.Helper
- {
- /// <summary>
- /// Summary description for CompressionUtility.
- /// </summary>
- public class CompressionUtility
- {
- private const string WINZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/winzip32.exe" ;
- private const string WZZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/wzzip.exe" ;
- private const string WZUNZIP_LOCATION = @"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/wzunzip.exe" ;
- private const string WINZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/winzip32.exe";
- private const string WZZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/wzzip.exe";
- private const string WZUNZIP_LOCATION_64 = @"SOFTWARE/Wow6432Node/Microsoft/Windows/CurrentVersion/App Paths/wzunzip.exe";
- private string winZipExe = string.Empty;
- private string winUnZipExe = string.Empty;
- private Process zipProcess;
- public delegate void ProcessStartedDelegate(int processId);
- public event ProcessStartedDelegate ProcessStarted;
- /// <summary>
- /// Initialize a new instance of the compression class
- /// </summary>
- public CompressionUtility()
- {
- //Check if winzip is installed
- RegistryKey key = Registry.LocalMachine.OpenSubKey(WINZIP_LOCATION);
- RegistryKey key64 = Registry.LocalMachine.OpenSubKey(WINZIP_LOCATION_64);
- //if not found prompt for installation
- if (key == null && key64 == null)
- {
- string winZipNotInstalledMessage = string.Format(
- "WinZip not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
- WINZIP_LOCATION, WINZIP_LOCATION_64);
- throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
- }
- //key.Close();
- //key64.Close();
- // Check if the command line version of winzip is installed
- RegistryKey wzZipKey = Registry.LocalMachine.OpenSubKey(WZZIP_LOCATION);
- RegistryKey wzZipKey64 = Registry.LocalMachine.OpenSubKey(WZZIP_LOCATION_64);
- RegistryKey wzUnZipKey = Registry.LocalMachine.OpenSubKey(WZUNZIP_LOCATION);
- RegistryKey wzUnZipKey64 = Registry.LocalMachine.OpenSubKey(WZUNZIP_LOCATION_64);
- // if not found prompt for installation
- if (wzZipKey == null && wzZipKey64 == null)
- {
- string winZipNotInstalledMessage = string.Format(
- "WinZip command line utility not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
- WZZIP_LOCATION, WZZIP_LOCATION_64);
- throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
- }
- // again if this is not there prompt for installation
- if (wzUnZipKey == null && wzUnZipKey64 == null)
- {
- string winZipNotInstalledMessage = string.Format(
- "WinZip command line utility not installed!" + Environment.NewLine + "Searched at {0} and {1}.",
- WZUNZIP_LOCATION, WZUNZIP_LOCATION_64);
- throw (new WinZipNotInstalledException(winZipNotInstalledMessage));
- }
- // find the exe path for the command line utilities
- winZipExe = wzZipKey != null ? wzZipKey.GetValue(string.Empty).ToString() : wzZipKey64.GetValue(string.Empty).ToString();
- winUnZipExe = wzUnZipKey != null ? wzUnZipKey.GetValue(string.Empty).ToString() : wzUnZipKey64.GetValue(string.Empty).ToString();
- //wzZipKey.Close();
- //wzZipKey64.Close();
- //wzUnZipKey.Close();
- //wzUnZipKey64.Close();
- }
- /// <summary>
- /// Create a zip file of all the files in the passed folder
- /// and name it foldername.zip
- /// </summary>
- /// <param name="folderName"></param>
- public void ZipFile(string zipFileName, string folderName, out string[] stdOutput)
- {
- try
- {
- //if the zip file exists, delete it
- if (File.Exists(zipFileName))
- {
- File.Delete(zipFileName);
- }
- //now create the zip file that contains all the files in the specified folder
- zipProcess = new Process();
- zipProcess.StartInfo.Arguments = "-rp /"" + zipFileName + "/" /"" + folderName + "/"";
- zipProcess.StartInfo.FileName = winZipExe;
- zipProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- zipProcess.StartInfo.RedirectStandardOutput = true;
- zipProcess.StartInfo.UseShellExecute = false;
- zipProcess.StartInfo.CreateNoWindow = true;
- zipProcess.Start();
- //pass the id of the process back to the callee
- if(ProcessStarted != null)
- {
- ProcessStarted(zipProcess.Id);
- }
- string results = zipProcess.StandardOutput.ReadToEnd();
- zipProcess.WaitForExit();
- zipProcess.StandardOutput.Close();
- if(zipProcess.ExitCode != 0)
- {
- throw(new Exception("Zip Creation Failed"));
- }
- results = results.Trim().Replace('/r', '/0');
- stdOutput = results.Split('/n');
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// Unzip a zip file and put all the contents in the
- /// specified folder
- /// </summary>
- /// <param name="folderName"></param>
- public void UnZipFile(string zipFileName, string outFolderName, out string[] stdOutput)
- {
- //see if the zipFile exists
- if(!File.Exists(zipFileName))
- {
- throw new FileNotFoundException("Zipfile not found!", zipFileName);
- }
- //check if the out directory exists, if not create it
- if(!Directory.Exists(outFolderName))
- {
- Directory.CreateDirectory(outFolderName);
- }
- //now unzip the file to the directory
- Process zipProcess = new Process();
- zipProcess.StartInfo.Arguments = "-d -o -ybc /"" + zipFileName + "/" /"" + outFolderName + "/"";
- zipProcess.StartInfo.FileName = winUnZipExe;
- zipProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- zipProcess.StartInfo.RedirectStandardOutput = true;
- zipProcess.StartInfo.UseShellExecute = false;
- zipProcess.StartInfo.CreateNoWindow = true;
- zipProcess.Start();
- //pass the id of the process back to the callee
- if (ProcessStarted != null)
- {
- ProcessStarted(zipProcess.Id);
- }
- string results = zipProcess.StandardOutput.ReadToEnd();
- zipProcess.WaitForExit();
- zipProcess.StandardOutput.Close();
- if (zipProcess.ExitCode != 0)
- {
- throw(new Exception("Zip Creation Failed"));
- }
- results = results.Trim().Replace('/r', '/0');
- stdOutput = results.Split('/n');
- }
- }
- /// <summary>
- /// Create a new exception class for winzip not installed
- /// </summary>
- public class WinZipNotInstalledException : ApplicationException
- {
- public WinZipNotInstalledException(string message)
- : base(message)
- {
- }
- public WinZipNotInstalledException(string message, Exception inner)
- : base(message, inner)
- {
- }
- }
- }
对文件压缩解压操作
最新推荐文章于 2019-07-05 03:02:09 发布