项目引用ICSharpCode.SharpZipLib.Zip 这个组件 是通过程序包管理器加入的。
ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
try
{
byte[] buffer = new byte[BufferSize];
ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
zipOutStream.PutNextEntry(entry);
zipOutStream.SetLevel(6);
zipOutStream.Password = pwd;
最后一行,已经设置了密码。但生成的文件打开时,却不需要密码。
后来发现密码设置语句不能写在加载ZipEntry之后。应该是这样:
ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
zipOutStream.SetLevel(6);
zipOutStream.Password = pwd;
try
{
byte[] buffer = new byte[BufferSize];
ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
zipOutStream.PutNextEntry(entry);