在.net可以通过多种方式实现的zip的压缩和解压:1,使用system.io.packaging程序; 2,使用第三方类库; 3,通过system.io.compression命名空间中新增的ziparchive,的zipfile类等实现
一,使用system.io.packaging程序压缩和解压
包为一个抽象类,可用于将对象组织到定义的物理格式的单个实体中,从而实现可移植性与高效访问.zip文件是包的主物理格式。其他套餐实现可以使用其他物理格式(如xml文档,数据库或web服务。与文件系统类似,在分层组织的文件夹和文件中引用包中包含的项。虽然包装是抽象类,但package.open方法默认使用zippackage派生类。
system.io.packaging程序在windowsbase.dll中程序集下,使用时需要添加对windowsbase的引用。
1,将整个文件夹压缩成压缩
代码///
///添加一个文件夹及其子文件夹一个包沿着
/// 摘要>
/// 添加文件夹 param>
/// 创建包 param>
/// 覆盖exsisitng文件 param>
/// 回报>
静态布尔packagefolder(字符串文件夹名,字符串compressedfilename,布尔overrideexisting)
{
如果(foldername.endswith(@“\”))
foldername = foldername.remove(foldername.length - 1);
布尔结果= false;
如果(directory.exists(文件夹名)!)
{
返回结果;
}
(!overrideexisting && file.exists(compressedfilename))如果
{
返回结果;
}
尝试
{
使用(套餐包= package.open(compressedfilename,filemode.create))
{
var的filelist = directory.enumeratefiles(文件夹名,“*”,searchoption.alldirectories);
的foreach(在的filelist字符串文件名)
{
//包中的路径是所有文件夹名后的子文件夹
弦pathinpackage;
pathinpackage = path.getdirectoryname(文件名).replace(文件夹名,的string.empty)+“/”+ path.getfilename(文件名);
乌里parturidocument = packurihelper.createparturi(新的uri(pathinpackage,urikind.relative));
的packagepart packagepartdocument = package.createpart(parturidocument,“”,compressionoption.maximum);
使用(的filestream filestream =新的filestream(文件名,filemode.open,fileaccess.read))
{
filestream.copyto(packagepartdocument.getstream());
}
}
}
结果=真;
}
赶上(例外五)
{
抛出新的异常(“错误荏苒文件夹”文件夹名+,e);
}
返回结果;
}
2,将单个文件添加到压缩文件中
的代码///
///压缩文件成zip文件作为容器店
/// 摘要>
/// 文件压缩 param>
/// 归档文件 param>
/// 覆盖现有文件 param>
/// 回报>
静态布尔packagefile(字符串文件名,字符串compressedfilename,布尔overrideexisting)
{
布尔结果= false;
如果(file.exists(文件名)!)
{
返回结果;
}
(!overrideexisting && file.exists(compressedfilename))如果
{
返回结果;
}
尝试
{
乌里parturidocument = packurihelper.createparturi(新的uri(path.getfilename(文件名),urikind.relative));
使用(套餐包= package.open(compressedfilename,filemode.openorcreate))
{
如果(package.partexists(parturidocument))
{
package.deletepart(parturidocument);
}
的packagepart packagepartdocument = package.createpart(parturidocument,“”,compressionoption.maximum);
使用(的filestream filestream =新的filestream(文件名,filemode.open,fileaccess.read))
{
filestream.copyto(packagepartdocument.getstream());
}
}
结果=真;
}
赶上(例外五)
{
抛出新的异常(“错误压缩和解文件”+文件名,e);
}
返回结果;
} 3,压缩文件解压
代码///
///提取物的容器邮编。注:容器必须创建为开放式打包约定(opc)规范
/// 摘要>
/// 文件夹解压包 param>
/// 包文件 param>
/// 覆盖现有文件 param>
/// 回报>
静态布尔uncompressfile(字符串文件夹名,字符串compressedfilename,布尔overrideexisting)
{
布尔结果= false;
尝试
{
如果(file.exists(compressedfilename)!)
{
返回结果;
}
的directoryinfo directoryinfo的=新directoryinfo的(文件夹名);
如果(!directoryinfo.exists)
directoryinfo.create();
使用(套餐包= package.open(compressedfilename,filemode.open,fileaccess.read))
{
的foreach(的packagepart的packagepart在package.getparts())
{
extractpart(的packagepart,文件夹名,overrideexisting);
}
}
结果=真;
}
赶上(例外五)
{
抛出新的异常(“错误解压缩文件”+ compressedfilename,e);
}
返回结果;
}
静态无效extractpart(的packagepart的packagepart,串targetdirectory中,布尔overrideexisting)
{
字符串stringpart = targetdirectory中+ httputility.urldecode(packagepart.uri.tostring())更换('\\','/')。
如果(directory.exists(path.getdirectoryname(stringpart))!)
directory.createdirectory(path.getdirectoryname(stringpart));
如果(!overrideexisting && file.exists(stringpart))
回报;
使用(的filestream filestream =新的filestream(stringpart,filemode.create))
{
packagepart.getstream()copyto从(filestream)。
}
}
使用包压缩文件会在压缩文件自动生成[内容]的.xml,用来描述压缩文件解压支持的文件格式。
代码
类型>同样,如果压缩文件不包含[内容]的.xml文件,或者[内容]的.xml文件不包含所对应扩展名的描述(手动添加的[内容]的.xml也是可以) ,将无法解压文件。二,使用第三方类库压缩的压缩和解压使用比较的有sharpziplib和dotnetzip。
1,sharpziplib,也称为“#ziplib”,基于gpl开源,支持zip,gzip在内焦油和bzip2压缩的压缩和解压缩。
支持.net 1.1,.net 2.0(3.5,4.0)。
(1)zip压缩
codepublic静态无效的邮编(字符串srcfile,串dstfile,int缓冲区大小)
{
的filestream filestreamin =新的filestream
(srcfile,filemode.open,fileaccess.read);
的filestream filestreamout =新的filestream
(dstfile,filemode.create,fileaccess.write);
zipoutputstream zipoutstream =新zipoutputstream(filestreamout);
字节[]缓冲区=新字节;
入门的zipentry =新的zipentry(path.getfilename(srcfile));
zipoutstream.putnextentry(输入);
int大小;
做
{
大小= filestreamin.read(缓冲,0,buffer.length);
zipoutstream.write(缓冲液,0,大小);
}而(大小> 0);
zipoutstream.close();
filestreamout.close();
filestreamin.close();
}(2)解压zipcodepublic静态无效的解压(字符串srcfile,串dstfile,int缓冲区大小)
{
的filestream filestreamin =新的filestream
(srcfile,filemode.open,fileaccess.read);
zipinputstream zipinstream =新zipinputstream(filestreamin);
zipentry的条目= zipinstream.getnextentry();
的filestream filestreamout =新的filestream
(dstfile + @“\”+ entry.name,filemode.create,fileaccess.write);
int大小;
字节[]缓冲区=新字节;
做
{
大小= zipinstream.read(缓冲,0,buffer.length);
filestreamout.write(缓冲液,0,大小);
}而(大小> 0);
zipinstream.close();
filestreamout.close();
filestreamin.close();
} 2,dotnetlib,是基于“ws-pl”开源,使用比较简单(1)压缩代码使用(拉链使用zipfile =新的zipfile())
{
zip.addfile(的“readme.txt”);
zip.addfile(“7440-n49th.png”);
zip.addfile(“2008_annual_report.pdf”);
zip.save(“archive.zip”);
}(2)解压codeprivate无效myextract()
{
字符串ziptounpack =“c1p3sml.zip”;
字符串unpackdirectory =“解压缩文件”;
使用(zip1使用zipfile = zipfile.read(ziptounpack))
{
//这里,我们提取的每个条目,但我们可以有条件地提取
根据条目名称,大小,日期,复选框状态等//
的foreach(zipentry的e在zip1)
{
e.extract(unpackdirectory,extractexistingfileaction.overwritesilently);
}
}
}
三,在.net 4.5使用ziparchive,的zipfile等类压缩和解压
代码静态无效的主要(字串[] args)
{
字符串zippath = @“c:\用户\ exampleuser \ start.zip”
字符串extractpath = @“c:\用户\ exampleuser \提取物”;
字符串newfile = @“c:\用户\ exampleuser \ newfile.txt”
使用(ziparchive存档= zipfile.open(zippath,ziparchivemode.update))
{
archive.createentryfromfile(newfile“newentry.txt”);
archive.extracttodirectory(extractpath);
}
}
0
0
相关文章
C#如何使用FluentValidation C# FluentValidation验证库入门
maui 是什么 .net maui开发入门
c# ConfigureAwaitOptions 在 .NET 8 中的新功能
c# 在 .NET 中,一个进程最多可以创建多少个线程
blazor 是什么 blazor和vue/react对比
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。
391
2026.01.28
本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。
135
2026.01.28
AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。
233
2026.01.28
本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。
8
2026.01.28
本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。
10
2026.01.28
本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。
24
2026.01.27
在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。
124
2026.01.26
在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。
98
2026.01.26
热门下载
相关下载
精品课程
最新文章

