C#解壓縮文件實現

fp34 9年前發布 | 1K 次閱讀 C#

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

 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;
                /*壓縮文件的流的最后四個字節保存的是文件未壓縮前的長度信息,
                 * 把該字節數組轉換成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
    }
}

 本文由用戶 fp34 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!