C#壓縮與解壓縮流類 GZipStream 的使用

來源:互聯網
上載者:User

  在使用 GZipStream 進行壓縮的時候, 在最後必須調用 Close()方法, 否則會發現解壓縮後少一個位元組, 當壓縮的檔案小於4kb時, 解壓縮到檔案長度為0.

  下面為一個完整的壓縮與解壓縮檔案的代碼, 以做參考:

        private void button1_Click(object sender, EventArgs e)        {            string fileName = textBox1.Text; // @"f:\response.txt";            FileInfo fi = new FileInfo(fileName);            listBox1.Items.Add("source file length: " + fi.Length.ToString());            byte[] buf = Compress(fileName);            listBox1.Items.Add(String.Format("compress length: {0}, compress rate: {1:f2}%" ,buf.Length, (double)buf.Length * 100 / (double)fi.Length));            string newFileName = String.Format(@"{0}\{1}_new{2}",                 Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));             listBox1.Items.Add("decompress length: " + Decompress(buf, newFileName).ToString());        }        public static byte[] Compress(string fileName)        {            //壓縮後的MemoryStream            MemoryStream ms = new MemoryStream();            // 寫入壓縮            GZipStream compressedStream = new GZipStream(ms, CompressionMode.Compress, true);            FileStream fs = new FileStream(fileName, FileMode.Open);            byte[] buf = new byte[1024];            int count = 0;            do            {                count = fs.Read(buf, 0, buf.Length);                compressedStream.Write(buf, 0, count);            }            while (count > 0);            fs.Close();                        compressedStream.Close();            return ms.ToArray();        }        public static int Decompress(byte[] data, string fileName)        {            int iRet = 0;            byte[] buf = new byte[1024 * 1024];            try            {                FileStream fs = new FileStream(fileName, FileMode.Create);                MemoryStream ms = new MemoryStream(data);                GZipStream decompressedStream = new GZipStream(ms, CompressionMode.Decompress,true);                int count = 0;                do                {                    count = decompressedStream.Read(buf, 0, buf.Length);                    fs.Write(buf, 0, count);                    fs.Flush();                }                while (count > 0);                                               iRet = (int)fs.Length;                fs.Close();            }            finally            {            }            return iRet;        }        private void button3_Click(object sender, EventArgs e)        {            OpenFileDialog dialog = new OpenFileDialog()            {                InitialDirectory = Environment.CurrentDirectory,                Multiselect = false,            };            if (dialog.ShowDialog() == DialogResult.OK)            {                textBox1.Text = dialog.FileName;            }        } 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.