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;
}