Problem with C# Decompression -


have data in sybase image type column want use in c# app. data has been compressed java using java.util.zip package. wanted test decompress data in c#. wrote test app pulls out of database:

byte[] bytes = (byte[])reader.getvalue(0);   

this gives me compressed byte[] of 2479 length.
pass seemingly standard c# decompression method:

public static byte[] decompress(byte[] gzbuffer) {     memorystream ms = new memorystream();     int msglength = bitconverter.toint32(gzbuffer, 0);     ms.write(gzbuffer, 4, gzbuffer.length - 4);     byte[] buffer = new byte[msglength];      ms.position = 0;     gzipstream zip = new gzipstream(ms, compressionmode.decompress);     zip.read(buffer, 0, buffer.length);      return buffer; }   

the value msglength 1503501432 seems way out of range. original document should in range of 5k -50k. anyway when use value create "buffer" not surprisingly outofmemoryexception. happening? jim

the java compress method follows:

public byte[] compress(byte[] bytes) throws exception {     byte[] results = new byte[bytes.length];     deflater deflator = new deflater();     deflater.setinput(bytes);     deflater.finish();     int len = deflater.deflate(results);     byte[] out = new byte[len];     for(int i=0; i<len; i++) {         out[i] = results[i];     }     return(out); }   

as cant see java code, can guess compressing data zip file stream. therefore fail if trying decompress stream gzip decompression in c#. either change java code gzip compression (example here @ bottom of page), or decompress zip file stream in c# appropriate library (e.g. sharpziplib).

update

ok now, see using deflate compression in java. so, have use same algorithm in c#: system.io.compression.deflatestream

public static byte[] decompress(byte[] buffer) {     using (memorystream ms = new memorystream(buffer))     using (stream zipstream = new deflatestream(ms,                            compressionmode.decompress, true))     {         int initialbufferlength = buffer.length * 2;          byte[] buffer = new byte[initialbufferlength];         bool finishedexactly = false;         int read = 0;         int chunk;          while (!finishedexactly &&                (chunk = zipstream.read(buffer, read, buffer.length - read)) > 0)         {             read += chunk;              if (read == buffer.length)             {                 int nextbyte = zipstream.readbyte();                  // end of stream?                 if (nextbyte == -1)                 {                     finishedexactly = true;                 }                 else                 {                     byte[] newbuffer = new byte[buffer.length * 2];                     array.copy(buffer, newbuffer, buffer.length);                     newbuffer[read] = (byte)nextbyte;                     buffer = newbuffer;                     read++;                 }             }         }         if (!finishedexactly)         {             byte[] final = new byte[read];             array.copy(buffer, final, read);             buffer = final;         }     }      return buffer; }   

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -