using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;
using System.IO.Compression;
/// <summary>
///decompressFile 的摘要说明
/// </summary>
public class decompressFile
{
	public decompressFile()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static void DecompressFile(string sourceFile, string destinationFile)
    {
        if (!File.Exists(sourceFile)) throw new FileNotFoundException();
        using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
        {
            byte[] quartetBuffer = new byte[4];
            int position = (int)sourceStream.Length - 4;
            sourceStream.Position = position;
            sourceStream.Read(quartetBuffer, 0, 4);
            sourceStream.Position = 0;
            int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
            byte[] buffer = new byte[checkLength + 100];
            using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
            {
                int total = 0;
                for (int offset = 0; ; )
                {
                    int bytesRead = decompressedStream.Read(buffer, offset, 100);
                    if (bytesRead == 0) break;
                    offset += bytesRead;
                    total += bytesRead;
                }
                using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
                {
                    destinationStream.Write(buffer, 0, total);
                    destinationStream.Flush();
                }
            }
        }
    }


}
