Ionic Zip password set in azure blob storage and set best compression for zip folder not files

825 views Asked by At

How to set zip folder best compression and set a password for Zip folder, I have few Code that i am used please help me how to set best compression and password for zip folder.

using (var zipStream = new MemoryStream())                    
{ 
zipFile.Save(zipStream);
zipStream.Seek(0, SeekOrigin.Begin);                        
CloudBlockBlob cblob = container.GetBlockBlobReference("MockTestZip/MT-" + this.mockExamId + ".zip");
cblob.UploadFromStream(zipStream);                        
}

I just added these two lines in it but password is not set in zip folder in azure blob storage

zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zipFile.Password = "encrypt";
1

There are 1 answers

0
Bruce Chen On

According to your description, I leverage DotNetZip version 1.10.1 to test this issue on my side. And here is the remark about the Password property in ZipFile:

When writing a zip archive, this password is applied to the entries, not to the zip archive itself. It applies to any ZipEntry subsequently added to the ZipFile, using one of the AddFile, AddDirectory, AddEntry, or AddItem methods, etc.

Based on your scenario, I assumed that you could store the files in a zip file without password, and then store the zip file within a second outer zip file with password as follows:

using (ZipFile zipFile = new ZipFile())
{
    zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    zipFile.Password = "{your-password}";
    zipFile.AddFile(@"inner.zip",""); //Passing the empty string ("") to store your zip file at the root path within the archive.
    using (var zipStream = new MemoryStream())
    {
        zipFile.Save(zipStream);
        zipStream.Seek(0, SeekOrigin.Begin);
        CloudBlockBlob cblob = container.GetBlockBlobReference("MockTestZip/MT-" + this.mockExamId + ".zip");
        cblob.UploadFromStream(zipStream);
    }
}