using System.IO.Compression;
#region 檔案壓縮
/// <summary>
/// 檔案壓縮
/// </summary>
/// <param name="strDFile">壓縮前檔案及路徑</param>
/// <param name="strCFile">壓縮後檔案及路徑</param>
public void compressFile(string strDFile, string strCFile)
{
if (!File.Exists(strDFile)) throw new FileNotFoundException(); //先檢查檔案有無存在
using (FileStream sourceStream = new FileStream(strDFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(strCFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}
#endregion
留言列表