Decompress string in java from compressed string in C# -


i searching correct solution decompress string in java coming c# code.i tried myself lot of techniques in java like(gzip,inflatter etc.).but didn't solution.i got error while trying decompress string in java compressed string c# code.

my c# code compress string is,

public static string compressstring(string text) {     byte[] bytearray = encoding.getencoding(1252).getbytes(text);//  encoding.ascii.getbytes(text);     using (var ms = new memorystream())    {     // compress text     using (var ds = new deflatestream(ms, compressionmode.compress))     {      ds.write(bytearray, 0, bytearray.length);     }      return convert.tobase64string(ms.toarray());    }   } 

and decompress string in java using,

private static void compressanddecompress(){     try {         // encode string bytes         string string = "xxxxxxsamplecompressedstringxxxxxxxxxx";         // // compress bytes         byte[] decoded = base64.decodebase64(string.getbytes());         byte[] output = new byte[4096];         // decompress bytes         inflater decompresser = new inflater();         decompresser.setinput(decoded);          int resultlength = decompresser.inflate(output);         decompresser.end();          // decode bytes string         string outputstring = new string(output, 0, resultlength, "utf-8");          system.out.println(outputstring);     } catch(java.io.unsupportedencodingexception ex) {             ex.printstacktrace();     } catch (java.util.zip.dataformatexception ex) {         ex.printstacktrace();     } } 

i exception when running above code:

java.util.zip.dataformatexception: incorrect header check 

kindly give me sample code in java decompress string java.thanks

my c# code compress

 private string compress(string text)     {         byte[] buffer = encoding.utf8.getbytes(text);         memorystream ms = new memorystream();         using (gzipstream zip = new gzipstream(ms, compressionmode.compress, true))         {             zip.write(buffer, 0, buffer.length);         }          ms.position = 0;         memorystream outstream = new memorystream();          byte[] compressed = new byte[ms.length];         ms.read(compressed, 0, compressed.length);          byte[] gzbuffer = new byte[compressed.length + 4];         system.buffer.blockcopy(compressed, 0, gzbuffer, 4, compressed.length);         system.buffer.blockcopy(bitconverter.getbytes(buffer.length), 0, gzbuffer, 0, 4);         return convert.tobase64string(gzbuffer);     } 

java code decompress text

private string decompress(string compressedtext) {      byte[] compressed = compressedtext.getbytes("utf8");     compressed = org.apache.commons.codec.binary.base64.decodebase64(compressed);     byte[] buffer=new byte[compressed.length-4];     buffer = copyfordecompression(compressed,buffer, 4, 0);     final int buffer_size = 32;     bytearrayinputstream = new bytearrayinputstream(buffer);     gzipinputstream gis = new gzipinputstream(is, buffer_size);     stringbuilder string = new stringbuilder();     byte[] data = new byte[buffer_size];     int bytesread;     while ((bytesread = gis.read(data)) != -1)      {         string.append(new string(data, 0, bytesread));     }     gis.close();     is.close();     return string.tostring(); } private  byte[] copyfordecompression(byte[] b1,byte[] b2,int srcoffset,int dstoffset) {            for(int i=0;i<b2.length && i<b1.length;i++)     {         b2[i]=b1[i+4];     }     return b2; } 

this code works fine me.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -