博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DotNetZip封装类
阅读量:4671 次
发布时间:2019-06-09

本文共 3168 字,大约阅读时间需要 10 分钟。

 

DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。

下载地址在这里:

下载到的包里有很多个dll文件,一般引用Ionic.Zip.dll就可以:

然后引用这个命名空间:

using Ionic.Zip;

以下是我自己封装的一个类:

/// 
/// Zip操作基于 DotNetZip 的封装
/// 
public static class ZipUtils
{
/// 
/// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】
/// 
/// 
/// 
public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
{
MemoryStream compressedStream = new MemoryStream();
if (sourceStream != null)
{
long sourceOldPosition = 0;
try
{
sourceOldPosition = sourceStream.Position;
sourceStream.Position = 0;
using (ZipFile zip = new ZipFile())
{
zip.AddEntry(entryName, sourceStream);
zip.Save(compressedStream);
compressedStream.Position = 0;
}
}
catch
{
}
finally
{
try
{
sourceStream.Position = sourceOldPosition;
}
catch
{
}
}
}
return compressedStream;
}
 
 
/// 
/// 得到指定的字节数组的ZIP解压流对象
/// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件
/// 
/// 
/// 
public static Stream ZipDecompress(byte[] data)
{
Stream decompressedStream = new MemoryStream();
if (data != null)
{
try
{
MemoryStream dataStream = new MemoryStream(data);
using (ZipFile zip = ZipFile.Read(dataStream))
{
if (zip.Entries.Count > 0)
{
zip.Entries.First().Extract(decompressedStream);
// Extract方法中会操作ms,后续使用时必须先将Stream位置归零,否则会导致后续读取不到任何数据
// 返回该Stream对象之前进行一次位置归零动作
decompressedStream.Position = 0;
}
}
}
catch
{
}
}
return decompressedStream;
}
 
/// 
/// 压缩ZIP文件
/// 支持多文件和多目录,或是多文件和多目录一起压缩
/// 
/// 待压缩的文件或目录集合
/// 压缩后的文件名
/// 是否按目录结构压缩
/// 
成功:true/失败:false
public static bool CompressMulti(List
list, string strZipName, bool IsDirStruct)
{
try
{
using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码
{
foreach (string path in list)
{
string fileName = Path.GetFileName(path);//取目录名称
//如果是目录
if (Directory.Exists(path))
{
if (IsDirStruct)//按目录结构压缩
{
zip.AddDirectory(path, fileName);
}
else//目录下的文件都压缩到Zip的根目录
{
zip.AddDirectory(path);
}
}
if (File.Exists(path))//如果是文件
{
zip.AddFile(path);
}
}
zip.Save(strZipName);//压缩
return true;
}
}
catch (Exception)
{
return false;
}
}
 
/// 
/// 解压ZIP文件
/// 
/// 待解压的ZIP文件
/// 解压的目录
/// 是否覆盖
/// 
成功:true/失败:false
public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
{
try
{
ReadOptions options = new ReadOptions();
options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码
using (ZipFile zip = ZipFile.Read(strZipPath, options))
{
foreach (ZipEntry entry in zip)
{
if (string.IsNullOrEmpty(strUnZipPath))
{
strUnZipPath = strZipPath.Split('.').First();
}
if (overWrite)
{
entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖
}
else
{
entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖
}
}
return true;
}
}
catch (Exception)
{
return false;
}
}
 
 
}

使用方法:

1.压缩文件

  List<string> list = new List<string>();

  list.Add(@"D:\Test\ss");
  list.Add(@"D:\Test\test1.jpg");
  list.Add(@"D:\公司文件.txt");
  list.Add(@"D:\Test\ss.xml"); 
bool isSuc =ZipUtils. CompressMulti(list, "D:\\Test2.zip",true);

2.解压文件

  bool isSuc = ZipUtils.Decompression("D:\\Test\\Test1.zip", "D:\\Teest", true);

更详细的例子在这里:

转载于:https://www.cnblogs.com/sczw-maqing/p/3375835.html

你可能感兴趣的文章
keil5 配置 stm32f103rc 软件仿真
查看>>
RESTful到底是什么玩意??
查看>>
Oracle创建视图的一个问题
查看>>
(一)线性表
查看>>
hdu 1003 Max Sum (DP)
查看>>
mysql增备
查看>>
[APIO2015]雅加达的摩天楼
查看>>
andorid之帧布局FrameLayout
查看>>
(转,记录用)jQuery页面加载初始化的3种方法
查看>>
C++常量的引用 const
查看>>
51nod 1101 换零钱 【完全背包变形/无限件可取】
查看>>
python单例设计模式(待补充)
查看>>
Binary Tree Inorder Traversal
查看>>
HDU 1394 Minimum Inversion Number (数据结构-线段树)
查看>>
ansible-playbook && Roles && include
查看>>
String s String s=null和String s="a"区别
查看>>
[Alpha阶段]第二次Scrum Meeting
查看>>
关于Java 8 forEach
查看>>
.NET设计模式(1):1.1 单例模式(Singleton Pattern)
查看>>
创建模态对话框和非模态对话框
查看>>