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

DeflateStream throws exception when inflating PDF streams
Teymur Hajiyev
Hi!
Did you ever get a solution to this problem
Thomas
Pirgher
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++){
}
MemoryStream uncompstream = new MemoryStream(plainblock64K);
MemoryStream compstream = new MemoryStream();
Zip.CompressStreamToStream(uncompstream, compstream);
}
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
Karsinogeeni
Looks like he already has:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx feedbackid=3f6e3d2a-23d1-46dd-aaa0-53df18df4190.
Shula
paragp
Fidencio Monroy
John,
Can you post a bug report on the Microsoft Product Feedback Center with a project that reproduces the problem
Regards
David