Skip to content

Default encryption when adding to zipfile #571

Answered by piksel
Hogedal asked this question in Q&A
Discussion options

You must be logged in to vote

ZipFile doesn't support adding files with AES encryption. But you can use ZipOutputStream instead:

using(var zipStream = new ZipOutputStream(File.Create($"{archiveOutputFolder}\\{pack.Name}.zip")))
{
    zipStream.Password = _cryptKey;
    foreach (var file in pack.Files)
    {
        var entry = new ZipEntry(file.RelativeFilePath);
        entry.AESKeySize = 256;
        
        zipStream.PutNextEntry(entry);
        using(var sourceStream = File.OpenRead(file.Info.FullName)) 
        {
            sourceStream.CopyTo(zipStream);
        }
    }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Hogedal
Comment options

Answer selected by Hogedal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants