中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#解壓縮文件實(shí)現(xiàn)

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

在網(wǎng)上看到了用c#的Compression類來對文件進(jìn)行壓縮和解壓縮的代碼,其源碼都是直接把文件直接讀到內(nèi)存中,這樣如果文件過大就會(huì)造成內(nèi)存溢出,或者突然占用很大的內(nèi)存空間,下面的代碼對此進(jìn)行了修改,逐步讀取文件到內(nèi)存中,每次讀取1024*64個(gè)字節(jié)。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace ZipOrDeCompressHelper
{
    public static  class Zip
    {
        #region 壓縮、解壓縮文件
        /// <summary>
        /// 壓縮文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="destinationFile"></param>
        public static void CompressFile(string sourceFile, string destinationFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException();
            using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                //if (checkCount != buffer.Length) throw new ApplicationException();
                using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (GZipStream compressStream = new GZipStream(destinationStream, CompressionMode.Compress))
                    {
                        byte[] buffer = new byte[1024 * 64];
                        int checkCount = 0;
                        while ((checkCount = sourceStream.Read(buffer, 0, buffer.Length)) >= buffer.Length)
                        {
                            compressStream.Write(buffer, 0, buffer.Length);
                        }
                        compressStream.Write(buffer, 0, checkCount);
                    }
                }
            }
        }
        
        /// <summary>
        /// 解壓縮文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="destinationFile"></param>
        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];
                const int bufferLength = 1024 * 64;
                /*壓縮文件的流的最后四個(gè)字節(jié)保存的是文件未壓縮前的長度信息,
                 * 把該字節(jié)數(shù)組轉(zhuǎn)換成int型,可獲取文件長度。
                 * 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[1024*64];
                using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
                {
                    using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
                    {
                        int total = 0;
                        int bytesRead = 0;
                        while ((bytesRead = decompressedStream.Read(buffer, 0, bufferLength)) >= bufferLength)
                        {
                            destinationStream.Write(buffer, 0, bufferLength);
                        }
                        destinationStream.Write(buffer,0,bytesRead);
                        destinationStream.Flush();
                    }
                }
            }

        }
        #endregion
    }
}

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java文件下載

下一篇:Android中兩種播放聲音的方法