C# 文件流压缩解压

简介: /// <summary> /// 文件流压缩解压 /// </summary> public class ZipHelper { public static int BEST_COMPRESSION = 9; public static int BEST_SPEED = 1; publi
 /// <summary>
    /// 文件流压缩解压
    /// </summary>
    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压缩
        /// <summary>
        /// Deflate方式压缩(默认压缩级别最高)
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static Stream Deflate(Stream stream)
        {
            return ZipHelper.Deflate(stream, ZipHelper.DEFAULT_COMPRESSION);
        }
        /// <summary>
        ///  Deflate方式压缩
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="level">压缩品质级别(0~9)</param>
        /// <returns></returns>
        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);
        }

        /// <summary>
        /// Deflate方式压缩
        /// </summary>
        /// <param name="input"></param>
        /// <param name="level">压缩品质级别(0~9)</param>
        /// <returns></returns>
        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解压
        /// <summary>
        /// Inflate解压
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// Inflate解压
        /// </summary>
        /// <param name="zipStream"></param>
        /// <returns></returns>
        public static Stream Inflate(Stream zipStream)
        {
            byte[] input = ZipHelper.StreamToBytes(zipStream);
            byte[] bytes = ZipHelper.Inflate(input);
            return ZipHelper.BytesToStream(bytes);
        }
        #endregion

        #endregion

        #region GZip压缩
        /// <summary>
        /// GZip压缩
        /// </summary>
        /// <param name="srcStream"></param>
        /// <param name="output"></param>
        public static void GZipCompress(Stream srcStream, Stream output)
        {
            ZipHelper.GZipCompress(srcStream, 6, output);
        }
        /// <summary>
        ///  GZip压缩
        /// </summary>
        /// <param name="srcStream"></param>
        /// <param name="compressLevel">压缩品质级别(0~9)</param>
        /// <param name="output"></param>
        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();
        }
        /// <summary>
        ///  GZip解压
        /// </summary>
        /// <param name="zipStream"></param>
        /// <param name="outputStream"></param>
        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压缩
        /// <summary>
        /// BZip2压缩
        /// </summary>
        /// <param name="inStream"></param>
        /// <param name="outStream"></param>
        /// <param name="blockSize"></param>
        public static void BZip2Compress(Stream inStream, Stream outStream, int blockSize)
        {
            BZip2.Compress(inStream, outStream, blockSize);
        }
        /// <summary>
        /// BZip2解压
        /// </summary>
        /// <param name="inStream"></param>
        /// <param name="outStream"></param>
        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);
        }
    }

目录
相关文章
|
27天前
|
前端开发
14_文件下载&文件流
14_文件下载&文件流
22 0
|
6月前
|
移动开发
二进制文件与文本文件的区别
二进制文件与文本文件的区别
|
4月前
C++IO流文件读写(文本文件,二进制文件)
C++IO流文件读写(文本文件,二进制文件)
40 0
|
8月前
|
Shell 开发工具
读取文件
读取文件
28 3
|
10月前
项目3(文件流)
项目3(文件流)
|
10月前
项目2(文件流)
项目2(文件流)
|
11月前
|
存储 iOS开发 C++
C++中文件操作与文件流
🐰文件操作与文件流 🏡文件流类和文件流对象 🏡文件的打开与关闭 🌸1.文件的打开 🌸2.文件的关闭 🏡对文本文件的操作 🏡对二进制文件的操作 🌸1.用成员函数write和read操作二进制文件 🌸2.随机访问二进制文件
|
移动开发 C++ Windows
C++读取文件
C++读取文件
|
iOS开发 C++
C++文件读写操作分析文本文件与二进制文件
文本文件 写文件 写文件步骤如下: 1. 包含头文件 #include <fstream> 2. 创建流对象 ofstream ofs; 3. 打开文件 ofs.open("文件路径",打开方式); 4. 写数据 ofs << "写入的数据"; 5. 关闭文件 ofs.close(); 文件打开方式: 打开方式 解释 ios::in 为读文件而打开文件 ios::out 为写文件而打开文件 ios::ate 初始位置:文件尾 ios::app 追加方式写文件 ios::trunc 如果文件存在先删除,再创建 ios::binary 二进制方式
342 0
C++文件读写操作分析文本文件与二进制文件
C#编程-35:写入读取文本文件
C#编程-35:写入读取文本文件

热门文章

最新文章