C# - 使用 GZipStream 壓縮與解壓縮 TextFile

摘要:C# - 使用 GZipStream 壓縮與解壓縮 TextFile

最近專案的關係,看到有人使用 GZipStream 來做解壓縮的動作,讓小弟好奇了一下其作法為何!? 遍尋文章才發現其實還滿好做的且行之有年,以下就來實作這個功能...

步驟一:在 C: 下建立一個 TextFile 檔


步驟二:建立一個 WinForm 並且建立壓縮與解壓縮的按鈕


步驟三:開始撰寫程式

Code:

using System.IO;
using System.IO.Compression;

namespace GZipStreamClass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strReadTextContent = File.ReadAllText(@"C:\張小呆的碎碎唸.txt");

            CompressStringToFile(@"C:\張小呆的碎碎唸.gz", strReadTextContent);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DecompressStringToFile(@"C:\張小呆的碎碎唸.gz", @"C:\解壓縮後的檔案.txt");
        }

        public static void CompressStringToFile(string strCompressFileName, string strFileContent)
        {
            try
            {
                //資料暫存的路徑
                string tempFilePath = Path.GetTempFileName();

                //將建立新檔案,將指定的資料寫入檔案中
                File.WriteAllText(tempFilePath, strFileContent);

                byte[] b;

                //開啟在新建立的檔案,並且將資料資料全部讀取
                using (FileStream f_ReadTempFileContent = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
                {
                    b = new byte[f_ReadTempFileContent.Length];
                    f_ReadTempFileContent.Read(b, 0, (int)f_ReadTempFileContent.Length);
                }

                //建立一個 *.gz 的檔案
                using (FileStream f_CompressFileName = new FileStream(strCompressFileName, FileMode.Create))
               
                //開始進行壓縮
                using (GZipStream gz = new GZipStream(f_CompressFileName, CompressionMode.Compress, false))
                {
                    gz.Write(b, 0, b.Length);
                }

                MessageBox.Show("完成壓縮!!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.ToString(), "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static void DecompressStringToFile(string strCompressFileName, string strDecompressFileName)
        {
            int count = 0;
            int bufferSize = 4096;
            byte[] buffer = new byte[4096];

            try
            {
                //讀取壓縮的 *.gz 的檔案
                using (FileStream fs_CompressFile = new FileStream(strCompressFileName, FileMode.Open, FileAccess.Read, FileShare.Read))

                //建立一個解壓縮後的檔案路徑與名稱
                using (FileStream fs_DecompressFile = new FileStream(strDecompressFileName, FileMode.Create, FileAccess.Write, FileShare.None))

                //解壓縮 *.gz 的檔案
                using (GZipStream gz = new GZipStream(fs_CompressFile, CompressionMode.Decompress, true))

                //讀取 *.gz 中的壓縮檔內容,並且寫入到新建立的檔案當中
                //直到內容結束為止
                while (true)
                {
                    count = gz.Read(buffer, 0, bufferSize);

                    if (count != 0)
                        fs_DecompressFile.Write(buffer, 0, count);

                    if (count != bufferSize)
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.ToString(), "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            MessageBox.Show("完成壓縮!!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

結果:





參考:
GZipStream 類別
How to: Compress and Decompress Data
Unzipping compressed files using GZipStream
DeCompress a text file using GZipStream