程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 文件流壓縮解壓

C# 文件流壓縮解壓

編輯:C#入門知識

C# 文件流壓縮解壓


 /// 
    /// 文件流壓縮解壓
    /// 
    public class ZipHelper
    {
        public static int BEST_COMPRESSION = 9;
        public static int BEST_SPEED = 1;
        public static int DEFAULT_COMPRESSION = -1;
        public static int NO_COMPRESSION = 0;

        #region  Deflate壓縮

        #region Deflate壓縮
        /// 
        /// Deflate方式壓縮(默認壓縮級別最高)
        /// 
        ///
        /// 
        public static Stream Deflate(Stream stream)
        {
            return ZipHelper.Deflate(stream, ZipHelper.DEFAULT_COMPRESSION);
        }
        /// 
        ///  Deflate方式壓縮
        /// 
        ///
        ///壓縮品質級別(0~9)
        /// 
        public static Stream Deflate(Stream stream, int level)
        {
            byte[] array = ZipHelper.StreamToBytes(stream);
            byte[] array2 = new byte[array.Length];
            Deflater deflater = new Deflater();
            deflater.SetLevel(level);
            deflater.SetStrategy(DeflateStrategy.Default);
            deflater.SetInput(array);
            deflater.Finish();
            int num = deflater.Deflate(array2);
            byte[] array3 = new byte[num];
            Array.Copy(array2, array3, num);
            return ZipHelper.BytesToStream(array3);
        }

        /// 
        /// Deflate方式壓縮
        /// 
        ///
        ///壓縮品質級別(0~9)
        /// 
        public static byte[] Deflate(byte[] input, int level)
        {
            byte[] result;
            try
            {
                if (input == null && input.Length == 0)
                {
                    result = new byte[0];
                }
                else
                {
                    byte[] array = new byte[input.Length];
                    Deflater deflater = new Deflater();
                    deflater.SetLevel(level);
                    deflater.SetStrategy(DeflateStrategy.Default);
                    deflater.SetInput(input);
                    deflater.Finish();
                    int num = deflater.Deflate(array);
                    byte[] array2 = new byte[num];
                    Array.Copy(array, array2, num);
                    result = array2;
                }
            }
            catch (Exception innerException)
            {
                throw new Exception(壓縮程序出錯!, innerException);
            }
            return result;
        }
        #endregion

        #region Inflate解壓
        /// 
        /// Inflate解壓
        /// 
        ///
        /// 
        public static byte[] Inflate(byte[] input)
        {
            byte[] result;
            try
            {
                if (input == null && input.Length == 0)
                {
                    result = new byte[0];
                }
                else
                {
                    Inflater inflater = new Inflater();
                    inflater.SetInput(input);
                    byte[] array = new byte[1024];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        for (int i = inflater.Inflate(array, 0, array.Length); i > 0; i = inflater.Inflate(array, 0, array.Length))
                        {
                            memoryStream.Write(array, 0, i);
                        }
                        byte[] buffer = memoryStream.GetBuffer();
                        memoryStream.Close();
                        result = buffer;
                    }
                }
            }
            catch (Exception innerException)
            {
                throw new Exception(解壓縮程序出錯!, innerException);
            }
            return result;
        }
        /// 
        /// Inflate解壓
        /// 
        ///
        /// 
        public static Stream Inflate(Stream zipStream)
        {
            byte[] input = ZipHelper.StreamToBytes(zipStream);
            byte[] bytes = ZipHelper.Inflate(input);
            return ZipHelper.BytesToStream(bytes);
        }
        #endregion

        #endregion

        #region GZip壓縮
        /// 
        /// GZip壓縮
        /// 
        ///
        ///
        public static void GZipCompress(Stream srcStream, Stream output)
        {
            ZipHelper.GZipCompress(srcStream, 6, output);
        }
        /// 
        ///  GZip壓縮
        /// 
        ///
        ///壓縮品質級別(0~9)
        ///
        public static void GZipCompress(Stream srcStream, int compressLevel, Stream output)
        {
            if (compressLevel < 1 || compressLevel > 9)
            {
                throw new Exception(string.Format(您指定的壓縮級別 {0} 不在有效的范圍(1-9)內, compressLevel));
            }
            srcStream.Position = 0L;
            GZipOutputStream gZipOutputStream = new GZipOutputStream(output);
            gZipOutputStream.SetLevel(compressLevel);
            try
            {
                int i = 4096;
                byte[] buffer = new byte[i];
                while (i > 0)
                {
                    i = srcStream.Read(buffer, 0, i);
                    gZipOutputStream.Write(buffer, 0, i);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(GZip壓縮出錯: + ex.Message);
            }
            srcStream.Close();
            gZipOutputStream.Finish();
        }
        /// 
        ///  GZip解壓
        /// 
        ///
        ///
        public static void GZipDeCompress(Stream zipStream, Stream outputStream)
        {
            GZipInputStream gZipInputStream = new GZipInputStream(zipStream);
            try
            {
                int i = 4096;
                byte[] buffer = new byte[i];
                while (i > 0)
                {
                    i = gZipInputStream.Read(buffer, 0, i);
                    outputStream.Write(buffer, 0, i);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(GZip解壓縮出錯: + ex.Message);
            }
            zipStream.Close();
            gZipInputStream.Close();
        }
        #endregion

        #region  BZip2壓縮
        /// 
        /// BZip2壓縮
        /// 
        ///
        ///
        ///
        public static void BZip2Compress(Stream inStream, Stream outStream, int blockSize)
        {
            BZip2.Compress(inStream, outStream, blockSize);
        }
        /// 
        /// BZip2解壓
        /// 
        ///
        ///
        public static void BZip2Decompress(Stream inStream, Stream outStream)
        {
            BZip2.Decompress(inStream, outStream);
        }
        #endregion


        private static byte[] StreamToBytes(Stream stream)
        {
            byte[] array = new byte[stream.Length];
            stream.Seek(0L, SeekOrigin.Begin);
            stream.Read(array, 0, array.Length);
            stream.Close();
            return array;
        }
        private static Stream BytesToStream(byte[] bytes)
        {
            return new MemoryStream(bytes);
        }
        private static void StreamToFile(Stream stream, string fileName)
        {
            byte[] array = new byte[stream.Length];
            stream.Read(array, 0, array.Length);
            stream.Seek(0L, SeekOrigin.Begin);
            FileStream fileStream = new FileStream(fileName, FileMode.Create);
            BinaryWriter binaryWriter = new BinaryWriter(fileStream);
            binaryWriter.Write(array);
            binaryWriter.Close();
            fileStream.Close();
        }
        private static Stream FileToStream(string fileName)
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] array = new byte[fileStream.Length];
            fileStream.Read(array, 0, array.Length);
            fileStream.Close();
            return new MemoryStream(array);
        }
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved