SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

inflate - deflate mess up

Last post 01-25-2010 4:01 PM by oz030. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-22-2010 5:14 PM

    • oz030
    • Not Ranked
    • Joined on 01-22-2010
    • Posts 2

    inflate - deflate mess up

    I'am writing on compressing and decompressing SWF files. I searched the forum for solutions but I didn't found something. Maybe you can help me.

    Inflate works:

    public Stream Uncompress( Stream input ){
    
                byte[ content = new byte[ _Length ];
                input.Seek( 0, SeekOrigin.Begin );
                input.Read( content, 0, 8 );
    
                content[ 0 ] = ( byte )0x46;
    
                byte[ zipData = new byte[ input.Length ];
                input.Read( zipData, 0, ( int )( input.Length - 8 ) );
    
                Inflater zLib = new Inflater();
                zLib.SetInput( zipData );
                if ( zLib.Inflate( content, 8, ( content.Length - 8 ) ) == 0 )
                {
                    Exception e = new Exception( "Invalid compressed content, Inflate() failed" );
                    throw e;
                }
                else
                {
                    return new MemoryStream( content );
                }
            }
    

    Deflating seems to work. But when I try to open the file in Flash i won't be displayed and inflating again does not work with the following exception:

    Unhandled Exception: ICSharpCode.SharpZipLib.SharpZipBaseException: Header checksum illegal
       at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.DecodeHeader()
       at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode()
       at ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Inflate(Byte[ buffer, Int32 offset, Int32 count)

    The code:

    public byte[ Compress( Stream input, int level ){
    
                input.Seek( 0, SeekOrigin.Begin );
    
                byte[ header = new byte[ 8 ];
                byte[ data = new byte[ input.Length - 8 ];
                byte[ compressed = new byte[ input.Length * 2 ];
    
                int bytesOut = 0;
    
                try
                {
                    input.Read( header, 0, 8 );
                    input.Read( data, 0, ( int )( input.Length - 8 ) );
                }
                catch ( Exception e )
                {
                    throw e;
                }
    
                if ( header[ 0 ] == ( byte )0x43 )
                {
                    byte[ temp = new byte[ input.Length ];
                    input.Read( temp, 0, (int)input.Length );
                    return temp;
                }
    
                header[ 0 ] = ( byte )0x43;
    
                Deflater deflater = new Deflater( level, false );
                try
                {
                    deflater.SetInput( data );
                    deflater.Finish();
                    bytesOut = deflater.Deflate( compressed );
                }
                catch ( Exception e )
                {
                    throw e;
                }
    
                byte[ output = new byte[ 8 + bytesOut ];
    
                Array.Copy( header, 0, output, 0, 8 );
                Array.Copy( data, 0, output, 8, bytesOut );
    
                return output;
    }
    
  • 01-22-2010 11:45 PM In reply to

    Re: inflate - deflate mess up

    You might find some useful material here

    http://community.sharpdevelop.net/forums/t/10319.aspx

    and

    http://community.sharpdevelop.net/forums/t/1009.aspx

     

    Hope this helps,
    David

  • 01-25-2010 4:01 PM In reply to

    • oz030
    • Not Ranked
    • Joined on 01-22-2010
    • Posts 2

    Re: inflate - deflate mess up

    Hey guys I'am very happy that I can tell you that it is now working. The bug was not int the algo. I wrote the uncompressed data back into the stream. Shame on me!

     

    Array.Copy( data, 0, output, 8, bytesOut );

     

    Should be:

     

    Array.Copy( compressed, 0, output, 8, bytesOut );

     

    Anyway. If anyone is looking for a way compressing/decompressing SWF file, this works. ;0)

Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.