通过ICSharpCode.SharpZipLib类库对zip文件进行压缩与解压

2024-01-03 21:58

本文主要是介绍通过ICSharpCode.SharpZipLib类库对zip文件进行压缩与解压,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;namespace DataDisplay.Utilities
{public class ZipHelper{#region 压缩/// <summary>   /// 递归压缩文件夹的内部方法   /// </summary>   /// <param name="folderToZip">要压缩的文件夹路径</param>   /// <param name="zipStream">压缩输出流</param>   /// <param name="parentFolderName">此文件夹的上级文件夹</param>   /// <returns></returns>   private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName){bool result = true;string[] folders, files;ZipEntry ent = null;FileStream fs = null;Crc32 crc = new Crc32();try{ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));zipStream.PutNextEntry(ent);zipStream.Flush();files = Directory.GetFiles(folderToZip);foreach (string file in files){fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)));ent.DateTime = DateTime.Now;ent.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);ent.Crc = crc.Value;zipStream.PutNextEntry(ent);zipStream.Write(buffer, 0, buffer.Length);}}catch{result = false;}finally{if (fs != null){fs.Close();fs.Dispose();}if (ent != null){ent = null;}GC.Collect();GC.Collect(1);}folders = Directory.GetDirectories(folderToZip);foreach (string folder in folders)if (!ZipDirectory(folder, zipStream, folderToZip))return false;return result;}/// <summary>   /// 压缩文件夹    /// </summary>   /// <param name="folderToZip">要压缩的文件夹路径</param>   /// <param name="zipedFile">压缩文件完整路径</param>   /// <param name="password">密码</param>   /// <returns>是否压缩成功</returns>   public static bool ZipDirectory(string folderToZip, string zipedFile, string password){bool result = false;if (!Directory.Exists(folderToZip))return result;ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));zipStream.SetLevel(6);if (!string.IsNullOrEmpty(password)) zipStream.Password = password;result = ZipDirectory(folderToZip, zipStream, "");zipStream.Finish();zipStream.Close();return result;}/// <summary>   /// 压缩文件夹   /// </summary>   /// <param name="folderToZip">要压缩的文件夹路径</param>   /// <param name="zipedFile">压缩文件完整路径</param>   /// <returns>是否压缩成功</returns>   public static bool ZipDirectory(string folderToZip, string zipedFile){bool result = ZipDirectory(folderToZip, zipedFile, null);return result;}/// <summary>   /// 压缩文件   /// </summary>   /// <param name="fileToZip">要压缩的文件全名</param>   /// <param name="zipedFile">压缩后的文件名</param>   /// <param name="password">密码</param>   /// <returns>压缩结果</returns>   public static bool ZipFile(string fileToZip, string zipedFile, string password){bool result = true;ZipOutputStream zipStream = null;FileStream fs = null;ZipEntry ent = null;if (!File.Exists(fileToZip))return false;try{fs = File.OpenRead(fileToZip);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);fs.Close();fs = File.Create(zipedFile);zipStream = new ZipOutputStream(fs);if (!string.IsNullOrEmpty(password)) zipStream.Password = password;ent = new ZipEntry(Path.GetFileName(fileToZip));zipStream.PutNextEntry(ent);zipStream.SetLevel(6);zipStream.Write(buffer, 0, buffer.Length);}catch{result = false;}finally{if (zipStream != null){zipStream.Finish();zipStream.Close();}if (ent != null){ent = null;}if (fs != null){fs.Close();fs.Dispose();}}GC.Collect();GC.Collect(1);return result;}/// <summary>   /// 压缩文件   /// </summary>   /// <param name="fileToZip">要压缩的文件全名</param>   /// <param name="zipedFile">压缩后的文件名</param>   /// <returns>压缩结果</returns>   public static bool ZipFile(string fileToZip, string zipedFile){bool result = ZipFile(fileToZip, zipedFile, null);return result;}/// <summary>   /// 压缩文件或文件夹   /// </summary>   /// <param name="fileToZip">要压缩的路径</param>   /// <param name="zipedFile">压缩后的文件名</param>   /// <param name="password">密码</param>   /// <returns>压缩结果</returns>   public static bool Zip(string fileToZip, string zipedFile, string password){bool result = false;if (Directory.Exists(fileToZip))result = ZipDirectory(fileToZip, zipedFile, password);else if (File.Exists(fileToZip))result = ZipFile(fileToZip, zipedFile, password);return result;}/// <summary>   /// 压缩文件或文件夹   /// </summary>   /// <param name="fileToZip">要压缩的路径</param>   /// <param name="zipedFile">压缩后的文件名</param>   /// <returns>压缩结果</returns>   public static bool Zip(string fileToZip, string zipedFile){bool result = Zip(fileToZip, zipedFile, null);return result;}#endregion#region 解压/// <summary>  /// 功能:解压zip格式的文件。  /// </summary>  /// <param name="zipFilePath">压缩文件路径</param>  /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  /// <param name="err">出错信息</param>  /// <returns>解压是否成功</returns>  public bool UnZipFile(string zipFilePath, string unZipDir)// , out string err  {// err = "";  if (zipFilePath == string.Empty){//err = "压缩文件不能为空!";  return false;}if (!File.Exists(zipFilePath)){//err = "压缩文件不存在!";  return false;}//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  if (unZipDir == string.Empty)unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));if (!unZipDir.EndsWith("\\"))unZipDir += "\\";if (!Directory.Exists(unZipDir))Directory.CreateDirectory(unZipDir);try{using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath.ToLower()))){ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(theEntry.Name);string fileName = Path.GetFileName(theEntry.Name);if (directoryName.Length > 0){Directory.CreateDirectory(unZipDir + directoryName);}if (!directoryName.EndsWith("\\"))directoryName += "\\";if (fileName != String.Empty){using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)){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;}}}}}//while  }}catch (Exception ex){//err = ex.Message;  return false;}return true;}//解压结束  #endregion}
}

这篇关于通过ICSharpCode.SharpZipLib类库对zip文件进行压缩与解压的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567179

相关文章

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

Java 压缩包解压实现代码

《Java压缩包解压实现代码》Java标准库(JavaSE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RA... 目录一、解压压缩包1.zip解压代码实现:2.rar解压代码实现:3.调用解压方法:二、注意事项三、总

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代