DeflateStream throws exception when inflating PDF streams

Hi,
I tried to use System.IO.Compression.DeflateStream
to decompress streams from a PDF file. The streams use
FlateDecode compression, which is supposed to be
compatible with DeflateStream (both are supposed to comply
with RFC 1951). In fact, I can successfully inflate
these PDF streams using ZLIB library.

But when I try using System.IO.Compression.DeflateStream,
it consistently throws System.IO.InvalidDataException:
"Block length does not match with its complement."

Is there any way to fix that

Thank you
John



Answer this question

DeflateStream throws exception when inflating PDF streams

  • Teymur Hajiyev

    Hi!

    Did you ever get a solution to this problem

    Thomas


  • Pirgher

    Still no replies
    I will wait one more day, then try posting a bug report.
    John

  • Looooooka

    DeflateStream and GZipStream have some bugs in them. I tried to compress a block of 64k worth of Es and the decompressed buffer is missing 2 E's. Here is the code.

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Linq;

    using System.Security;

    using System.Security.Cryptography;

    using System.Text;

    using NUnit.Framework;

    [TestFixture]

    public class PositiveZip

    {

    /// <summary>

    /// Test the block compression and decompression APIs.

    /// </summary>

    [Test]

    public void ZipTestCase01001()

    {

    int plainbuffsize = 1024 * 64;

    byte[] plainblock64K = new byte[plainbuffsize];

    for (int pbindex = 0; pbindex < plainbuffsize; pbindex++)

    {

    plainblock64K[pbindex] = 0x45;

    }

    MemoryStream uncompstream = new MemoryStream(plainblock64K);

    MemoryStream compstream = new MemoryStream();

    Zip.CompressStreamToStream(uncompstream, compstream);

    byte[] compbuff = compstream.ToArray();

    Assert.Less(compbuff.Length, plainblock64K.Length);

    compstream.Position = 0;

    MemoryStream recoverstream = new MemoryStream();

    Zip.DeCompressStreamToStream(compstream, recoverstream);

    byte[] recoverbuffer = recoverstream.ToArray();

    Assert.AreEqual(plainblock64K.Length, recoverbuffer.Length);

    Assert.AreEqual(plainblock64K, recoverbuffer);

    }

    }

    static public class Zip

    {

    static public void CompressStreamToStream(Stream instream, Stream outstream)

    {

    GZipStream deflate = new GZipStream(outstream, CompressionMode.Compress, true);

    byte[] buffer = new byte[4096];

    do

    {

    int bytesread = instream.Read(buffer, 0, 4096);

    if (bytesread <= 0)

    break;

    deflate.Write(buffer, 0, bytesread);

    } while (true);

    return;

    }

    static public void DeCompressStreamToStream(Stream instream, Stream outstream)

    {

    GZipStream deflate = new GZipStream(instream, CompressionMode.Decompress, true);

    byte[] buffer = new byte[4096];

    do

    {

    int bytesread = deflate.Read(buffer, 0, 4096);

    if (bytesread <= 0)

    break;

    outstream.Write(buffer, 0, bytesread);

    } while (true);

    return;

    }

    }


  • Raymond Chen

    Turns out it's bloody PDF at fault. I went hunting through the iTextSharp source, and found out (not that the comments indicate this) that PDF slaps a 2 byte header onto the front of the DEFLATE stream.

    Simply ReadByte twice on your input stream, and THEN pass it to DeflateStream and it works a treat.



  • lucasjordan

    And it looks like there's been no movement on this issue in 2 months...

  • Karsinogeeni

  • Shula

    I got the same problem here ... any solution

  • paragp

    This issue with this is that I wasn't closeing the deflate stream. When you don't close a deflate stream it doesn't flush immediately to the underlying stream.


  • Fidencio Monroy

    John,

    Can you post a bug report on the Microsoft Product Feedback Center with a project that reproduces the problem

    Regards

    David



  • DeflateStream throws exception when inflating PDF streams