Solved

SharpZipLib Inflate caused Unexpected Eof C#

Posted on 2010-08-19
6
2,762 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:seashell1
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
Hi,

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

Regards,
VSS
0
 

Author Comment

by:seashell1
Comment Utility
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
0
 

Accepted Solution

by:
seashell1 earned 0 total points
Comment Utility
I couldn't get this working so I've abandoned this method and found someone else's compression and decompression code instead, which works.  I've attached the code that I am using now for anyone else who wants to use it.  
public MemoryStream CreateToZipMemoryStream(MemoryStream memStreamIn, string filename)

        {

            MemoryStream memStreamOut = new MemoryStream();

            using (ZipOutputStream s = new

                     ZipOutputStream(memStreamOut))

            {

                s.SetLevel(9); // 0-9, 9 being the highest compression



                byte[] buffer = new byte[4096];

                ZipEntry entry = new ZipEntry(filename);

                entry.DateTime = DateTime.Now;

                s.PutNextEntry(entry);

                int sourceBytes;

                do

                {

                    sourceBytes = memStreamIn.Read(buffer, 0,buffer.Length);

                    s.Write(buffer, 0, sourceBytes);

                } while (sourceBytes > 0);



                s.Finish();

                s.Close();

            }

            return memStreamOut;

        }



        public MemoryStream CreateFromZipMemoryStream(byte[] compressedData, string filename, int origFileLength)

        {

            MemoryStream output = new MemoryStream();

            ZipFile zf = null;

            try

            {

                MemoryStream ms = new MemoryStream(compressedData);

                zf = new ZipFile(ms);

                ZipEntry zipEntry = zf.GetEntry(filename);

                String entryFileName = zipEntry.Name;

                               

                Stream zipStream = zf.GetInputStream(zipEntry);

                byte[] buffer = ReadFully(zipStream, origFileLength);                

                output = new MemoryStream(buffer);                     

            }

            finally

            {

                if (zf != null)

                {

                    zf.IsStreamOwner = true; // Makes close also shut the underlying stream

                    zf.Close(); // Ensure we release resources

                }

            }



            return output;

        }





        private byte[] CompressFile(byte[] bytesIn)

        {

            MemoryStream ms = new MemoryStream(bytesIn);

            MemoryStream outputZip = CreateToZipMemoryStream(ms, "temp");

            

            // Use the newly created memory stream for the compressed data.            

            byte[] compressedData = (byte[])outputZip.ToArray();





            MemoryStream ms2 = CreateFromZipMemoryStream(compressedData, "temp", bytesIn.Length);

            byte[] decompressedBuffer = (byte[])ms2.ToArray();

            if (!CompareData(bytesIn, bytesIn.Length, decompressedBuffer, decompressedBuffer.Length))

            {

                throw new Exception("Error when compressing the file");

            }                        



            return compressedData;

        }





        public byte[] ReadFully(Stream stream, int initialLength)

        {

            // If we've been passed an unhelpful initial length, just

            // use 32K.

            if (initialLength < 1)

            {

                initialLength = 32768;

            }



            byte[] buffer = new byte[initialLength];

            int read = 0;



            int chunk;

            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)

            {

                read += chunk;



                // If we've reached the end of our buffer, check to see if there's

                // any more information

                if (read == buffer.Length)

                {

                    int nextByte = stream.ReadByte();



                    // End of stream? If so, we're done

                    if (nextByte == -1)

                    {

                        return buffer;

                    }



                    // Nope. Resize the buffer, put in the byte we've just

                    // read, and continue

                    byte[] newBuffer = new byte[buffer.Length * 2];

                    Array.Copy(buffer, newBuffer, buffer.Length);

                    newBuffer[read] = (byte)nextByte;

                    buffer = newBuffer;

                    read++;

                }

            }

            // Buffer is now too big. Shrink it.

            byte[] ret = new byte[read];

            Array.Copy(buffer, ret, read);

            return ret;

        }

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now