使用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 ...
随机推荐
- 将int型数值拆分成4字节
- CDOJ 1284 苦恼的郭大侠 map启发式合并
苦恼的郭大侠 题目连接: http://acm.uestc.edu.cn/#/problem/show/1284 Description 花开雷霆崖,血染伊吕波. 公元1772年. 郭大侠终于照着天行 ...
- python实现简单的梯度下降法
代码如下: # 梯度下降法模拟 import numpy as np import matplotlib.pyplot as plt plot_x = np.linspace(-1,6,141) # ...
- Active Snake (Level Set 模型)
前沿:最近由于大论文实验的原因,需要整理几种Snake方法,以比较道路提取效果.所以今天晚上就将电脑中的一些LBF Snake代码作一下分类定义.并给出效果.以便比较. 1. 原始的LBF Snake ...
- AS3.0纯代码编写的两款loading效果
AS3.0纯代码编写的两款loading效果 效果一 效果二 代码很简单,实现的原理一样,只不过矩形用到了Matrix类,对注册点进行了调整,效果可用于视频播放器缓冲时的loading效果,下面看代码 ...
- golang解析json格式 -- 全
项目中客户端和服务端的交互数据部分为json,因此在服务端就得解析,复杂的json解析起来其实还是挺费劲的. 交互的数据类似如下格式: {"sn":1,"ls" ...
- Spring事务管理笔记
事务的目的就是要保证数据的高度完整性和一致性. 在实际的项目中,大多都是使用注解的方式来实现事物,这里也就简单记录下使用@Transactional方法和注意事项. 在xml中添加配置 1234567 ...
- exVim安装
安装 安装必备 Vim 7.3 or higher. Vundle or Pathogen 下载/更新exVim 注意事项 安装exVim将不会覆盖你已经存在的Vim环境,这个仓库所包含的文件,变化, ...
- CKFinder的_thumbs缓存文件夹
<CKFinder2.0.2的使用与破解> 作者: 宓晨 说下背景,发下牢骚! 由于网站需要支持对图片的上传以及操作的管理,还有文字+图片的组合上传.问了一下同 ...
- 探讨android更新UI的几种方法(转)
作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...