使用DotNetZip压缩与解压缩
下载地址:http://dotnetzip.codeplex.com/
解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文件拷贝到C盘下
接着打开Visual Studio Command Prompt (2010),然后定位到C盘根目录,依次输入gacutil -i Ionic.Zip.dll 和regasm Ionic.Zip.dll ,然后拷贝C盘下的这个Ionic.Zip.dll到您的应用程序中,添加引用即可!代码例子如下:
[csharp] view plaincopy .using System;
.using System.Collections.Generic;
.using System.ComponentModel;
.using System.Data;
.using System.Drawing;
.using System.Linq;
.using System.Text;
.using System.Windows.Forms;
.using Ionic.Zip;
.namespace WindowsFormsZIP
.{
. public partial class Form1 : Form
. {
. public Form1()
. {
. InitializeComponent();
. }
.
. /// <summary>
. /// 压缩带中文的文件名.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button1_Click(object sender, EventArgs e)
. {
.
. using (ZipFile zip = new ZipFile("苏志.zip",Encoding.Default))
. {
. zip.AddFile("数据库文档.doc");
. zip.Save();
. }
.
. }
.
. /// <summary>
. /// 用密码加密ZIP文件.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button2_Click(object sender, EventArgs e)
. {
.
. using (ZipFile zip = new ZipFile())
. {
. zip.Password = "123456!";
. zip.AddFile("WPF 4 Unleashed.pdf");
. zip.Save("WPF 4 Unleashed.zip");
. }
.
. }
.
. /// <summary>
. /// 压缩文件夹
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button3_Click(object sender, EventArgs e)
. {
. using (ZipFile zip = new ZipFile())
. {
. zip.AddDirectory(@"E:\suzhi");
. zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
. zip.Save("test.zip");
. }
. }
.
. /// <summary>
. /// 抽取ZIP中的文件到指定的文件夹.
. /// </summary>
. /// <param name="sender">The source of the event.</param>
. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
. private void button4_Click(object sender, EventArgs e)
. {
. using (ZipFile zip = ZipFile.Read("test.zip"))
. {
. foreach (ZipEntry z in zip)
. {
. z.Extract(@"F:\kk");
. }
. }
.
. }
.
. }
.}
具体的例子:
//获取压缩包路径
string filename = material.CreationTime.ToyyyyMMdd().Replace("-", ""); //string zippath = AppDomain.CurrentDomain.BaseDirectory + "upload\\Zipped\\" + filename;
//和上面的结果一样
string zippath = material.FilePath;
string decomPath = "";
string nowpath = "";
if (material.FilePath.Substring(material.FilePath.Length - , ).Equals("zip"))
{ decomPath = material.FilePath.Replace(".zip", ""); }
if (material.FilePath.Substring(material.FilePath.Length - , ).Equals("rar"))
{ decomPath = material.FilePath.Replace(".rar", ""); }
//查询此目录下是否存在需要解压过的同名的文件夹名称
DirectoryInfo direInfo = new DirectoryInfo(decomPath);
if (direInfo.Exists)
{
DirectoryInfo[] childs = direInfo.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
}
direInfo.Delete(true);
} //解压文件夹
using (ZipFile zip = ZipFile.Read(zippath))
{ foreach (ZipEntry z in zip)
{
//z.Extract(material.FilePath.Replace(".zip", ""));
z.Extract(zippath.Replace(".zip", ""));
}
} direInfo = new DirectoryInfo(zippath.Replace(".zip", ""));
if (direInfo.Exists)
{
DirectoryInfo[] dirchild = direInfo.GetDirectories();
if (dirchild.Count() == )
{
nowpath = decomPath + "\\" + dirchild[].Name;
direInfo = new DirectoryInfo(nowpath);
DirectoryInfo[] dirchild2 = direInfo.GetDirectories();
foreach (DirectoryInfo childrens in dirchild2)
{ //修改文件夹的内容
FileStream fs = new FileStream(nowpath +"\\"+ childrens.Name + "\\index.html", FileMode.OpenOrCreate, FileAccess.ReadWrite);
string fileText = "";
GameServer server = GameServerService.GetGameServerWithGame(materialLog.ServerId);
Game game = server.Game;
using (StreamReader sr = new StreamReader(fs))
{
fileText = sr.ReadToEnd();
fileText = fileText.Replace("#GameCode#", game.Code);
fileText = fileText.Replace("#ServerCode#", server.Code);
fileText = fileText.Replace("#ServerId#", Convert.ToString(server.ServerId));
}
if (fileText != "")
{
using (StreamWriter sw = new StreamWriter(nowpath + "\\" + childrens.Name + "\\index.html", false, Encoding.Default))
{
sw.Write(fileText);
}
} }
} } //重新压缩 using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(decomPath);
zip.Save(nowpath+".zip");
}
使用DotNetZip压缩与解压缩的更多相关文章
- Java 基础【12】 压缩与解压缩
Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...
- 菜鸟学Linux命令:tar命令 压缩与解压缩
tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件. tar最初被用来在磁带上创建档案,现在,用户可以 ...
- Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控
一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...
- gzip [选项] 压缩(解压缩)
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
- PclZip:强大的PHP压缩与解压缩zip类
PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...
- zip格式压缩、解压缩(C#)
压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...
- Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- Deflater与Inflater的压缩与解压缩
原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...
随机推荐
- (疯狂java)第二课
(本文章只是为了好玩,没有别的意思,有理解错误之处,恳请提醒,谢谢) 三.数据类型和运算符 想了一下今天看的内容好像依然大脑停留在用C语言去理解java,感觉有点奇怪,为啥本章叫数据类型和运算符,上来 ...
- 1.3(SQL学习笔记)计算字段及函数
一.计算字段 1.1拼接字段 一般情况下返回的字段是指定列的属性名.如果有时我们对返回格式有特殊要求. 例如,我们需要将显示商品名,即商品价格,同时商品名后面的价格放在括号内. prod_name(p ...
- 【OpenJudge9275】【Usaco2009 Feb】【递推】Bullcow
Bullcow 总时间限制: 12000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB [描述] 一年一度的展会要来临了,农民约翰想要把N(1 <= N <= 10 ...
- php分布式缓存系统 Memcached 入门
Memcached 是一个分布式的缓存系统, 但是 Memcachd 到底是什么意思,有什么作用呢?缓存一般用来保存一些经常被存取的数据和资源(例如:浏览器会将访问过的网页会话缓存起来),因为通过缓存 ...
- linux安装配置redis
redis是支持linux的,所以linux安装redis非常简单,按照官网的提示操作即可. 下载 $ wget http://download.redis.io/releases/redis-3.2 ...
- UITabBarController 和 UINavigationController 的详解
首先得搞清这两个控制器之间的层级关系,我们直接看官网给的图,如下所示: 从这张图可以看到:最右边的Assembled views是呈现给用户的界面,它左边的Window是最底层的窗口,重点来了,再往左 ...
- Android开发高级进阶——多进程间通信
一. 什么是多进程? 多进程就是多个进程的意思,那么什么是进程呢? 当一个应用在开始运行时,系统会为它创建一个进程,一个应用默认只有一个进程,这个进程(主进程)的名称就是应用的包名. 进程的特点: 进 ...
- Codeforces Round #313 (Div. 1) Gerald's Hexagon
http://codeforces.com/contest/559/problem/A 题目大意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 解 ...
- python 调试模式pdb(转)
标准库的pdb pdb是Python自带的一个库,为Python程序提供了一种交互式的源代码调试功能,包含了现代调试器应有的功能,包括设置断点.单步调试.查看源码.查看程序堆栈等.如果读者具有C或C+ ...
- python查看字节码
查看字节码可以帮助我们更好的理解python的执行流程 查看字节码列表 import opcode for op in range(len(opcode.opname)): print('0x%.2X ...