1

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?

4

2 回答 2

2

我知道这是一个很老的话题,但它可能对搜索解决方案的人仍然有用。

我注意到调用压缩的属性设置器的顺序很重要。和

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

之前设置AddEntry会使 ZIP 文件无法被 DotNetZip 读取。正确的设置位置,CompressionMethodCompressionLevel在保存之前:

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();
}
于 2015-05-21T12:33:16.253 回答
0

这可能暗示了这个问题:

和 gzip 一样,bzip2 只是一个数据压缩器。它不是 tar 或 ZIP 之类的存档器;该程序本身没有用于多个文件、加密或存档拆分的功能,但是,在 UNIX 传统中,这些任务依赖于单独的外部实用程序,例如 tar 和 GnuPG。

资料来源:维基百科

于 2014-06-21T22:16:54.527 回答