ICSharpCode.SharpZipLib 开源压缩库使用示例
官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
插件描述: ICSharpCode.SharpZipLib.dll 是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩, SharpZipLib 的许可是经过修改的GPL,底线是允许用在不开源商业软件中,意思就是免费使用。
一、在ThinksKing的Plugins里面找到已经解压好的SharpZipLib,使用net-20文件夹中的ICSharpCode.SharpZipLib.dll 。添加至项目引用中。
二、操作指南:
1.1 创建zip文件,并添加文件:
using (ZipFile zip = ZipFile.Create(@”E:\test.zip”))
{
zip.BeginUpdate();
zip.Add(@”E:\文件1.txt”);
zip.Add(@”E:\文件2.txt”);
zip.CommitUpdate();
}
1.2 将文件夹压缩为文件
(new FastZip()).CreateZip(@”E:\test.zip”, @”E:\test\”, true, “”);
最后一个参数是使用正则表达式表示的过滤文件规则。CreateZip方法有3个重载版本,其中有目录过滤参数、文件过滤参数及用于指定是否进行子目录递归的一个bool类型的参数。
1.3 将文件添加到已有zip文件中
using (ZipFile zip = new ZipFile(@”E:\test.zip”))
{
zip.BeginUpdate();
zip.Add(@”E:\test.doc”);
zip.CommitUpdate();
}
1.4 列出zip文件中文件
using (ZipFile zip = new ZipFile(@”E:\test.zip”))
{
string list = string.Empty;
foreach (ZipEntry entry in zip)
{
list += entry.Name + “\r\n”;
}
MessageBox.Show(list);
}
1.5 删除zip文件中的一个文件
using (ZipFile zip = new ZipFile(@”E:\test.zip”))
{
zip.BeginUpdate();
zip.Delete(@”test.doc”);
zip.Delete(@”test22.txt”);
zip.CommitUpdate();
}
1.6 解压zip文件中文件到指定目录下
(new FastZip()).ExtractZip(@”E:\test.zip”, @”E:\test\”, “”);
1.7 常用类
ZipInputStream、GZipInputStream用于解压缩Deflate、GZip格式流,ZipOutputStream、GZipOutputStream用于压缩Deflate、GZip格式流。
StreamUtil类包含了几个Stream处理辅助方法:
1) Copy(Stream, Stream, Byte[])用于从一个Stream对象中复制数据到另一Stream对象。有多个重写。
2) ReadFully(Stream, Byte [])用于从Stream对象中读取所有的byte数据。有多个重写。
三、帮助文档
在SharpZipLib中有SharpZipLib_0860.chm官方帮助文档。
参考链接:在路上的博文:《ICSharpCode.SharpZipLib 插件使用示例》
在处理后台附件上载由于文件较多,需要每个文件单独上传关键是有些文件数据量比较少 也需要单独上传,这样导致后台数据流量较大而且用户操作麻烦.
在处理这方面业务时,可以简化:首先验证用户上传文件的大小,设定不超过1M文件为限制并记录,当用户点击一次操作时后台程序把所有小数据量文件进行压缩成一个单独文件来上传,这样简化用户操作难度 增强用户体验,在获得上载文件时同样把这个文件进行解压本地即可...
使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类: 完整代码如下
A:ICSharpCode.SharpZipLib.DLL组件下载地址,如果要实现必须在项目中引用该组件DLL
下载地址:http://good.gd/203866.htm
B:完整的操作类代码实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using the Compent Comspac
using System.IO;
using System.Text;
using System.Threading;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
namespace TestJqueryAjax
{
/**//// <summary>
/// 使用ICSharpZipCode.Dll实现解压缩
/// Author:chenkai Time:2009年7月13日22:03:27
/// Version:Beta1.0.0-(测试版)
/// </summary>
public class ICSharpZipCodeTest
{
/**//// <summary>
/// 实现压缩功能
/// </summary>
/// <param name="filenameToZip">要压缩文件(绝对文件路径)</param>
/// <param name="Zipedfiledname">压缩(绝对文件路径)</param>
/// <param name="CompressionLevel">压缩比</param>
/// <param name="password">加密密码</param>
/// <param name="comment">压缩文件描述</param>
/// <returns>异常信息</returns>
public static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel,
string password, string comment)
{
try
{
//使用正则表达式-判断压缩文件路径
System.Text.RegularExpressions.Regex newRegex = new System.Text.
RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))");
if (!newRegex.Match(Zipedfiledname).Success)
{
File.Delete(Zipedfiledname);
return "压缩文件的路径有误!";
}
//创建ZipFileOutPutStream
ZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname,
FileMode.OpenOrCreate)); //判断Password
if (password != null && password.Length > )
{
newzipstream.Password = password;
}
if (comment != null && comment.Length > )
{
newzipstream.SetComment(comment);
}
//设置CompressionLevel
newzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression //执行压缩
foreach (string filename in filenameToZip)
{
FileStream newstream = File.OpenRead(filename);//打开预压缩文件
//判断路径
if (!newRegex.Match(Zipedfiledname).Success)
{
File.Delete(Zipedfiledname);
return "压缩文件目标路径不存在!";
}
byte[] setbuffer=new byte[newstream.Length];
newstream.Read(setbuffer,,setbuffer.Length);//读入文件
//新建ZipEntrity
ZipEntry newEntry = new ZipEntry(filename);
//设置时间-长度
newEntry.DateTime = DateTime.Now;
newEntry.Size = newstream.Length;
newstream.Close();
newzipstream.PutNextEntry(newEntry);//压入
newzipstream.Write(setbuffer,,setbuffer.Length); }
//重复压入操作
newzipstream.Finish();
newzipstream.Close(); }catch (Exception e)
{
//出现异常
File.Delete(Zipedfiledname);
return e.Message.ToString();
} return "";
}
/**//// <summary>
/// 实现解压操作
/// </summary>
/// <param name="zipfilename">要解压文件Zip(物理路径)</param>
/// <param name="UnZipDir">解压目的路径(物理路径)</param>
/// <param name="password">解压密码</param>
/// <returns>异常信息</returns>
public static string UnMakeZipFile(string zipfilename,string UnZipDir,string password)
{
//判断待解压文件路径
if (!File.Exists(zipfilename))
{
File.Delete(UnZipDir);
return "待解压文件路径不存在!";
} //创建ZipInputStream
ZipInputStream newinStream = new ZipInputStream(File.OpenRead(zipfilename)); //判断Password
if (password != null && password.Length > )
{
newinStream.Password = password;
}
//执行解压操作
try
{
ZipEntry theEntry;
//获取Zip中单个File
while ((theEntry = newinStream.GetNextEntry()) != null)
{
//判断目的路径
if (Directory.Exists(UnZipDir))
{
Directory.CreateDirectory(UnZipDir);//创建目的目录
}
//获得目的目录信息
string Driectoryname = Path.GetDirectoryName(UnZipDir);
string pathname = Path.GetDirectoryName(theEntry.Name);//获得子级目录
string filename = Path.GetFileName(theEntry.Name);//获得子集文件名
//处理文件盘符问题
pathname = pathname.Replace(":", "$");//处理当前压缩出现盘符问题
Driectoryname = Driectoryname + "\\" + pathname;
//创建
Directory.CreateDirectory(Driectoryname);
//解压指定子目录
if (filename != string.Empty)
{
FileStream newstream = File.Create(Driectoryname + "\\" + pathname);
int size = ;
byte[] newbyte = new byte[size];
while (true)
{
size = newinStream.Read(newbyte, , newbyte.Length);
if (size > )
{
//写入数据
newstream.Write(newbyte, , size);
}
else
{
break;
}
newstream.Close();
}
}
}
newinStream.Close();
}
catch (Exception se)
{
return se.Message.ToString();
}
finally
{
newinStream.Close();
} return "";
}
}
}
参考链接:陈凯的博文:《使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类》
ICSharpCode.SharpZipLib 开源压缩库使用示例的更多相关文章
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
- ICSharpCode.SharpZipLib实现压缩解压缩
最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...
- C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...
- 利用ICSharpCode.SharpZipLib进行压缩
#ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is im ...
- ICSharpCode.SharpZipLib工具压缩与解压缩zip文件
using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...
- ICSharpCode.SharpZipLib.dll 压缩多文件
网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...
- ICSharpCode.SharpZipLib.Zip 压缩文件
public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
随机推荐
- Chinese remainder theorem again(中国剩余定理)
C - Chinese remainder theorem again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:% ...
- Java Servlet 过滤器与 springmvc 拦截器的区别?
前言:在工作中,遇到需要记录日志的情况,不知道该选择过滤器还是拦截器,故总结了一下. servlet 过滤器 定义 java过滤器能够对目标资源的请求和响应进行截取.过滤器的工作方式分为四种 应用场景 ...
- Hadoop集群动态服役新的数据节点&&退役数据节点
备注:新添的机器为hadoop05,现有hadoop01.hadoop02.hadoop03.hadoop04 环境准备: 1.先克隆一台和集群中一样的机器 2.修改机器ip和主机名称 3.删除原来的 ...
- Warning:java:资源1.5已过时,将在未来所有发行版中删除
idea运行提示错误信息:解决办法如下:第一步![ ...
- SD从零开始05-06
SD从零开始5 从库存销售 销售凭证类型Sales document type: 用来鉴别和控制不同的业务流程类型: 标准的销售凭证类型: standard order: Rush order: Ca ...
- linux svn配置与使用
svn错误码对照表: http://docs.sharpsvn.net/current/html/T_SharpSvn_SvnErrorCode.htm https://www.cnblogs ...
- Expo大作战(十七)--expo结合哨兵(sentry)进行错误异常记录
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- LeetNode 题解之Reverse Nodes in k-Group
1.题目描述 2.问题分析 这个题本质上还是按照链表翻转的思路来解,只是需要添加一些细节判断. 3.代码 class Solution { public: ListNode* reverseKGrou ...
- VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享
版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...
- SQLSERVER中的鬼影索引
SQLSERVER中的鬼影索引 看这篇文章之前可以先看一下鬼影记录 了解了解一下SQLSERVER里的鬼影记录关于鬼影记录的翻译一关于鬼影记录的翻译二 当删除表中的某一条记录的时候,索引页面的对应记录 ...