使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx下载;
有两种方式可以实现压缩与解压缩,这里只提供了ZIP格式;
1、DLL本身提供的快速压缩与解压缩,直接代码:
using System;
using ICSharpCode.SharpZipLib.Zip; namespace Zip
{
public static class FastZipHelper
{
#region CreateZipFile public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter, bool createEmptyDirectories,
bool restoreAttributesOnExtract, bool restoreDateTimeOnExtract)
{
try
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = createEmptyDirectories;
fastZip.RestoreAttributesOnExtract = restoreAttributesOnExtract;
fastZip.RestoreDateTimeOnExtract = restoreDateTimeOnExtract;
fastZip.CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter);
return true;
}
catch (Exception ex)
{
return false;
}
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter, bool createEmptyDirectories)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter, createEmptyDirectories,
true, true);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter, true);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, string.Empty);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse)
{
return CreateZip(zipFileName, sourceDirectory, recurse, string.Empty);
} public static bool CreateZip(string zipFileName, string sourceDirectory)
{
return CreateZip(zipFileName, sourceDirectory, true);
} #endregion #region ExtractZip public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime, bool createEmptyDirectories,
bool restoreAttributesOnExtract, bool restoreDateTimeOnExtract)
{
try
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = createEmptyDirectories;
fastZip.RestoreAttributesOnExtract = restoreAttributesOnExtract;
fastZip.RestoreDateTimeOnExtract = restoreDateTimeOnExtract;
fastZip.ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime);
return true;
}
catch (Exception ex)
{
return false;
}
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime, bool createEmptyDirectories)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime, createEmptyDirectories, true, true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime, true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, string.Empty);
} public static bool ExtractZip(string zipFileName, string targetDirectory)
{
return ExtractZip(zipFileName, targetDirectory, string.Empty);
} public static bool ExtractZip(string zipFileName, string targetDirectory, string fileFilter)
{
return ExtractZip(zipFileName, targetDirectory, FastZip.Overwrite.Always, null, fileFilter);
} #endregion
}
}
2、自己实现:
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip; namespace Zip
{
public static class ZipHelper
{
public static string[] GetDirectories(string sourceDirectory)
{
List<string> result = new List<string>(); string[] directories = Directory.GetDirectories(sourceDirectory);
if (directories.Length > 0)
{
foreach (var directory in directories)
{
result.Add(directory);
result.AddRange(GetDirectories(directory));
}
}
return result.ToArray();
} public static string[] GetFiles(string sourceDirectory)
{
List<string> result = new List<string>();
result.AddRange(Directory.GetFiles(sourceDirectory));
string[] directories = Directory.GetDirectories(sourceDirectory);
if (directories.Length > 0)
{
foreach (var directory in directories)
{
result.AddRange(GetFiles(directory));
}
}
return result.ToArray();
} #region CreateZipFile public static void CreateZip(string sourceDirectory)
{
CreateZip(sourceDirectory, null);
} public static void CreateZip(string sourceDirectory, string zipFileName)
{
if (string.IsNullOrEmpty(sourceDirectory))
{
throw new ArgumentNullException("sourceDirectory", "sourceDirectory can not be null or empty.");
}
if (!Directory.Exists(sourceDirectory))
{
throw new DirectoryNotFoundException(sourceDirectory + " can not be found.");
}
if (string.IsNullOrEmpty(zipFileName))
{
zipFileName = sourceDirectory.TrimEnd('\\').TrimEnd('/') + ".zip";
}
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
//string[] filenames = Directory.GetFiles(sourceDirectory);
string[] directories = GetDirectories(sourceDirectory);
string[] filenames = GetFiles(sourceDirectory);
if (!Directory.Exists(Path.GetDirectoryName(zipFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipFileName));
}
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
{
s.SetLevel(9); // 0 - store only to 9 - means best compression ZipEntryFactory factory = new ZipEntryFactory();
foreach (var directory in directories)
{
string virtualDirectory = directory.Replace(sourceDirectory, string.Empty);
ZipEntry zipEntry = factory.MakeDirectoryEntry(virtualDirectory);
zipEntry.DateTime = DateTime.Now;
s.PutNextEntry(zipEntry);
} byte[] buffer = new byte[4096]; foreach (string file in filenames)
{
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
string newfileName = file.Replace(sourceDirectory, string.Empty);
ZipEntry entry = factory.MakeFileEntry(newfileName); // Setup the entry data as required. // Crc and size are handled by the library for seakable streams
// so no need to do them here. // Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
} // Finish/Close arent needed strictly as the using statement does this automatically // Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish(); // Close is important to wrap things up and unlock the file.
s.Close();
}
} #endregion #region ExtractZip public static void ExtractZip(string zipFileName)
{
ExtractZip(zipFileName, null);
} public static void ExtractZip(string zipFileName, string targetDirectory)
{
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("zipFileName", "zipFileName can not be null or empty.");
} if (!File.Exists(zipFileName))
{
throw new FileNotFoundException(zipFileName + " can not be found.");
} if (string.IsNullOrEmpty(targetDirectory))
{
targetDirectory = Path.Combine(Path.GetDirectoryName(zipFileName),
Path.GetFileNameWithoutExtension(zipFileName));
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
//create directory
string targetPath = Path.Combine(targetDirectory, theEntry.Name);
if (theEntry.IsDirectory)
{
Directory.CreateDirectory(targetPath);
}
if (theEntry.IsFile)
{
if (!Directory.Exists(Path.GetDirectoryName(targetPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
}
using (FileStream streamWriter = File.Create(targetPath))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
} #endregion
}
}
使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩的更多相关文章
- 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩
原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...
- C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类
最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
- 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature
写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- ICSharpCode.SharpZipLib.Zip
//压缩整个目录下载 var projectFolder = Request.Params["folder"] != null ? Request.Params["fol ...
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
随机推荐
- [转] 怎样在Ubuntu 14.04中搭建gitolite git服务器
相比gitosis,gitolite的功能更为强大,支持对权限的细分控制,学习一下在最新版 的ubuntu 14.04 LTS中搭建gitolite服务器是非常有必要的,嘿嘿,一会属于我们自己的Git ...
- [转] npm install 本地安装与全局安装的区别
npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如 npm install grunt # 本地安装 npm install -g ...
- iOS Multiview Applications
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- 如何通过WiFi来进行Android的真机模拟
我们知道,在使用模拟机模拟的时候会出现较多的问题,所以如果有一部Android手机的话进行真机模拟是极好的. 准备工作: 第一种方法:使用数据线,具体操作百度.略(非WIFI操作的真机模拟) 第二方法 ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
- 关于ASP.NET控件方面的学习(恢复版)
前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...
- exp/imp 有很多弊端
弊端1. 空表 无法执行导出操作弊端2. 高版本的导出文件 无法使用 低版本的 oracle软件 导入 环境准备:create table test0707(n1 date); 认证弊端1 案例1. ...
- jQuery 效果 - 滑动
jQuery 滑动方法可使元素上下滑动. 点击这里,隐藏/显示面板 实例 jQuery slideDown()演示 jQuery slideDown() 方法. jQuery slideUp()演示 ...
- Pomelo实现最简单的通信-egret。
昨天因为需要开始学习Pomelo 做H5游戏的服务端. 因为个人学习习惯,我从来不适合去跟着文档看.一般我直接是看下大概的API,但是Pomelo的API全部都是英文的. 昨天我就告诉自己用一下午时间 ...
- css media
/* media */ /* 横屏 */ @media screen and (orientation:landscape){ } /* 竖屏 */ @media screen and (orient ...