C# 从C中的压缩字符串解压缩java中的字符串#
我一直在寻找正确的解决方案来解压来自c代码的java字符串。我尝试了很多java技术,如gzip、Inflater等。但没有找到解决方案。我在尝试从c代码的压缩字符串解压java字符串时出错 我压缩字符串的C代码是C# 从C中的压缩字符串解压缩java中的字符串#,c#,java,gzip,inflate,compression,C#,Java,Gzip,Inflate,Compression,我一直在寻找正确的解决方案来解压来自c代码的java字符串。我尝试了很多java技术,如gzip、Inflater等。但没有找到解决方案。我在尝试从c代码的压缩字符串解压java字符串时出错 我压缩字符串的C代码是 public static string CompressString(string text) { byte[] byteArray = Encoding.GetEncoding(1252).GetBytes(text);// Encoding.ASCII.GetByte
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());
}
}
并使用
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();
}
}
我在运行上述代码时遇到此异常:
java.util.zip.DataFormatException: incorrect header check
请给我用java解压字符串java的示例代码。谢谢我要压缩的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<b2.length && i<b1.length;i++)
{
b2[i]=b1[i+4];
}
return b2;
}
私有字符串解压缩(字符串压缩文本)
{
byte[]compressed=compressedText.getBytes(“UTF8”);
compressed=org.apache.commons.codec.binary.Base64.decodeBase64(压缩);
byte[]buffer=新字节[compressed.length-4];
缓冲区=copyForDecompression(压缩,缓冲区,4,0);
最终整数缓冲区大小=32;
ByteArrayInputStream is=新的ByteArrayInputStream(缓冲区);
GZIPInputStream gis=新的GZIPInputStream(即缓冲区大小);
StringBuilder字符串=新的StringBuilder();
字节[]数据=新字节[缓冲区大小];
int字节读取;
而((bytesRead=gis.read(data))!=-1)
{
append(新字符串(数据,0,字节读取));
}
gis.close();
is.close();
返回字符串.toString();
}
专用字节[]copyForDecompression(字节[]b1,字节[]b2,int srcfostate,int dstfostate)
{
对于(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休伯特为深受喜爱的谷歌人
正如@dbw所提到的
据《华盛顿邮报》报道,
C中使用的默认平减指数#
这就是为什么用户更喜欢Gzip和Ziplib
或者其他一些压缩技术
一个相对简单的方法是使用GZip。
对于公认的答案,一个问题是,在这种方法中,您应该自己将数据大小附加到压缩字符串中,更重要的是,根据我自己在生产应用程序中的经验,当字符串达到~2000个字符时,就会出现问题
错误在System.io.Compression.gzip流中
在c#中使用任何方法,问题都会消失,一切都会像以下代码片段一样简单:
爪哇:
和c#:
您可能还会发现代码请向我们显示您收到的错误消息。谢谢@Deepak Bhatia,我尝试使用您的java代码进行解压缩,我没有收到任何异常,但没有给出结果,它在这行代码GZIPInputStream gis=new GZIPInputStream(即缓冲区大小)后返回。我没有权限更改C#中的压缩方法。所以我应该像C#中那样。但是在java中,解压缩是不起作用的。请帮助meSorry,我没有打印异常。现在它抛出java.util.zip.ZipException:不是GZIP格式根据帖子,C#中使用的默认平减器没有任何java等效项,这就是用户喜欢G的原因zip、Ziplib或其他一些zip技术。但是如果你找到了一个答案,因为它将帮助其他用户。我面临着类似的问题-java.util.zip.ZipException:不是GZIP格式。你能找到解决这个问题的方法吗???@Ashu我已经花了很长时间研究它,也许你能得到上面发布的代码,看看它是否有效rks。。。。。。
import android.util.Base64;
import com.google.android.gms.common.util.IOUtils;
import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class CompressionHelper {
@Nullable
public static String compress(@Nullable String data) {
if(data == null || data.length() == 0)
return null;
try {
// Create an output stream, and a gzip stream to wrap over.
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
GZIPOutputStream gzip = new GZIPOutputStream(bos);
// Compress the input string
gzip.write(data.getBytes());
gzip.close();
byte[] compressed;
// Convert to base64
compressed = Base64.encode(bos.toByteArray(),Base64.NO_WRAP);
bos.close();
// return the newly created string
return new String(compressed);
} catch(IOException e) {
return null;
}
}
@Nullable
public static String decompress(@Nullable String compressedText) {
if(compressedText == null || compressedText.length() == 0)
return null;
try {
// get the bytes for the compressed string
byte[] compressed = compressedText.getBytes("UTF-8");
// convert the bytes from base64 to normal string
compressed = Base64.decode(compressed, Base64.NO_WRAP);
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(bis);
byte[] bytes = IOUtils.toByteArray(gis);
return new String(bytes, "UTF-8");
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
using ICSharpCode.SharpZipLib.GZip; //PM> Install-Package SharpZipLib
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeneralTools
{
public static class CompressionTools
{
public static string CompressString(string text)
{
if (string.IsNullOrEmpty(text))
return null;
byte[] buffer = Encoding.UTF8.GetBytes(text);
using (var compressedStream = new MemoryStream())
{
GZip.Compress(new MemoryStream(buffer), compressedStream, false);
byte[] compressedData = compressedStream.ToArray();
return Convert.ToBase64String(compressedData);
}
}
public static string DecompressString(string compressedText)
{
if (string.IsNullOrEmpty(compressedText))
return null;
byte[] gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
using (var compressedStream = new MemoryStream(gZipBuffer))
{
var decompressedStream = new MemoryStream();
GZip.Decompress(compressedStream, decompressedStream, false);
return Encoding.UTF8.GetString(decompressedStream.ToArray()).Trim();
}
}
}
}
}