通过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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详