首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为zip文件设置密码

为zip文件设置密码
EN

Stack Overflow用户
提问于 2018-08-27 16:09:19
回答 3查看 3.4K关注 0票数 3

我正在尝试使用带有.Net核心的SharpZipLib库为压缩文件设置密码。

我按照this示例来设置密码,但是一旦创建了压缩文件,文件就在里面,压缩文件就创建了,但是没有密码。

代码语言:javascript
运行
复制
// Create a password for the Zipfolder
// https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
using (ICSharpCode.SharpZipLib.Zip.ZipFile ZipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(Path.GetFileName(destinationPath)))
{
     ZipFile.Password = "foo";
     ZipFile.Add(destinationPath, "");
}
EN

回答 3

Stack Overflow用户

发布于 2018-08-27 19:51:23

上面的答案对我来说都不起作用。

说到这里,我在SharpZipLib库中找到了适合我的this Fast Zip类。

代码语言:javascript
运行
复制
// Create a password for the Zipfolder
// https://github.com/icsharpcode/SharpZipLib/wiki
ICSharpCode.SharpZipLib.Zip.FastZip zipFile = new ICSharpCode.SharpZipLib.Zip.FastZip();
zipFile.Password = "foo";
zipFile.CreateEmptyDirectories = true;
zipFile.CreateZip(destinationPath,tempPath, true, "");

但是,我唯一不喜欢它的地方是它没有实现IDisposable

票数 2
EN

Stack Overflow用户

发布于 2018-08-27 16:45:12

我使用了wiki中的例子,它工作起来没有任何问题。

代码:

代码语言:javascript
运行
复制
using (FileStream fsOut = File.Create(@"d:\temp\sharplib_pwtest.zip"))
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)) {
    zipStream.SetLevel(3);
    zipStream.Password = "Testpassword";
    var folderName = @"D:\temp\sharpZipLibTest\";
    int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
    CompressFolder(folderName, zipStream, folderOffset);
}
票数 1
EN

Stack Overflow用户

发布于 2018-08-27 16:56:30

您只需输入BeginUpdate()CommitUpdate(),这将反映在您的输出中。

代码语言:javascript
运行
复制
using (ICSharpCode.SharpZipLib.Zip.ZipFile ZipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(Path.GetFileName(destinationPath)))
{
    ZipFile.BeginUpdate();
    ZipFile.Password = "foo";
    ZipFile.Add(destinationPath, "");
    ZipFile.CommitUpdate();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52035101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档