从C#中的压缩字符串中解压缩java中的字符串

我正在寻找正确的解决方案来解压缩来自c#code的java中的字符串。我尝试使用java中的许多技术(gzip,inflatter等)。但是没有得到解决方案。我在尝试时遇到了一些错误从c#代码压缩字符串解压缩java中的字符串。

压缩字符串的我的C#代码是,

public static string CompressString(string text) { byte[] byteArray = Encoding.GetEncoding(1252).GetBytes(text);// Encoding.ASCII.GetBytes(text); using (var ms = new MemoryStream()) { // Compress the text using (var ds = new DeflateStream(ms, CompressionMode.Compress)) { ds.Write(byteArray, 0, byteArray.Length); } return Convert.ToBase64String(ms.ToArray()); } } 

并在java中解压缩字符串,

 private static void compressAndDecompress(){ try { // Encode a String into bytes String string = "xxxxxxSAMPLECOMPRESSEDSTRINGxxxxxxxxxx"; // // Compress the bytes byte[] decoded = Base64.decodeBase64(string.getBytes()); byte[] output = new byte[4096]; // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(decoded); int resultLength = decompresser.inflate(output); decompresser.end(); // Decode the bytes into a 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(); } } 

运行上面的代码时出现此exception:

 java.util.zip.DataFormatException: incorrect header check 

请给我一些java中的示例代码来解压缩字符串java.Thanks

我压缩的C#代码是

  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代码是

 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 is = 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 

这段代码对我来说非常好。

有完全相同的问题。 可以解决它

 byte[] compressed = Base64Utils.decodeFromString("mybase64encodedandwithc#zippedcrap"); Inflater decompresser = new Inflater(true); decompresser.setInput(compressed); byte[] result = new byte[4096]; decompresser.inflate(result); decompresser.end(); System.out.printf(new String(result)); 

在实例化充气机时,boolen参数会产生魔力

BW休伯特