C#利用SharpZipLib解压或压缩文件夹实例操作
最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享。
这里主要解决文件夹包含文件夹的解压缩问题。
)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”点击Download可以下载,解压后里边有好多文件夹,因为不同的版本,我用的FW2.0。
)引用SharpZipLib.dll,在项目中点击项目右键-->添加引用-->浏览,找到要添加的DLL-->确认
)改写了文件压缩和解压缩的两个类,新建两个类名字为ZipFloClass.cs,UnZipFloClass.cs 压缩文件夹 源码如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI; using System.IO; using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip; /// /// ZipFloClass 的摘要说明
/// public class ZipFloClass
{
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(); // 0 - store only to 9 - means best compression
zip(strFile, s, strFile);
s.Finish();
s.Close();
} private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{ if (Directory.Exists(file))
{
zip(file, s, staticFile);
} else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(tempfile); entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry); s.Write(buffer, , buffer.Length);
}
}
} } 解压文件夹 using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary; using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums; /// /// UnZipFloClass 的摘要说明
/// public class UnZipFloClass
{
public string unZipFile(string TargetFile, string fileDir)
{
string rootFile = " ";
try
{
//读取压缩文件(zip文件),准备解压缩
ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry theEntry;
string path = fileDir;
//解压出来的文件保存的路径 string rootDir = " ";
//根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= )
{
rootDir = rootDir.Substring(, rootDir.IndexOf("\\") + );
}
string dir = Path.GetDirectoryName(theEntry.Name);
//根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name);
//根目录下的文件名称
if (dir != " " )
//创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(fileDir + "\\" + dir))
{
path = fileDir + "\\" + dir;
//在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == " " && fileName != "")
//根目录下的文件
{
path = fileDir;
rootFile = fileName;
}
else if (dir != " " && fileName != "")
//根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf("\\") > )
//指定文件保存的路径
{
path = fileDir + "\\" + dir;
}
} if (dir == rootDir)
//判断是不是需要保存在根目录下的文件
{
path = fileDir + "\\" + rootDir;
} //以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\" + fileName); int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
} streamWriter.Close();
}
}
s.Close(); return rootFile;
}
catch (Exception ex)
{
return "1; " + ex.Message;
}
}
}
调用:
/// <summary>
/// 压缩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[];
FileProperties[] = "D:\\Debug\\";//待压缩文件目录
//Debug2=>目录必须存在(可以改进程序)
FileProperties[] = "D:\\Debug2\\a.zip"; //压缩后的目标文件
ZipFloClass Zc = new ZipFloClass();
Zc.ZipFile(FileProperties[], FileProperties[]);
} /// <summary>
/// 解压
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[];
FileProperties[] = "D:\\Debug2\\a.zip";//待解压的文件
//=>Debug3目录可以不用存在
FileProperties[] = "D:\\Debug3\\";//解压后放置的目标目录
UnZipFloClass UnZc = new UnZipFloClass();
UnZc.unZipFile(FileProperties[], FileProperties[]);
}
C#利用SharpZipLib解压或压缩文件夹实例操作的更多相关文章
- C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)
		
需要下载ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; u ...
 - C#解压或压缩文件夹
		
这里主要解决文件夹包含文件夹的解压缩问题.1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource /SharpZipLib/Downloa ...
 - [转]Ubuntu Linux 安装 .7z 解压和压缩文件
		
[转]Ubuntu Linux 安装 .7z 解压和压缩文件 http://blog.csdn.net/zqlovlg/article/details/8033456 安装方法: sudo apt-g ...
 - 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!
		
修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景: 有一个springboot项目,事先我已经用编译好打成jar包以 ...
 - 【VC++技术杂谈008】使用zlib解压zip压缩文件
		
最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...
 - C# 解压RAR压缩文件
		
此方法适用于C盘windows文件夹中有WinRAR.exe文件 /// 解压文件(不带密码) RAR压缩程序 返回解压出来的文件数量 /// </summary> /// <par ...
 - C# 解压zip压缩文件
		
此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...
 - C# 解压与压缩文件
		
解压文件 ,引用 SharpZipLib.dll类库 方法一: public void UnGzipFile(string zipfilename) { //同压缩文件同级同名的非压缩文件路径 var ...
 - C# 解压及压缩文件源代码
		
using System.IO; using System.Windows.Forms; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.Sh ...
 
随机推荐
- 嵌入式 hi3518平台获取网关
			
</pre><pre code_snippet_id="495447" snippet_file_name="blog_20141024_1_70650 ...
 - C# 阳历转农历
			
你妹的sb 原文 C#(ASP.NET)公历转农历的简单方法 Dot Net 平台,对全球化的支持做的非常好,不得不称赞一个 通常,将公历转为农历,是个非常烦的事情,需要整理闰年.闰月等的对照表. 在 ...
 - C++中类的public,private,protected比较
			
当private,public,protected单纯的作为一个类中的成员权限设置时:private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. protecte ...
 - DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现
			
在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中,笔者阐述了一个防御状态机,它可用来抵御来自应用层的DDoS攻击,但是该状态机依赖一个能应对大量条目快速增删的ip黑白 ...
 - jquery图片裁切+PHP文件上传
			
下载地址:download.csdn.net/source/2745353
 - 《Windows核心编程》第5版 学习进度备忘
			
学习资源:<Windows核心编程>第5版 知识基础支持: 本书与<Windows程序设计>第5版珍藏版结合很好,二者重叠内容不多,二者互补性强,而且相关方面的优秀书籍 跳过的 ...
 - 坑爹的gltools编译错误解决
			
搭边手游,看opengl superbible, 书中代码需要一个gltools库.作者自己提供的code google地址里gltools项目是空的,此一坑. 他的网站(www.starstones ...
 - 基于OSGI.Net的图形界面系统
			
在2013年的十月份有幸接触了osgi.net和iopenworks的创始人,了解和学习的插件式开发,开始了后台数据的处理生涯. 第一个有图形界面的系统——智能农业的环境监测系统,其实在这个系统中所有 ...
 - strtol()函数
			
#include <stdlib.h>#include <stdio.h> int main(){ char a[] = "100"; char b[] = ...
 - url(data:image/png;base64,R0**)(转)
			
url(data:image/png;base64,R0**) 所谓"data"类型的Url格式,是在RFC2397中 提出的,目的对于一些"小"的数据,可以在 ...