C# 文件流压缩解压

 /// <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);
        }
    }
时间: 2024-10-03 17:03:15

C# 文件流压缩解压的相关文章

在Java中操作Zip文件,压缩/解压

压缩 可随意转载,但请注明出处及作者SonyMusic2003.05.28==========================================================================在Java中操作Zip文件,压缩/解压 package test.nothing; import java.io.*;import java.util.*;import java.util.zip.*; import com.beaconsystem.util.*; impor

linux下文件夹压缩解压.tar , .gz , .tar.gz , .bz2 , .tar.bz2 , .bz , .tar.bz , .zip , .rar

.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------------------------------------- .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.t

电脑压缩解压文件设置技巧

  1. 解决好压解压的小麻烦 自动清理压缩包源文件 网上下载的软件多数都被压缩为Zip或RAR文件,每次安装需要解开压缩包才能执行其中的安装包.如果希望今后使用方便,就需将软件解压后保存.这样,原始的压缩包也就属多余.若下载的软件包多,解压后再去删除这些压缩包,就多了一道环节.如果在解压时顺手动添加一个参数项,即可免去手动清理压缩包源文件. 右击压缩包并选择"好压→解压到-"命令项,在解压参数设置窗口中点击"高级"选项卡,将"删除压缩文件"分组

C# txt或html文件 关于在线压缩解压问题

问题描述 如何实现A服务器将文件(txt/html)压缩后传到B服务器上并解压还原保存文件,解压文件不能在本地生成.有没有可能实现?如果觉得我秒速得不太清楚.以下是原始需要解决的问题.1.蜘蛛扒到的网站太大,需要节省流量压缩后才上传到服务器.2.压缩解压文件会耗费硬盘资源和CPU.所以希望全由线上程序处理 解决方案 解决方案二:无法回答,你先解释什么是不能在"本地"生成,什么又是全由"线上"程序处理,服务器A采用何种方式把文件上传到服务器B,你所能修改的又是哪部分的

压缩文件无法解压-PGM9.6模拟狗的压缩文件怎么无法解压啊?

问题描述 PGM9.6模拟狗的压缩文件怎么无法解压啊? 今天刚刚在这边下载到的PGM9.6模拟狗的压缩文件怎么无法解压啊?有哪位大师帮帮忙回答一下

Windows 7 RC中压缩解压软件的兼容性测试报告

测试背景: 2009年5月5日,微软正式发布了Windows 7 RC版.与Beta版不同,Windows 7 RC版在很多方面都跟未来发布的正式版几乎相同,因此,对于想把日常操作系统转移到Windows 7上的用户来说,Windows 7 RC将会是一个重要的版本,而在RC版上对常用软件进行兼容性测试也就有了实际的意义.因为很多人虽然对Windows 7蠢蠢欲动,但未知的软件兼容性使得他们对"搬家"到Windows 7望而却步.为了能让用户在转移到Windows 7之前有个参考,华军

C#制作CAB压缩包压缩解压类

代码如下: 以下为引用的内容: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace Com.ImYan.CabHelper { /// <summary> /// CAB文件压缩解压类 /// </summary> public class Cab { #region 属性列表 Propertie

Linux下压缩/解压方法大全

  linux下压缩包格式繁多,并且在命令行下没有Windows下使用那么方便,有时候解压文件的时候会突然忘掉命令,这里fcbu.com为大家收集了基本所有文件格式的解压方法和打包命令.需要的可以参考一下: .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar只是打包,没有经过压缩的!) --------------- .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.

Centos学习笔记 linux 常用命令:压缩解压命令

  2012-2-9 星期4 linux 常用命令:压缩解压命令 -gz命令的英文原意:Gnu zip 语法:gzip 选项[文件] 功能描述:压缩文件 压缩后文件格式: .gz 1. 只能压缩文件,不能压缩目录 2.不保留源文件 解压缩命令:gunzip 语法:gunzip选项[压缩文件] 功能描述:解压缩.gz的压缩文件 范例:gunzip file1.gz 压缩解压目录:tar 命令名称:tar 语法 tar选项[cvf][目录]      -c 产生.tar打包文件      -v 显示