利用ICSharpCode.SharpZipLib进行压缩
#ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language).#ZipLib
was ported from the GNU Classpath ZIP library for use with #Develop (http://www.icsharpcode.net/OpenSource/SD) which needed gzip/zip compression. Later bzip2
compression and tar archiving was added due to popular demand.
官方下载:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
ZipLib组件与.net自带的Copression比较,在压缩方面更胜一筹,经过BZip2压缩要小很多,亲手测试,不信你也可以试一试。而且这个功能更加强大。下面就是个人做的一个小例子,具体的应用程序源码:
/Files/yank/Compress.rar。
usingSystem;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;

/**////<summary>
/// Summary description for ICSharp
///</summary>
publicclass ICSharp


{
public ICSharp()

{
//
// TODO: Add constructor logic here
//
}
/**////
<summary>
/// 压缩
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public
string Compress(string param)

{
byte[] data
= System.Text.Encoding.UTF8.GetBytes(param);
//byte[] data = Convert.FromBase64String(param);
MemoryStream ms= new MemoryStream();
Stream stream= new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
try

{
stream.Write(data,, data.Length);
}
finally

{
stream.Close();
ms.Close();
}
return Convert.ToBase64String(ms.ToArray());
}
/**////
<summary>
/// 解压
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public
string Decompress(string param)

{
string commonString="";
byte[] buffer=Convert.FromBase64String(param);
MemoryStream ms= new MemoryStream(buffer);
Stream sm= new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
//这里要指明要读入的格式,要不就有乱码
StreamReader reader= new StreamReader(sm,System.Text.Encoding.UTF8);
try

{
commonString=reader.ReadToEnd();
}
finally

{
sm.Close();
ms.Close();
}
return commonString;
}
}Encoding.UTF8与Convert.FromBase64String
Unicode 标准为所有支持脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码方式。Unicode 标准 3.2 版使用下列 UTF:
UTF-8,它将每个码位表示为一个由 1 至 4 个字节组成的序列。
UTF-16,它将每个码位表示为一个由 1 至 2 个 16 位整数组成的序列。
UTF-32,它将每个码位表示为一个 32 位整数。
Convert.FromBase64String 方法
将指定的 String(它将二进制数据编码为 base 64 数字)转换成等效的 8 位无符号整数数组。
它的参数也又一定的要求:
参数是 由基 64 数字、空白字符和尾随填充字符组成。从零开始以升序排列的以 64 为基的数字为大写字符“A”到“Z”、小写字符“a”到“z”、数字“0”到“9”以及符号“+”和“/”。空白字符为 Tab、空格、回车和换行。s 中可以出现任意数目的空白字符,因为所有空白字符都将被忽略。无值字符“=”用于尾部的空白。s 的末尾可以包含零个、一个或两个填充字符。
异常:
| 异常类型 | 条件 |
|---|---|
| ArgumentNullException | s 为空引用(Visual Basic 中为 Nothing)。 |
| FormatException | s 的长度(忽略空白字符)小于 4。
- 或 - s 的长度(忽略空白字符)不是 4 的偶数倍。 |
s 的长度(忽略空白字符)不是 4 的偶数倍。
利用ICSharpCode.SharpZipLib进行压缩的更多相关文章
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密
C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩
这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http://icsharpcode.github.io/SharpZipLib/ 1.单个或多个文件加 ...
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...
- ICSharpCode.SharpZipLib实现压缩解压缩
最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...
- ICSharpCode.SharpZipLib 开源压缩库使用示例
官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述: ICSharpCode.SharpZipLib.dl ...
随机推荐
- SqlSugar ORM 入门到精通【一】入门篇
背景 SqlSugar是一款国产ORM,除了拥有媲美原生的性能 另外还有满足各种需求的功能,简单好用一分钟就能够轻松上手. 2.x版本正式在自已公司内部项目使用 3.x版本得到了更多公司的喜欢不过也会 ...
- create-react-app搭建的项目中添加bootstrap
react-bootstrap是一个非常受欢迎的针对react封装过的bootstrap,它本身不包含css,所以也是需要使用bootstrap原生库. 在create-react-app建的项目目录 ...
- php 数组变成树状型结构
<? php $stime = microtime(true); $nodes = [ ['id' = > 1, 'pid' = > 0, 'name' = > 'a'], [ ...
- React项目模板-从项目搭建到部署
前一段时间做了一个小项目,时间比较紧,就一个人月.最终希望能够通过微信公众号链接启动应用. 项目的业务细节就不多说了,主要是想分享一下做这个项目技术方面的一些经验. 技术选型 参考范围大致三种:Ang ...
- shell编程之SHELL基础(1)
shell脚本基础 shell是一个命令行解释器,她为互用提供了一个想linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至编写一些程序. shell还是一个功 ...
- shell实现go环境的部署搭建
##############################Deploy go enviroment######################## echo "start deploy g ...
- Nginx:413 Request Entity Too Large解决
最近在做给博客添加上传PDF的功能,但是在测试上传文件的过程中遇到了413 Request Entity Too Large错误.不过这个无错误是很好解决的,这个错误的出现是因为上传的文件大小超过了N ...
- 隱藏在素數規律中的Pi -- BZOJ1041解題報告
退役狗在刷程書的過程中看到了一個有趣的視頻, 講解了一個有趣的問題. 在網上隨便搜索了一下居然還真的找到了一道以它爲背景的OI題目, BZOJ1041. 下面的內容會首先回顧一下視頻所討論的知識, 有 ...
- shiro笔记-AuthenticatingRealm和AuthorizingRealm关系
AuthenticatingRealm-------->用于认证方法的Realm AuthorizingRealm--------->用于授权和认证的realm一般使用这个 Authori ...
- 几个常用的文本处理shell 命令:find、grep、sort、uniq、sed、awk
find 文件查找 查找txt和pdf文件 find . \( -name "*.txt" -o -name "*.pdf" \) -print 查找所有字母开 ...