實現在線壓縮檔案的實現程式碼

來源:互聯網
上載者:User

標籤:

[引入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

 

實現在線壓縮檔案的實現程式碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.