VB.NET中使用ZipArchive为压缩包添加密码是否可行?
使用ZipArchive无法实现压缩包密码保护
- .NET自带的
System.IO.Compression.ZipArchive类不支持为压缩包设置密码保护,这是该API的原生限制,没有内置属性或方法可直接实现密码加密功能。 - 若需生成带密码的压缩包,可参考以下两种方案:
- 使用第三方库(如DotNetZip,可通过NuGet安装),它提供了简洁的密码设置接口,示例代码如下:
Using zip As New Ionic.Zip.ZipFile() zip.Password = "你的密码" zip.AddFile("file1.txt") zip.AddFile("file2.txt") ' 添加剩余3个文件 zip.Save("加密压缩包.zip") End Using - 调用第三方压缩工具的命令行版本(如
7z.exe),通过进程调用完成加密压缩,示例代码如下:Dim compressProcess As New Process() compressProcess.StartInfo.FileName = "7z.exe" compressProcess.StartInfo.Arguments = "a -p你的密码 加密压缩包.zip file1.txt file2.txt file3.txt file4.txt file5.txt" compressProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden compressProcess.Start() compressProcess.WaitForExit()
- 使用第三方库(如DotNetZip,可通过NuGet安装),它提供了简洁的密码设置接口,示例代码如下:
内容的提问来源于stack exchange,提问作者Tim F
相关产品推荐
相关产品推荐

