对文件压缩解压操作
2008-09-04 16:49
369 查看
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)
{
}
}
}
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)
{
}
}
}
相关文章推荐
- XZ文件操作(压缩、解压)
- Zip压缩文件 与 解压 (MFC 文件操作 四 )
- Windows系统下Python压缩和解压文件、文件夹操作
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- C#文件的压缩和解压(ZIP)使用DotNetZip封装类操作zip文件(创建/读取/更新)实例
- unix解压文件和压缩文件操作
- 使用QuaZip对文件进行压缩解压操作
- linux中对文件的操作命令之打包压缩与解压
- Zip压缩文件 与 解压 (MFC 文件操作 四 )
- Qt使用QuaZip对文件进行压缩解压操作
- Qt使用QuaZip对文件进行压缩解压操作
- linux之压缩文件,查看压缩文件,解压文件的操作命令
- IOS 压缩与解压字符串(文件) 操作
- Zip压缩文件 与 解压 (MFC 文件操作 四 )
- Java操作Ant压缩和解压文件及批量打包Anroid应用
- linux shell ------有关文件的操作(压缩和解压)
- linux文件/目录/压缩解压 操作指令
- Linux 基础操作(十一) ————文件的归档、压缩与解压及rsync
- c# 动态操作 zip\rar 文件,压缩解压组件使用
- VC 文件的解压和压缩操作