博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 文件流压缩解压
阅读量:6723 次
发布时间:2019-06-25

本文共 7622 字,大约阅读时间需要 25 分钟。

///     /// 文件流压缩解压    ///     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); } }

转载地址:http://lynmo.baihongyu.com/

你可能感兴趣的文章
java IO 包源码解析
查看>>
CSS3 线性渐变(linear-gradient)
查看>>
position 属性的值的比较
查看>>
学术休假-银行储蓄系统
查看>>
[CodeForces - 463B] Caisa and Pylons
查看>>
【JAE】JAE京东云擎部署首个Java应用
查看>>
Java时间和时间戳的相互转换
查看>>
Spring进行表单验证
查看>>
Java对象序列化与反序列化
查看>>
multiprocess(上)
查看>>
第43条:返回零长度的数组或者集合,而不是null
查看>>
homework-10
查看>>
1123: 零起点学算法30——参加程序设计竞赛
查看>>
jquery中this和$(this)使用的地方
查看>>
洛谷P4557 [JSOI2018]战争(闵可夫斯基和+凸包)
查看>>
洛谷P4151 [WC2011]最大XOR和路径(线性基)
查看>>
[bzoj 2456]mode
查看>>
jQuery页面元素操作之遍历元素
查看>>
CodeFile与CodeBehind的区别
查看>>
UOJ#428. 【集训队作业2018】普通的计数题
查看>>