c#使用DotNetZip封装类操作zip文件(创建/读取/更新)实例
时间:2023-04-08 02:00:19
下载地址在这里:http://dotnetzip.codeplex.com/
下载到的包里有很多个dll文件,一般引用Ionic.Zip.dll就可以:
然后引用这个命名空间:
using Ionic.Zip;
以下是我自己封装的一个类:
/// <summary>
/// Zip操作基于 DotNetZip 的封装
/// </summary>
public static class ZipUtils
{
/// <summary>
/// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】
/// </summary>
/// <param name="sourceStream"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 得到指定的字节数组的ZIP解压流对象
/// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件
/// </summary>
/// <param name="sourceStream"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 压缩ZIP文件
/// 支持多文件和多目录,或是多文件和多目录一起压缩
/// </summary>
/// <param name="list">待压缩的文件或目录集合</param>
/// <param name="strZipName">压缩后的文件名</param>
/// <param name="IsDirStruct">是否按目录结构压缩</param>
/// <returns>成功:true/失败:false</returns>
public static bool CompressMulti(List<string> 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;
}
}
/// <summary>
/// 解压ZIP文件
/// </summary>
/// <param name="strZipPath">待解压的ZIP文件</param>
/// <param name="strUnZipPath">解压的目录</param>
/// <param name="overWrite">是否覆盖</param>
/// <returns>成功:true/失败:false</returns>
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);
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis多表关联查询的实现(DEMO)
![](https://img.aspxhome.com/file/2023/3/67613_0s.png)
BroadcastReceiver静态注册案例详解
![](https://img.aspxhome.com/file/2023/6/139286_0s.gif)
Android 10 启动分析之init语法详解
Java如何提供给第三方使用接口方法详解
Java文件上传下载、邮件收发实例代码
obix协议在java中的配置和使用详解
![](https://img.aspxhome.com/file/2023/5/60275_0s.jpg)
RandomId生成随机字符串详解实例
![](https://img.aspxhome.com/file/2023/0/100230_0s.jpg)
基于jstl 标签的使用介绍
Android简单实现菜单拖拽排序的功能
Android实现上拉加载更多以及下拉刷新功能(ListView)
![](https://img.aspxhome.com/file/2023/0/108320_0s.png)
Android编程基础之简单Button事件响应综合提示控件Toast应用示例
![](https://img.aspxhome.com/file/2023/6/137426_0s.jpg)
微信开发之使用java获取签名signature
![](https://img.aspxhome.com/file/2023/3/65203_0s.jpg)
IDEA的Swing可视化插件JFormDesigner详解
![](https://img.aspxhome.com/file/2023/6/62996_0s.jpg)
Java常量池知识点总结
![](https://img.aspxhome.com/file/2023/2/66712_0s.png)
将cantk runtime嵌入到现有的APP中的方法
Spring Cloud Alibaba Nacos Config加载配置详解流程
详解如何在C#中使用投影(Projection)
![](https://img.aspxhome.com/file/2023/1/119551_0s.png)
SpringBoot项目为何引入大量的starter?如何自定义starter?
![](https://img.aspxhome.com/file/2023/5/78955_0s.webp)
java substring 截取字符串的方法
Android编程实现图片的颜色处理功能示例
![](https://img.aspxhome.com/file/2023/8/92588_0s.gif)