当前位置: 首页 > 编程日记 > 正文

Compression Helper Class using SharpZipLib

使用 SharpZipLib 进行压缩的辅助类,简化压缩字节数组和字符串的操作。


None.gifusing System;
None.gif
using System.Text;
None.gif
using System.IO;
None.gif
using ICSharpCode.SharpZipLib.BZip2;
None.gif
using ICSharpCode.SharpZipLib.GZip;
None.gif
using ICSharpCode.SharpZipLib.Zip;
None.gif
None.gif
namespace Compression
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 压缩方式。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum CompressionType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// GZip 压缩格式
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        GZip,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// BZip2 压缩格式
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        BZip2,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Zip 压缩格式
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Zip
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 使用 SharpZipLib 进行压缩的辅助类,简化对字节数组和字符串进行压缩的操作。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CompressionHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 压缩供应者,默认为 GZip。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static CompressionType CompressionProvider = CompressionType.GZip;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public methods#region Public methods
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从原始字节数组生成已压缩的字节数组。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="bytesToCompress">原始字节数组。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回已压缩的字节数组</returns>

InBlock.gif        public static byte[] Compress(byte[] bytesToCompress)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MemoryStream ms 
= new MemoryStream();
InBlock.gif            Stream s 
= OutputStream(ms);
InBlock.gif            s.Write(bytesToCompress, 
0, bytesToCompress.Length);
InBlock.gif            s.Close();
InBlock.gif            
return ms.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从原始字符串生成已压缩的字符串。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="stringToCompress">原始字符串。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回已压缩的字符串。</returns>

InBlock.gif        public static string Compress(string stringToCompress)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] compressedData = CompressToByte(stringToCompress);
InBlock.gif            
string strOut = Convert.ToBase64String(compressedData);
InBlock.gif            
return strOut;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从原始字符串生成已压缩的字节数组。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="stringToCompress">原始字符串。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回已压缩的字节数组。</returns>

InBlock.gif        public static byte[] CompressToByte(string stringToCompress)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] bytData = Encoding.Unicode.GetBytes(stringToCompress);
InBlock.gif            
return Compress(bytData);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从已压缩的字符串生成原始字符串。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="stringToDecompress">已压缩的字符串。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回原始字符串。</returns>

InBlock.gif        public string DeCompress(string stringToDecompress)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string outString = string.Empty;
InBlock.gif            
if (stringToDecompress == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentNullException("stringToDecompress""You tried to use an empty string");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] inArr = Convert.FromBase64String(stringToDecompress.Trim());
InBlock.gif                outString 
= Encoding.Unicode.GetString(DeCompress(inArr));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (NullReferenceException nEx)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return nEx.Message;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return outString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从已压缩的字节数组生成原始字节数组。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="bytesToDecompress">已压缩的字节数组。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回原始字节数组。</returns>

InBlock.gif        public static byte[] DeCompress(byte[] bytesToDecompress)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] writeData = new byte[4096];
InBlock.gif            Stream s2 
= InputStream(new MemoryStream(bytesToDecompress));
InBlock.gif            MemoryStream outStream 
= new MemoryStream();
InBlock.gif
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int size = s2.Read(writeData, 0, writeData.Length);
InBlock.gif                
if (size > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    outStream.Write(writeData, 
0, size);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            s2.Close();
InBlock.gif            
byte[] outArr = outStream.ToArray();
InBlock.gif            outStream.Close();
InBlock.gif            
return outArr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Private methods#region Private methods
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从给定的流生成压缩输出流。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="inputStream">原始流。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回压缩输出流。</returns>

InBlock.gif        private static Stream OutputStream(Stream inputStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (CompressionProvider)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case CompressionType.BZip2:
InBlock.gif                    
return new BZip2OutputStream(inputStream);
InBlock.gif
InBlock.gif                
case CompressionType.GZip:
InBlock.gif                    
return new GZipOutputStream(inputStream);
InBlock.gif
InBlock.gif                
case CompressionType.Zip:
InBlock.gif                    
return new ZipOutputStream(inputStream);
InBlock.gif
InBlock.gif                
default:
InBlock.gif                    
return new GZipOutputStream(inputStream);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从给定的流生成压缩输入流。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="inputStream">原始流。</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回压缩输入流。</returns>

InBlock.gif        private static Stream InputStream(Stream inputStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (CompressionProvider)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case CompressionType.BZip2:
InBlock.gif                    
return new BZip2InputStream(inputStream);
InBlock.gif
InBlock.gif                
case CompressionType.GZip:
InBlock.gif                    
return new GZipInputStream(inputStream);
InBlock.gif
InBlock.gif                
case CompressionType.Zip:
InBlock.gif                    
return new ZipInputStream(inputStream);
InBlock.gif
InBlock.gif                
default:
InBlock.gif                    
return new GZipInputStream(inputStream);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



来源:http://www.mostlylucid.co.uk/archive/2004/04/06/958.aspx

相关文章:

matlab 线模式密度,环形腔窄线宽光纤激光器的研究

河北工业大学硕士2016环形腔窄线宽光纤激光器的研究Studt on Circular Cavity Fiber Laser with Narrow Line-width Output翟增武康志龙集成电路工程(专业学位)光纤激光器的主要特点就是指以光纤作为谐振腔的同时充当着光波导的角色。相比于气体激光器、固体激光器等其他的激光…

sql server 查看对象最后修改时间

sql server 查看对象最后修改时间&#xff0c;根据最后修改时间排序 存储过程 SELECT * FROM sys.all_objects WHERE TYPEP ORDER BY modify_date DESC; 视图 SELECT * FROM sys.all_objects WHERE TYPEv ORDER BY modify_date DESC; 表 SELECT * FROM sys.all_objects WHERE…

AI一分钟 | 比特大陆递交招股书,募资用于AI芯片研发;泰晤士2019全球大学排行榜:清华列亚洲第一...

▌比特大陆递交招股书&#xff0c;募资用于 AI 芯片研发9 月 26 日晚&#xff0c;比特大陆于香港联交所上载 A1 招股书&#xff0c;启动上市计划。中金为独家保荐人&#xff0c;搜狗创始人王小川担任其独立非执行董事。在招股书中&#xff0c;比特大陆称自身为中国第二大无晶圆…

php日志接入rsyslog,rsyslog和在PHP上的应用

rsyslog配置rsyslog的配置文件为/etc/rsyslog.conf&#xff0c;但真正的配置放在/etc/rsyslog.d/目录下# Default rules for rsyslog.## For more information see rsyslog.conf(5) and /etc/rsyslog.conf## First some standard log files. Log by facility.#auth,authpriv.* …

特斯拉“国王”王权不保

作者 | Just出品 | AI科技大本营&#xff08;公众号ID&#xff1a;rgznai100&#xff09;中国有句古话&#xff0c;“偷鸡不成蚀把米”。发推一时爽&#xff0c;本就麻烦缠身的马斯克不会想到&#xff0c;8 月 7 日那条考虑要将特斯拉私有化的推文&#xff0c;让他陷入了更大的…

流水账(4)---礼拜二---“抗议,我不是电工!”

礼拜二&#xff0c;今天。 6点半起床&#xff0c;9点钟到东郊。 上午帮东郊重装一台电脑。郁闷&#xff0c;我在那边一直就被当作修电脑的工人用了。还想起一个很有意思的误会。 一次&#xff0c;我和老师说&#xff0c;你知道罢&#xff0c;东郊还有一个fudan的法硕在那…

Python--1 初识

1.1 Python简介 Python[1] &#xff08;英国发音&#xff1a;/ˈpaɪθən/ 美国发音&#xff1a;/ˈpaɪθɑːn/&#xff09;, 是一种面向对象的解释型计算机程序设计语言&#xff0c;python的创始人为吉多范罗苏姆(Guido van Rossum)&#xff0c;Python是著名的“龟叔”Gui…

php 485串口通信,485串口通信中的常见问题

通信距离485总线的通讯距离理论可以达到1200米&#xff0c;一般是指通讯线材优质达标,波特率9600,只有一台485设备才能使得通讯距离达到1200米,而且能通讯并不代表每次通讯都正常.所以通常485总线实际的稳定的通讯距离远远达不到1200米. 负载485设备多,线材阻抗不合乎标准,线径…

人工智能加“医真云”,每年让5700万人告别误诊

“去拍个片子吧。”这是去医院看病常常能听到的话。现代医学诊断越来越倚重影像&#xff0c;专业医疗科学网站估计&#xff1a;医疗数据中有超过90%的数据来自于医学影像。然而&#xff0c;即便设备产生了高精度的大量影像&#xff0c;针对这些数据的分析&#xff0c;现在主要还…

关于Iframe之间以及与父窗体的值传递

自己实现过了的,javascript脚本关于Iframe之间以及与父窗体的值传递,事件调用问题; 在父页面访问Iframe子窗体的txtAddress控件window.frames["ifrMapCompanyDetails"].document.all("txtAddress").value 地址 ; 在Iframe子窗体1访问父页面的TextBox1控…

Php将网站推送到手机桌面的方法,把网页发送到桌面代码

把网页发送到桌面相信很多站长都会用到&#xff0c;可有的站长可能不会写&#xff0c;今天碎碎就给大家分享下把网页发送到桌面php和asp良种语言的代码。首先我们先做准备工作&#xff0c;要先上传自己网站的ioc文件&#xff0c;这样发送到桌面的时候才会有图标显示。然后新建一…

如何设计 Web App 应用架构?「两分钟了解 IOING」

IOING 在做些什么&#xff1f; IOING 在你的代码和浏览器之间架设了一个中间解释层&#xff0c;该解释层提供了一套新的语法来填补浏览器所不具备的能力。 SPA 开发痛点 开发一个 SPA 应用的痛点是不同模块页面的状态保存&#xff0c;当从一个页面跳转到另一个页面的时候窗口的…

ICLR 2019论文投稿近1600篇,强化学习最热门

作者 | 非主流出品 | AI科技大本营&#xff08;公众号ID&#xff1a;rgznai100&#xff09;ICLR 2019 的论文提交已经截止。根据官方消息&#xff0c;本届大会共收到近 1600 篇投稿&#xff0c;相比 ICLR 2018 的 935 篇&#xff0c;以及 ICLR 2017 的 490 篇&#xff0c;几乎每…

[Java]学习Java(4)类、接口、语句

1)类多了包的概念2)类继承时与C&#xff0b;&#xff0b;不同&#xff0c;它可以将父类protected的函数重写为public的。3)接口、纯虚函数概念都差不多&#xff0c;语法为&#xff1a;public class A extends B implements IC,ID,IE { ...}4)语句&#xff1a;与C&#xff0b;&a…

php easyui tree 结构,EasyUI Tree树组件无限循环的解决方法

在学习jquery easyui的tree组件的时候&#xff0c;在url为链接地址的时&#xff0c;发现如果最后一个节点的state为closed时&#xff0c;未节点显示为文件夹&#xff0c;单击会重新加载动态(Url:链接地址)形成无限循环。如&#xff1a;tree.json[{"id":1,"text&…

Scikit-learn 发布 0.20版本!新增处理缺失值、合并Pandas等亮点功能

整理 | Jane出品 | AI科技大本营之前一直预告 Scikit-learn 的新版本会在 9 月发布&#xff0c;在马上就要结束的 9 月&#xff0c;我们终于迎来了 Scikit-learn 0.20。此版本修复了大量的错误和功能&#xff0c;增强了 Scikit-learn 库&#xff0c;改善了文档和示例。在此对 …

深挖数据价值 阿里云栖开年大会报道

本文讲的是深挖数据价值 阿里云栖开年大会报道【IT168 云计算】经历风雨&#xff0c;转身看到彩虹。在这个“化云为雨”的时节&#xff0c;造云大咖们角色扮演也逐步渐入佳境&#xff0c;或随需而动&#xff0c;或引领潮流。阿里云作为国内公有云绝对的大咖之一&#xff0c;正以…

使用Facade模式分析

在遇到以下情况时可以考虑使用Facade模式&#xff1a;1、当你要为一个复杂子系统提供一个简单接口时。子系统往往因为不断演化而变得越来越复杂。大多数模式使用时都会产生更多更小类。这使得子系统更具可重用性&#xff0c;也更容易对子系统进行定制&#xff0c;但这也给那些不…

我与前端之间不得不说的三天两夜之javaScript

前端基础之JavaScriptJavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape(网景)接收Nombas的理念,(Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套…

难以置信!LSTM和GRU的解析从未如此清晰(动图+视频)

作者 | Michael Nguyen编译 | 蔡志兴、费棋编辑 | Jane出品 | AI科技大本营【导语】机器学习工程师 Michael Nguyen 在其博文中发布了关于 LSTM 和 GRU 的详细图解指南。博文中&#xff0c;他先介绍了 LSTM 和 GRU 的本质&#xff0c; 然后解释了让 LSTM 和 GRU 有良好表现的内…

php 开发restful api,用PHP创建RESTful API?

如果您的服务支持所有CRUD操作,则始终建议实现RESTful接口.这样做并不是很难.我已经概述了下面的一些基础知识.RESTful服务只是做了一些事情&#xff1a;>它使用HTTP请求方法进行CRUD操作的通信>它使用HTTP状态代码来传达响应状态,以及>它使用URI来定义您的资源(您正在…

MySQL服务器的配置教程

1、安装MySQL 这个应该很简单了&#xff0c;而且我觉得大家在安装方面也没什么太大问题&#xff0c;所以也就不多说了&#xff0c;下面我们来讲讲配置。 2、配置MySQL 注意&#xff0c;在Ubuntu下MySQL缺省是只允许本地访问的&#xff0c;如果你要其他机器也能够访问的话&am…

php开发我的世界插件,WorldEdit/开发与API

本页面已存在其他语言的内容&#xff0c;请协助翻译为本地化的中文。点击此处开始翻译。如本模板出现在原文存档页面&#xff0c;请注意更新主页面后&#xff0c;仍需要去除此处该模板如当前页面已经没有需要翻译的内容&#xff0c;请删去待翻译模板有标题的大篇幅文章&#xf…

关于AI,腾讯又有大动作!开发者该如何应对?

时隔 6 年后&#xff0c;腾讯公司的组织架构迎来新一轮的优化调整&#xff0c;在原有七大事业群&#xff08;BG&#xff09;的基础上进行重组整合。 腾讯公司董事会主席兼首席执行官马化腾表示&#xff1a;“作为一家以互联网为基础的科技和文化公司&#xff0c;技术是腾讯公司…

Android存储方式之SQLite

前言 SQLite数据库操作在Android开发中非常常用今天我将带大家全面了解关于SQLite数据库的操作&#xff08;增、删、查、改&#xff09;目录 1. SQLite数据库介绍 SQLite是Android内置的一个小型、关系型、属于文本型的数据库。 > Android提供了对 SQLite数据库的完全支持&a…

最近要换个主机,现在的太慢了

最近在学习wtl&#xff0c;打算做一个文件搜索工具来练手&#xff0c;需要正则表达式处理、数据库存储、键盘HOOK以及UI等技术UI用WTL搞定正则表达式的处理&#xff1a;http://research.microsoft.com/projects/greta/regex_perf.htmlhttp://www.tropicsoft.com/Components/Reg…

oracle数据库配置失败,oracle11g – Oracle 11G XE安装错误:数据库配置失败

在linuxMint上安装后运行oracle配置时&#xff1a;/etc/init.d/oracle-xe configure指定以下错误&#xff1a;sudo /etc/init.d/oracle-xe configureOracle Database 11g Express Edition Configuration-------------------------------------------------This will configure …

显示来自多个表的数据——JOIN

表关系简介 一、语法 SELECT 字段列表FROM TABLE1 [CROSS JOIN TABLE2 ] | [NATURAL JOIN TABLE2 ] | [JOIN TABLE2 USING (字段名) ] | [JOIN TABLE2 ON (TABLE.COLUMN_NAME TABLE2.COLUMN_NAME) ] | [(LEFT | RIGHT | FULL OUT) JOIN TABLE2 ON (TABLE1.COLUMN_NAME TABL…

20年第三次架构大调整,腾讯永远年轻!

整理 | 琥珀出品 | AI科技大本营&#xff08;公众号ID&#xff1a;rgznai100&#xff09;凌晨&#xff0c;腾讯官方公众号发布了一则消息&#xff0c;公布了成立 20 周年以来的新一轮整体战略升级。此前&#xff0c;国内各大互联网公司包括阿里、百度、美团、滴滴、京东都相继进…