设置zip文件的密码

时间:2018-08-27 08:09:19

标签: .net-core zipfile

我正在尝试使用带有.Net Core的this库为zip文件设置密码。

我遵循this示例来设置密码,但是,一旦创建了zip文件,就已经在其中,并且已经创建了zip文件,但是没有密码

userRates[rateCategories.name$0]

3 个答案:

答案 0 :(得分:1)

我使用了Wiki中的示例,并且没有问题。

代码:

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 :(得分:1)

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

using (ICSharpCode.SharpZipLib.Zip.ZipFile ZipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(Path.GetFileName(destinationPath)))
{
    ZipFile.BeginUpdate();
    ZipFile.Password = "foo";
    ZipFile.Add(destinationPath, "");
    ZipFile.CommitUpdate();
}

答案 2 :(得分:1)

以上答案均不适合我,
话虽如此,我在对我有用的SharpZipLib库中发现了this Fast Zip类。

// 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

相关问题