[引入using System.Diagnostics;using Microsoft.Win32;]
#region 使用GZip压缩文件,返回bool值
/// <param name="FileSource">要压缩的源文件名</param>
/// <param name="FileTarget">压缩后的文件名</param>
/// <returns>压缩后的成功类型</returns>
public static bool GZipFile(string FileSource, string FileTarget)
{
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myComStream = null;
try
{
myStream = new FileStream(FileSource, FileMode.Open, FileAccess.Read, FileShare.Read);
myByte = new byte[myStream.Length];
myStream.Read(myByte, 0, myByte.Length);
myDesStream = new FileStream(FileTarget, FileMode.OpenOrCreate, FileAccess.Write);
myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
myComStream.Write(myByte, 0, myByte.Length);
return true;
}
catch(Exception ex)
{
exceptionMessage = ex.Message;
return false;
}
finally
{
myStream.Close();
myComStream.Close();
myDesStream.Close();
}
}
#endregion
#region 使用GZip解压文件,返回bool值
/// <summary>
/// 使用GZip解压文件,返回bool值
/// </summary>
/// <param name="FileSource">要解压的源文件名</param>
/// <param name="FileTarget">要解压的文件名</param>
/// <returns>解压的成功类型</returns>
public static bool GZipSFile(string FileSource, string FileTarget)
{
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myDeComStream = null;
try
{
myStream = new FileStream(FileSource, FileMode.Open);
myDeComStream = new GZipStream(myStream, CompressionMode.Decompress, true);
myByte = new byte[4];
int myPosition = (int)myStream.Length - 4;
myStream.Position = myPosition;
myStream.Read(myByte, 0, 4);
myStream.Position = 0;
int myLength = BitConverter.ToInt32(myByte, 0);
byte[] myData = new byte[myLength + 100];
int myOffset = 0;
int myTotal = 0;
while (true)
{
int myBytesRead = myDeComStream.Read(myData, myOffset, 100);
if (myBytesRead == 0)
break;
myOffset += myBytesRead;
myTotal += myBytesRead;
}
myDesStream = new FileStream(FileTarget, FileMode.Create);
myDesStream.Write(myData, 0, myTotal);
myDesStream.Flush();
return true;
}
catch(Exception ex)
{
exceptionMessage = ex.Message;
return false;
}
finally
{
myStream.Close();
myDeComStream.Close();
myDesStream.Close();
}
}
#endregion
[目标机器上必须装有winrar压缩软件]
#region 使用WinRAR压缩文件
/// <summary>
/// 使用WinRAR压缩文件
/// </summary>
/// <param name="FileSource">要压缩的源文件名</param>
/// <param name="FileTarget">压缩后的文件名</param>
/// <returns>压缩的成功类型</returns>
public static bool RarFile(string FileSource, string FileTarget)
{
String myRar;
RegistryKey myReg;
Object myObj;
String myInfo;
ProcessStartInfo myStartInfo;
Process myProcess;
try
{
myReg = Registry.ClassesRoot.OpenSubKey("Applications\WinRAR.exe\Shell\Open\Command");
myObj = myReg.GetValue("");
myRar = myObj.ToString();
myReg.Close();
myRar = myRar.Substring(1, myRar.Length - 7);
myInfo = " a " + FileTarget + " " + FileSource;
myStartInfo = new ProcessStartInfo();
myStartInfo.FileName = myRar;
myStartInfo.Arguments = myInfo;
myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess = new Process();
myProcess.StartInfo = myStartInfo;
myProcess.Start();
return true;
}
catch(Exception ex)
{
exceptionMessage = ex.Message;
return false;
}
}
#endregion
#region 使用WinRAR解压文件
/// <summary>
/// 使用WinRAR解压文件
/// </summary>
/// <param name="FileSource">要解压的源文件名</param>
/// <param name="FileTarget">解压后的文件名</param>
/// <returns>解压的成功类型</returns>
public static bool RarSFile(string FileSource, string FileTarget)
{
String myRar;
RegistryKey myReg;
Object myObj;
String myInfo;
ProcessStartInfo myStartInfo;
Process myProcess;
try
{
myReg = Registry.ClassesRoot.OpenSubKey("Applications\WinRar.exe\Shell\Open\Command");
myObj = myReg.GetValue("");
myRar = myObj.ToString();
myReg.Close();
myRar = myRar.Substring(1, myRar.Length - 7);
myInfo = " X " + FileSource+ " " + FileTarget + "\";
myStartInfo = new ProcessStartInfo();
myStartInfo.FileName = myRar;
myStartInfo.Arguments = myInfo;
myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess = new Process();
myProcess.StartInfo = myStartInfo;
myProcess.Start();
return true;
}
catch(Exception ex)
{
exceptionMessage = ex.Message;
return false;
}
}
#endregion
转载原文:http://www.zhixing123.cn/net/16278.html