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

时间:2016-05-04 06:48:31

标签: c# .net passwords zip

我正在开发一个项目,我需要创建一个带有密码保护的zip文件,不受c#中文件内容的限制。

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

4 个答案:

答案 0 :(得分:5)

查看DotNetZip

它有很好的geat文档,它还允许你在运行时加载dll作为嵌入文件。

答案 1 :(得分:1)

不幸的是,框架中没有这样的功能。有一种方法可以制作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);
}

答案 2 :(得分:0)

我想添加更多替代方案。

对于.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功能。

答案 3 :(得分:0)

DotNetZip干净利索。

DotNetZip is a FAST, FREE cla-ss 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");
        }
 }