Search code examples
c#aesdotnetzipbzip2zipinputstream

DotNetZip ZipInputStream issue


I use DotNetZip 1.9.3 library and have faced a problem using BZip2 and AES256 both for 1 entry within 1 archive. ZipOutputStream works fine, but ZipInputStream gets wrong BZip2 header. If I use commom deflate algorithm, everything is fine.

I create ZipOutputStream (from file stream):

protected override Stream OnCreateStream(string path)
{
    var encryptedStream = new ZipOutputStream(base.OnCreateStream(path))
    {
        Password = this.Password,
        Encryption = EncryptionAlgorithm.WinZipAes256,
   //     CompressionMethod = CompressionMethod.BZip2,
    };
    var entry = encryptedStream.PutNextEntry("_");
    return encryptedStream;
}

And I always dispose ZipOutputStream when the job is done.

Stream to read zip archive:

protected override Stream OnCreateStream(string path)
{
    ZipInputStream stream = new ZipInputStream(base.OnCreateStream(path))
    {
        Password = this.Password
    };
    var entry = stream.GetNextEntry();
    return stream;
}

If I uncomment compression method and tries to read ZipInputStream I get exception:

Not a valid BZip2 stream. byte 1, expected '90', got '107'
   в Ionic.BZip2.BZip2InputStream.CheckMagicChar(Char expected, Int32 position)
   в Ionic.BZip2.BZip2InputStream.init()
   в Ionic.BZip2.BZip2InputStream..ctor(Stream input, Boolean leaveOpen)
   в Ionic.Zip.ZipEntry.GetExtractDecompressor(Stream input2)
   в Ionic.Zip.ZipEntry.InternalOpenReader(String password)
   в Ionic.Zip.ZipInputStream.SetupStream()
   в Ionic.Zip.ZipInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   в System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)
   в System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)

Any ideas?


Solution

  • I know it's quite old topic, but it may be still useful for someone googling for solution.

    I've noticed that order of invoking compression's property setters is important. With

    zip.CompressionMethod = CompressionMethod.BZip2;
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    

    set before AddEntry makes ZIP file unreadable by DotNetZip. Right place of setting CompressionMethod and CompressionLevel is just before saving:

    using (var zip = new ZipFile(zipPath, Encoding.UTF8))
    {
        // set zip properties except compression ones
        zip.Encryption = EncryptionAlgorithm.WinZipAes256;
        zip.Password = "pass";
    
        // add entries
        zip.AddEntry("whatever", stream);
    
        // set compression just before Save()
        zip.CompressionMethod = CompressionMethod.BZip2;
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    
        zip.Save();
    }