C# zip/unzip with ICSharpCode.SharpZipLib
download ICSharpCode and add reference
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Core; namespace SKPaySlip
{ public class ZipHelper
{ public static void CreateZipFile(string filesPath, string zipFilePath)
{ if (!Directory.Exists(filesPath))
{
Console.WriteLine("Cannot find directory '{0}'", filesPath);
return;
} try
{
string[] filenames = Directory.GetFiles(filesPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
byte[] buffer = new byte[]; //缓冲区大小
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
} public static void UnZipFile(string zipFilePath, string outDir)
{
if (string.IsNullOrEmpty(outDir))
{
outDir = "";
}
else
{
if (!outDir.EndsWith(@"\"))
{
outDir = outDir + @"\";
}
}
if (!File.Exists(zipFilePath))
{ Console.WriteLine("Cannot find file '{0}'", zipFilePath);
return;
} using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
Console.WriteLine(theEntry.Name);
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName.Length > )
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{ using (FileStream streamWriter = File.Create(outDir + theEntry.Name))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
}
} } }
C# zip/unzip with ICSharpCode.SharpZipLib的更多相关文章
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
- ICSharpCode.SharpZipLib压缩解压
一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...
- 利用ICSharpCode.SharpZipLib进行压缩
#ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is im ...
- C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类
最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...
- C# zip -ICSharpCode.SharpZipLib
利用第三方组件 ICSharpCode.SharpZipLib download from: https://github.com/icsharpcode/SharpZipLib using S ...
- zip (ICSharpCode.SharpZipLib.dll文件需要下载)
ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...
- 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature
写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...
- ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用
MyZip.dll : 有BUG,会把子目录的文件解压到根目录.. ICSharpCode.SharpZipLib.dll: 把ICSharpCode.SharpZipLib.dll复制一份,重命名为 ...
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
随机推荐
- OC中字典:NSDictionary类是如何使用的
字典就是关键字及其定义(描述)的集合.Cocoa中的实现字典的集合NSDictionary在给定的关键字(通常是一个NSString)下存储一个数值(可以是任何类型的对象).然后你就可以用这个关键字来 ...
- 各I/O模型 对应Web服务应用模型(select,poll,epoll,kevent,"/dev/poll")
一.利用select多路复用I/O的Web服务应用模型 /* 可读.可写.异常三种文件描述符集的申明和初始化.*/ fd_set readfds, writefds, exceptionfds; F ...
- 深入理解计算机系统第二版习题解答CSAPP 2.19
在2.17的基础上完成下表: x 十六进制 T2U(x) -8 0x8 -3 0xD -2 0xE -1 0xF 0 0x0 5 0x5
- js 添加enter事件
$(function () { document.onkeydown = function (e) { var ev = document.all ? window.event : e; ) { if ...
- ASP.Net_入门准备
基础篇:(学习能力取决于你的基础扎不扎实) 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NET是一个全 ...
- 导出Excel事例
DataTable table = new DataTable(); StringWriter sw = new StringWriter(); string tabltitle = "客户 ...
- System.Web.Mvc.Html 命名空间小计(转)
最近在看MVC框架,发现这个博文对初学者可能有帮助,故转之. 1,Html.Action 使用指定参数调用指定子操作方法并以 HTML 字符串形式返回结果. Html.Action() < ...
- cocos2d-x实战 C++卷 学习笔记--第4章 win32平台下中文乱码问题
前言: 将GBK编码的字符串转为UTF-8编码.(通俗点说就是解决中文乱码问题) 简要介绍: 在Win32平台下通过 log 输出中文字符时,会出现中文乱码问题.同样的代码在 ios 和 Androi ...
- IOS-UI-UIDynamic(二)
UIPushBehavior :推动效果 UIAttachmentBehavior:附着效果 UISnapBehavior:迅速移动效果 一.重要的属性 UIPushBehavior :推动效果 ty ...
- [jquery]高级篇--获取div子元素
参考: http://zhidao.baidu.com/link?url=IfeQQBn1xMLqWvwdkKbQYJ8mC6ciGi_8M1NYkm6iQ-kXBMX2f2ylN-ckzFLiynn ...