简体   繁体   English

使用密码在 .net 中创建 zip 文件

[英]create zip file in .net with password

I'm working on a project that I need to create zip with password protected from file content in c#.我正在开发一个项目,我需要在 C# 中创建带有密码保护的 zip 文件。

Before I've use System.IO.Compression.GZipStream for creating gzip content.在我使用 System.IO.Compression.GZipStream 创建 gzip 内容之前。 Does .net have any functionality for create zip or rar password protected file? .net 是否具有创建 zip 或 rar 密码保护文件的功能?

Take a look at DotNetZip (@AFract supplied a new link to GitHub in the comments)看看DotNetZip (@AFract 在评论中提供了一个新的 GitHub 链接)

It has got pretty geat documentation and it also allow you to load the dll at runtime as an embeded file.它有非常好的文档,它还允许您在运行时将 dll 作为嵌入文件加载。

Unfortunately there is no such functionality in the framework.不幸的是,框架中没有这样的功能。 There is a way to make ZIP files, but without password.有一种方法可以制作 ZIP 文件,但无需密码。 If you want to create password protected ZIP files in C#, I'd recommend SevenZipSharp .如果您想在 C# 中创建受密码保护的 ZIP 文件,我建议使用SevenZipSharp It's basically a managed wrapper for 7-Zip.它基本上是 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);
}

I want to add some more alternatives.我想添加一些更多的选择。

For .NET one can use SharpZipLib , for Xamarin use SharpZipLib.Portable .对于 .NET 可以使用SharpZipLib ,对于 Xamarin 可以使用SharpZipLib.Portable

Example for .NET: .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;
}

More samples can be found here .可以在此处找到更多示例。

If you can live without password functionality, one can mention ZipStorer or the built in .NET function in System.IO.Compression .如果您可以在没有密码功能的情况下生活,可以提及ZipStorerSystem.IO.Compression的内置 .NET 功能。

DotNetZip worked great in a clean way. DotNetZip以一种干净的方式工作得很好。

DotNetZip is a FAST, FREE cla-ss library and toolset for manipulating zip files.

Code代码

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");
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM