【问题标题】:create zip file in .net with password使用密码在 .net 中创建 zip 文件
【发布时间】:2016-08-29 11:02:20
【问题描述】:

我正在做一个项目,我需要创建 zip 并使用密码保护 C# 中的文件内容。

在我使用 System.IO.Compression.GZipStream 创建 gzip 内容之前。 .net 是否具有创建 zip 或 rar 密码保护文件的功能?

【问题讨论】:

  • 一般来说,如果 System.IO.Compression.GZipStream 的功能不足以满足您的需求 - 有一个更复杂的 sevenzipsharp.codeplex.com 框架。在 .net 框架内,GZipStream 是创建档案的唯一方法。
  • @VitaliyK 我也推荐 7Zip#,但除了GZipStream,框架中还有其他一些压缩机制(例如DeflateStreamZipPackageZipFile(从 4.5 开始)等)
  • @VitaliyK gzipstream 有密码功能吗?我没有找到任何类型的密码功能
  • @Hamed_gibago 不,您不能使用 GZipStream 对存档进行密码保护。您需要使用其他 zip 框架(7Zip、DotNetZip 等)。

标签: c# .net passwords zip dotnetzip


【解决方案1】:

看看DotNetZip(@AFract 在 cmets 中提供了一个指向 GitHub 的新链接)

它有非常好的文档,它还允许您在运行时将 dll 作为嵌入文件加载。

【讨论】:

  • 我已经下载了这个,但在下载中找不到要在我的项目中使用的 DLL。
  • 这个还在维护吗?原始项目有一个 github 分支,但自从 CodePlex 关闭后,我经常不确定此类项目的状态
  • nuget.org/packages/DotNetZip 'Last Update 3 个月前' 我认为它仍然保持不变。
  • github 存储库绑定到 nuget 包:github.com/haf/DotNetZip.Semverd 不要看 codeplex,它(显然)已经过时了,因为 Codeplex 已经死了。
  • 请提供示例
【解决方案2】:

不幸的是,框架中没有这样的功能。有一种制作 ZIP 文件的方法,但无需密码。如果您想在 C# 中创建受密码保护的 ZIP 文件,我建议您使用 SevenZipSharp。它基本上是 7-Zip 的托管包装器。

SevenZipBase.SetLibraryPath(Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Environment.CurrentDirectory,
        "7za.dll"));

SevenZipCompressor compressor = new SevenZipCompressor();

compressor.Compressing += Compressor_Compressing;
compressor.FileCompressionStarted += Compressor_FileCompressionStarted;
compressor.CompressionFinished += Compressor_CompressionFinished;

string password = @"whatever";
string destinationFile = @"C:\Temp\whatever.zip";
string[] sourceFiles = Directory.GetFiles(@"C:\Temp\YourFiles\");

if (String.IsNullOrWhiteSpace(password))
{
    compressor.CompressFiles(destinationFile, sourceFiles);
}
else
{
    //optional
    compressor.EncryptHeaders = true;
    compressor.CompressFilesEncrypted(destinationFile, password, sourceFiles);
}

【讨论】:

  • 不幸的是,此解决方案仅适用于 Windows,因为它依赖于 7za.dll。其他解决方案跨平台工作。
【解决方案3】:

我想添加更多替代品。

对于 .NET,可以使用 SharpZipLib,对于 Xamarin,可以使用 SharpZipLib.Portable

.NET 示例:

using ICSharpCode.SharpZipLib.Zip;

// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) {

    MemoryStream outputMemStream = new MemoryStream();
    ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
    zipStream.Password = "Your password";

    ZipEntry newEntry = new ZipEntry(zipEntryName);
    newEntry.DateTime = DateTime.Now;

    zipStream.PutNextEntry(newEntry);

    StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
    zipStream.CloseEntry();

    zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
    zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.

    outputMemStream.Position = 0;
    return outputMemStream;

    // Alternative outputs:
    // ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
    byte[] byteArrayOut = outputMemStream.ToArray();

    // GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
    byte[] byteArrayOut = outputMemStream.GetBuffer();
    long len = outputMemStream.Length;
}

更多样例可以在here找到。

如果您可以在没有密码功能的情况下生活,可以提及ZipStorerSystem.IO.Compression 中的内置.NET 功能。

【讨论】:

    【解决方案4】:

    DotNetZip 以干净的方式工作得很好。

    DotNetZip is a FAST, FREE class library and toolset for manipulating zip files.
    

    代码

    static void Main(string[] args)
    {
            using (ZipFile zip = new ZipFile())
            {
    
                zip.Password = "mypassword";
    
                zip.AddDirectory(@"C:\Test\Report_CCLF5\");
                zip.Save(@"C:\Test\Report_CCLF5_PartB.zip");
            }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2010-12-26
      • 2022-08-10
      • 2022-11-04
      相关资源
      最近更新 更多