Link to home
Start Free TrialLog in
Avatar of seashell1
seashell1Flag for Afghanistan

asked on

SharpZipLib Inflate caused Unexpected Eof C#

I am working in C# and using SharpZipLib as I am working with Silverlight and .NET.  I want to compress a file and decompress it prior to transfer to make sure that the file size is the same after decompression, just to catch any compression errors.  I have attached my code.  When I inflate the stream, the InflatorInputStream has zero length and I get an error "Unexpected Eof" when I try to read from the InflatorInputStream.  I have reset the position and all the online examples of decompressing a stream are all same.  Am I missing something?  
Just to add that I was using a small text file while testing.
private byte[] CompressFile(byte[] bytesIn)
        {
            MemoryStream ms = new MemoryStream();
            
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
            Stream s = new DeflaterOutputStream(ms, defl);
            s.Write(bytesIn, 0, bytesIn.Length);
            
            byte[] compressedData = (byte[])ms.ToArray();
            ms.Position = 0;
            MemoryStream ms2 = new MemoryStream(compressedData);
            ms2.Position = 0;
            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(false);
            InflaterInputStream s2 = new InflaterInputStream(ms2, inf);
           
            byte[] decompressedBuffer = new byte[32768];
            int totalCount = ReadAllBytesFromStream(s2, decompressedBuffer);
            if (!CompareData(bytesIn, bytesIn.Length, decompressedBuffer, totalCount))
            {
                throw new Exception("Error when compressing the file");
            }
            s.Close();
            s2.Close();

            return compressedData;
        }


        public int ReadAllBytesFromStream(InflaterInputStream stream, byte[] buffer)
        {
            int offset = 0;
            int totalCount = 0;
            while (true)
            {
                int bytesRead = stream.Read(buffer, offset, buffer.Length);
                if (bytesRead <= 0)
                {
                    break;
                }
                offset += bytesRead;
                totalCount += bytesRead;
            }
            return totalCount;
        }

Open in new window

Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Hi,

Put your whole piece of code in try...catch block and let us know exactly which line is causing exception or error. and please let us know the whole exception details too.

Regards,
VSS
Hi,

Try this suggestion...I read it from google search. Put the line 10 code at line 4.

Regards,
VSS
Avatar of seashell1

ASKER

Sorry, I didn't really explain properly, but the error happens at line 35.
Compressing works from lines 3-9 and the compressedData does contain 2 bytes of information with a length of 233. I think ms2 successfully contains the compressedData and it's only when I put it into the InflaterInputStream s2 that it's empty.
I have tried you're suggestion to move line 10 to line 4, but this still did not work
Hi,

The problem actually lies at line number 14 which is reflected later on line number 35. Actually as I have learn till far by searching on google and trying the same code myself the InflaterInputStream constructor is not able to put values in its object. Line 14 says that the object is having length of zero means it is not having any values in it.

By the way I am trying for it.  If you get solution in between please let me know too. Searching online goes worst with no worthy solution.

Regards,
VSS
Hi,

I am still working on your code. And here is the report of something which I tried to find out more about it:

// Returns 0 once the end of the stream (EOF) has been reached. Otherwise returns 1.
            int EOF = s2.Available;
            bool timeout = s2.CanTimeout;
            lblMessage.Text = "EOF: " + EOF.ToString() + "||" + "TimeOut: " + timeout.ToString();

// The output I got is as EOF: 1|| TimeOut: False

So I concluded that it seems that the InflaterInputStream s2 is not stopping itself from reading the MemoryStream we are supply.

Regards,
V.S.Saini
ASKER CERTIFIED SOLUTION
Avatar of seashell1
seashell1
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial