星期一, 12月 12, 2016

[C#] 利用 GZipStram 進行壓縮與解壓縮

根據 如何:壓縮與解壓縮檔案 這篇文章來學習 GZipStream 壓縮、解壓縮檔案

WinFomr Layout

[C#] 壓縮與解壓縮-1


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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // 路徑預設值
            txtCom_SourceDir.Text = @"D:\GZipTemp";
            txtCom_DestDir.Text = @"D:\GZipTemp\壓縮";
            txtDeCom_SourceDir.Text = @"D:\GZipTemp\壓縮";
            txtDeCom_DestDir.Text = @"D:\GZipTemp\解壓縮";

            btnCom_SourceDir.Click += btnClick;
            btnCom_DestDir.Click += btnClick;
            btnDeCom_SourceDir.Click += btnClick;
            btnDeCom_DestDir.Click += btnClick;
        }

        private void btnClick(object sender, EventArgs e)
        {
            string path = string.Empty;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.RootFolder = Environment.SpecialFolder.Desktop;
            if (fbd.ShowDialog() == DialogResult.OK)
                path = fbd.SelectedPath;

            string btnName = (sender as Button).Name;
            switch (btnName)
            {
                case "btnCom_SourceDir":
                    txtCom_SourceDir.Text = path;
                    break;
                case "btnCom_DestDir":
                    txtCom_DestDir.Text = path;
                    break;
                case "btnDeCom_SourceDir":
                    txtDeCom_SourceDir.Text = path;
                    break;
                case "btnDeCom_DestDir":
                    txtDeCom_DestDir.Text = path;
                    break;
                default:
                    break;
            }
        }

        private void btnCompress_Click(object sender, EventArgs e)
        {
            string Com_SourceDir = txtCom_SourceDir.Text.Trim();
            string Com_DestDir = txtCom_DestDir.Text.Trim();

            if (Directory.Exists(Com_SourceDir) == false
                || Directory.Exists(Com_DestDir) == false) return;

            RunCompress(
                new DirectoryInfo(Com_SourceDir),
                Com_DestDir);
        }

        private void btnDeCompress_Click(object sender, EventArgs e)
        {
            string DeCom_SourceDir = txtDeCom_SourceDir.Text.Trim();
            string DeCom_DestDir = txtDeCom_DestDir.Text.Trim();

            if (Directory.Exists(DeCom_SourceDir) == false
                || Directory.Exists(DeCom_DestDir) == false) return;

            RunDeCompress(
                new DirectoryInfo(DeCom_SourceDir),
                DeCom_DestDir);
        }

        private void RunCompress(DirectoryInfo directorySelected, string DestDir)
        {
            foreach (FileInfo fileToCompress in directorySelected.GetFiles())
            {
                RunCompress(fileToCompress, DestDir);
            }
        }

        private void RunCompress(FileInfo fileToCompress, string DestDir)
        {
            if (fileToCompress.Extension == ".gz") return;
            if (File.GetAttributes(fileToCompress.FullName) == FileAttributes.Hidden) return;

            // 壓縮時要保留原檔案副檔名,解壓縮時才能還元檔案副檔名
            string compressFileFullName = Path.Combine(DestDir, fileToCompress.Name) + ".gz";

            using (FileStream originalFileStream = fileToCompress.OpenRead())
            using (FileStream compressedFileStream = File.Create(compressFileFullName))
            using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
            {
                originalFileStream.Position = 0;
                originalFileStream.CopyTo(compressionStream);
            }

            // 顯示訊息
            FileInfo info = new FileInfo(compressFileFullName);
            listBox1.Items.Add($"檔案名稱:{compressFileFullName}; 檔案大小:{fileToCompress.Length} => {info.Length}");
        }

        private void RunDeCompress(DirectoryInfo directorySelected, string DestDir)
        {
            foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
            {
                RunDeCompress(fileToDecompress, DestDir);
            }
        }

        private void RunDeCompress(FileInfo fileToDecompress, string DestDir)
        {
            if (fileToDecompress.Extension != ".gz") return;

            string decompressFileFullName = Path.Combine(DestDir, Path.GetFileNameWithoutExtension(fileToDecompress.FullName));

            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            using (FileStream decompressedFileStream = File.Create(decompressFileFullName))
            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(decompressedFileStream);
            }

            // 顯示訊息
            FileInfo info = new FileInfo(decompressFileFullName);
            listBox1.Items.Add($"檔案名稱:{decompressFileFullName}; 檔案大小:{fileToDecompress.Length} => {info.Length}");
        }
    }
}
[C#] 壓縮與解壓縮-2

沒有留言:

張貼留言