在.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
相关文章
maui 是什么 .net maui开发入门
c# ConfigureAwaitOptions 在 .NET 8 中的新功能
c# 在 .NET 中,一个进程最多可以创建多少个线程
blazor 是什么 blazor和vue/react对比
.net core 和 .net framework 的区别
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。
10
2026.01.27
在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。
109
2026.01.26
在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。
16
2026.01.26
苹果官方查询网站主要通过 checkcoverage.apple.com/cn/zh/ 进行,可用于查询序列号(SN)对应的保修状态、激活日期及技术支持服务。此外,查找丢失设备请使用 iCloud.com/find,购买信息与物流可访问 Apple (中国大陆) 订单状态页面。
136
2026.01.26
NPD(Narcissistic Personality Disorder)即自恋型人格障碍,是一种心理健康问题,特点是极度夸大自我重要性、需要过度赞美与关注,同时极度缺乏共情能力,背后常掩藏着低自尊和不安全感,影响人际关系、工作和生活,通常在青少年时期开始显现,需由专业人士诊断。
7
2026.01.26
关闭Windows安全中心(Windows Defender)可通过系统设置暂时关闭,或使用组策略/注册表永久关闭。最简单的方法是:进入设置 > 隐私和安全性 > Windows安全中心 > 病毒和威胁防护 > 管理设置,将实时保护等选项关闭。
6
2026.01.26
铁路12306提供起售时间查询、起售提醒、购票预填、候补购票及误购限时免费退票五项服务,并强调官方渠道唯一性与信息安全。
122
2026.01.26
以工资薪金所得为例,应纳税额 = 应纳税所得额 × 税率 - 速算扣除数。应纳税所得额 = 月度收入 - 5000 元 - 专项扣除 - 专项附加扣除 - 依法确定的其他扣除。假设某员工月工资 10000 元,专项扣除 1000 元,专项附加扣除 2000 元,当月应纳税所得额为 10000 - 5000 - 1000 - 2000 = 2000 元,对应税率为 3%,速算扣除数为 0,则当月应纳税额为 2000×3% = 60 元。
35
2026.01.26
oppo云服务https://cloud.oppo.com/可以在云端安全存储您的照片、视频、联系人、便签等重要数据。当您的手机数据意外丢失或者需要更换手机时,可以随时将这些存储在云端的数据快速恢复到手机中。
121
2026.01.26
热门下载
相关下载
精品课程
最新文章
