我正在尝试使用带有.Net核心的SharpZipLib库为压缩文件设置密码。
我按照this示例来设置密码,但是一旦创建了压缩文件,文件就在里面,压缩文件就创建了,但是没有密码。
// 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, "");
}
发布于 2018-08-27 19:51:23
上面的答案对我来说都不起作用。
说到这里,我在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
发布于 2018-08-27 16:45:12
我使用了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);
}
发布于 2018-08-27 16:56:30
您只需输入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();
}
https://stackoverflow.com/questions/52035101
复制相似问题