ICSharpCode.SharpZipLib 初级使用

简介: ICSharpCode.SharpZipLib 初级使用

其中将压缩包进行服务器端解压的过程就是通过ICSharpCode.SharpZipLib.dll来实现的。对于这个dll文件,可以通过搜索这个dll文件的名字下载到。

原来没有使用过,所以拿来帮助文档依葫芦画瓢。

  1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用;
  2. 在需要使用到ICSharpCode.SharpZipLib中定义的类的编码界面中将其导入(Imports)

(在C#中是using);

  1. 在选择命名空间的时候,你会发现在有这样几个同级的命名空间:

ICSharpCode.SharpZipLib.BZip2;

ICSharpCode.SharpZipLib.GZip;

ICSharpCode.SharpZipLib.Tar;

ICSharpCode.SharpZipLib.Zip。

这四个命名空间就对应着四种文件压缩方式,其中我们用的较多的是Zip的压缩方式,因为通过WinRAR软件就可以将文件压缩成.Zip的压缩包。

关于这四种压缩算法的介绍可以从维基百科上得到,这里就不再赘述了。

///
/// ZIP:压缩单个文件
/// add yuangang by 2016-06-13
///
/// 需要压缩的文件(绝对路径)
/// 压缩后的文件路径(绝对路径)
/// 压缩后的文件名称(文件名,默认 同源文件同名)
/// 压缩等级(0 无 - 9 最高,默认 5)
/// 缓存大小(每次写入文件大小,默认 2048)
/// 是否加密(默认 加密)
public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = true)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
}

//文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\" + ZipedFileName + ".zip";

using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\") + 1);

ZipEntry ZipEntry = new ZipEntry(fileName);

if (IsEncrypt)
{
//压缩文件加密
ZipStream.Password = “123”;
}

ZipStream.PutNextEntry(ZipEntry);

//设置压缩级别
ZipStream.SetLevel(CompressionLevel);

//缓存大小
byte[] buffer = new byte[BlockSize];

int sizeRead = 0;

try
{
do
{
sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
}
while (sizeRead > 0);
}
catch (System.Exception ex)
{
throw ex;
}

StreamToZip.Close();
}

ZipStream.Finish();
ZipStream.Close();
}

ZipFile.Close();
}
}

///
/// ZIP:压缩文件夹
/// add yuangang by 2016-06-13
///
/// 需要压缩的文件夹(绝对路径)
/// 压缩后的文件路径(绝对路径)
/// 压缩后的文件名称(文件名,默认 同源文件夹同名)
/// 是否加密(默认 加密)
public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
{
//如果目录不存在,则报错
if (!System.IO.Directory.Exists(DirectoryToZip))
{
throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
}

//文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\" + ZipedFileName + ".zip";

using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
if (IsEncrypt)
{
//压缩文件加密
s.Password = “123”;
}
ZipSetp(DirectoryToZip, s, "");
}
}
}
///
/// 递归遍历目录
/// add yuangang by 2016-06-13
///
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();

string[] filenames = Directory.GetFileSystemEntries(strDirectory);

foreach (string file in filenames)// 遍历所有的文件和目录
{

if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\") + 1);
pPath += "\";
ZipSetp(file, s, pPath);
}

else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

string fileName = parentPath + file.Substring(file.LastIndexOf("\") + 1);
ZipEntry entry = new ZipEntry(fileName);

entry.DateTime = DateTime.Now;
entry.Size = fs.Length;

fs.Close();

crc.Reset();
crc.Update(buffer);

entry.Crc = crc.Value;
s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);
}
}
}
}

///
/// ZIP:解压一个zip文件
/// add yuangang by 2016-06-13
///
/// 需要解压的Zip文件(绝对路径)
/// 解压到的目录
/// 解压密码
/// 是否覆盖已存在的文件
public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
{
//如果解压到的目录不存在,则报错
if (!System.IO.Directory.Exists(TargetDirectory))
{
throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
}
//目录结尾
if (!TargetDirectory.EndsWith("\")) { TargetDirectory = TargetDirectory + "\"; }

using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
{
zipfiles.Password = Password;
ZipEntry theEntry;

while ((theEntry = zipfiles.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\";

string fileName = Path.GetFileName(pathToZip);

Directory.CreateDirectory(TargetDirectory + directoryName);

if (fileName != "")
{
if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipfiles.Read(data, 0, data.Length);

if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}

zipfiles.Close();
}
}

相关文章
|
安全 数据挖掘 Linux
Linux命令rpm深度解析
`rpm`是Linux下的软件包管理器,用于安装、升级、卸载和查询`.rpm`包,常见于Red Hat系Linux。它管理依赖、维护软件信息数据库,支持版本控制和安全验证。常用命令如`-i`安装,`-U`升级,`-e`卸载,`-q`查询。安装时用`-v`和`-h`可查看详细信息和进度。注意依赖关系、权限和签名验证,最佳实践包括使用仓库、定期更新和备份数据。
|
C#
ICSharpCode.TextEditor使用及扩展
SharpDevelop (#develop)有很多“副产品”,其中最出名的应算SharpZipLib (#ziplib),纯C#的ZIP类库,而在SharpDevelop (#develop)中,“隐藏”了很多优秀的类库,其中ICSharpCode.TextEditor是表表者。
3177 0
|
3月前
|
人工智能 IDE 定位技术
Understand-Anything:不用硬啃源码,把项目变成一张能追问的知识图谱
Understand-Anything 是一款开源AI工具,通过静态分析+多智能体理解,自动构建代码库知识图谱,帮开发者快速掌握系统架构、业务流程与模块依赖。支持中文、影响分析、新人引导等,让读代码前先有“地图”。(238字)
1196 3
Understand-Anything:不用硬啃源码,把项目变成一张能追问的知识图谱
|
5月前
|
开发框架 安全 C#
【.NET】.NET 4.8下载 | .NET Framework 4.8安装使用指南(附安装包+图文步骤)
本文详解.NET Framework 4.8——微软最后也是最稳定的传统框架版本。它兼容性好、安全性高,是运行大量Windows软件(如办公工具、游戏、企业应用)的必备环境。含下载地址、安装步骤及常见错误(如0x800F081F)解决方案,适合普通用户与开发者参考。(239字)
|
9月前
|
人工智能 算法 搜索推荐
技术与合规两条线深度解析番茄写小说要不要勾选 AI(包括 AI 润色 / AI 修改 / AI 扩写)-卓伊凡
技术与合规两条线深度解析番茄写小说要不要勾选 AI(包括 AI 润色 / AI 修改 / AI 扩写)-卓伊凡
2975 0
|
8月前
|
SQL 人工智能 关系型数据库
【RuoYi-SpringBoot3-Pro】:想要什么数据库都有!三步教你轻松添加新支持
RuoYi-SpringBoot3-Pro 支持多数据库扩展!本文教你三步添加新数据库:初始化SQL适配、MyBatis配置新增databaseId、Dify智能体建表。轻松实现MySQL、PostgreSQL、达梦等多库兼容,结合AI快速生成建表语句,提升开发效率。
315 0
【RuoYi-SpringBoot3-Pro】:想要什么数据库都有!三步教你轻松添加新支持
|
数据采集 Web App开发 调度
Headless Chrome 优化:减少内存占用与提速技巧
在数据驱动的时代,爬虫技术至关重要。本文聚焦 Headless Chrome 优化方案,解决传统爬虫内存占用高、效率低等问题。通过无界面模式、代理 IP等配置,显著降低资源消耗并提升速度。实际案例中,该方案用于采集汽车点评数据,性能提升明显:内存占用降低 30%-50%,页面加载提速 40%-60%。结合技术架构图与演化树,全面解析爬虫技术演进,助力高效数据采集。
1012 0
Headless Chrome 优化:减少内存占用与提速技巧
|
前端开发 Java 调度
Spring Webflux 是 Spring Framework 提供的响应式编程支持
Spring Webflux 是 Spring Framework 提供的响应式编程支持
585 2
|
存储 Linux Docker
Docker 修改镜像存储位置(WSL2)
Docker 修改镜像存储位置(WSL2)
843 0