博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#用Zlib压缩或解压缩字节数组
阅读量:4307 次
发布时间:2019-06-06

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

///         /// 复制流        ///         /// 原始流        /// 目标流        public static void CopyStream(System.IO.Stream input, System.IO.Stream output)        {            byte[] buffer = new byte[2000];            int len;            while ((len = input.Read(buffer, 0, 2000)) > 0)            {                output.Write(buffer, 0, len);            }            output.Flush();        }        ///         /// 压缩字节数组        ///         /// 需要被压缩的字节数组        /// 
压缩后的字节数组
private static byte[] compressBytes(byte[] sourceByte) { MemoryStream inputStream = new MemoryStream(sourceByte); Stream outStream = compressStream(inputStream); byte[] outPutByteArray = new byte[outStream.Length]; outStream.Position = 0; outStream.Read(outPutByteArray, 0, outPutByteArray.Length); outStream.Close(); inputStream.Close(); return outPutByteArray; } /// /// 解压缩字节数组 /// /// 需要被解压缩的字节数组 ///
解压后的字节数组
private static byte[] deCompressBytes(byte[] sourceByte) { MemoryStream inputStream = new MemoryStream(sourceByte); Stream outputStream = deCompressStream(inputStream); byte[] outputBytes = new byte[outputStream.Length]; outputStream.Position = 0; outputStream.Read(outputBytes, 0, outputBytes.Length); outputStream.Close(); inputStream.Close(); return outputBytes; } /// /// 压缩流 /// /// 需要被压缩的流 ///
压缩后的流
private static Stream compressStream(Stream sourceStream) { MemoryStream streamOut = new MemoryStream(); ZOutputStream streamZOut = new ZOutputStream(streamOut, zlibConst.Z_DEFAULT_COMPRESSION); CopyStream(sourceStream, streamZOut); streamZOut.finish(); return streamOut; } /// /// 解压缩流 /// /// 需要被解压缩的流 ///
解压后的流
private static Stream deCompressStream(Stream sourceStream) { MemoryStream outStream = new MemoryStream(); ZOutputStream outZStream = new ZOutputStream(outStream); CopyStream(sourceStream, outZStream); outZStream.finish(); return outStream; }

zlib.NET库下载:

转载于:https://www.cnblogs.com/kingBook/p/5970271.html

你可能感兴趣的文章
Python 环境搭建
查看>>
免费字典api ,查询汉字完整信息
查看>>
Flume协作框架
查看>>
基于数据库的事务消息解决分布式事务方案
查看>>
HDU 2461 Rectangles#容斥原理
查看>>
网口扫盲二:Mac与Phy组成原理的简单分析(转)
查看>>
使用最大似然法来求解线性模型(1)
查看>>
EF 从sqlserver2008 迁移到 2005出现的BUG
查看>>
架构-浅谈MySQL数据库优化
查看>>
jquery锚点连接划动滚动条,再也不用a标签name 了
查看>>
Apache JMeter--网站自动测试与性能测评
查看>>
信号与系统(中)
查看>>
【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用
查看>>
bash 快捷键
查看>>
检测登录按钮 ,回车即登录
查看>>
蓝桥杯-微生物增殖
查看>>
ansible for devops读书笔记第一章
查看>>
解决IE6下,给图片加上line-height属性不起作用的方法
查看>>
#10172. 「一本通 5.4 练习 1」涂抹果酱 题解
查看>>
vue-cli 3.0安装和使用
查看>>