create zip file in .net with password

48,949

Solution 1

Take a look at DotNetZip (@AFract supplied a new link to GitHub in the comments)

It has got pretty geat documentation and it also allow you to load the dll at runtime as an embeded file.

Solution 2

Unfortunately there is no such functionality in the framework. There is a way to make ZIP files, but without password. If you want to create password protected ZIP files in C#, I'd recommend SevenZipSharp. It's basically a managed wrapper for 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);
}

Solution 3

I want to add some more alternatives.

For .NET one can use SharpZipLib, for Xamarin use SharpZipLib.Portable.

Example for .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.

Solution 4

DotNetZip worked great in a clean way.

DotNetZip is a FAST, FREE class 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");
        }
 }
Share:
48,949
Hamed_gibago
Author by

Hamed_gibago

Programmer and researcher in computer science. Programming C# and python. Databases MsSql, MongoDb, sqlite. Some knowledge of javascript and nodejs love game programming and computer graphics ;) https://www.linkedin.com/in/hamed-ak-59258787/

Updated on January 19, 2022

Comments

  • Hamed_gibago
    Hamed_gibago over 2 years

    I'm working on a project that I need to create zip with password protected from file content in c#.

    Before I've use System.IO.Compression.GZipStream for creating gzip content. Does .net have any functionality for create zip or rar password protected file?