通过网络将压缩后的数据从C#发送到Java [英] Sending gzipped data over a network from C# to Java

查看:122
本文介绍了通过网络将压缩后的数据从C#发送到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它将从PC上的数据源(例如Excel或Access)收集的数据发送到Android平板电脑上的接收应用程序.我处于测试阶段,即在发送数据之前先对其进行压缩,然后在接收到数据之后对其进行解压缩.我正在为此使用GZIP,在C#端使用DotNetZip,在Java端使用内置的GZIPInputStream类.

I have an application that sends data gathered from a data source on my PC (say, Excel or Access) to a receiving app on an Android tablet. I am in the testing stage of compressing the data before it is sent and then decompressing it after it is received. I'm using GZIP for this, with DotNetZip on the C# side and the built-in GZIPInputStream class on the Java side.

我在使它正常工作方面遇到问题.当我仅在一侧进行测试(压缩和解压缩以查看数据是否完好无损)时,一切都很好.它可以在C#和Java上运行.但是,当我到达发送和接收数据的地步时,我遇到了这个问题:未知格式(幻数ef1f)".我看过其他SO帖子,对此进行了讨论,但答案似乎无济于事.

I'm having issues getting it to work properly. When I do a test just on one side (compressing and decompressing to see if the data remains intact), everything is fine. It works on both C# and Java. But when I get to the point of the data being sent over and received, I run into this issue: "unknown format (magic number ef1f)." I've seen other SO posts where this has been discussed but the answers don't seem to help.

这是我用来在C#端压缩的代码:

Here is the code I am using to compress on the C# side:

public void compressData() {
    byte[] buffer = Ionic.Zlib.GZipStream.CompressBuffer(this.RawStreamData.ToArray());
    this.RawStreamData = new MemoryStream(buffer);
}

这是我用来在Java端解压缩的代码.由于数据是由BufferedReader读取的,因此我必须首先将char []转换为byte []:

And here is the code I am using to decompress on the Java side. Because the data is read in by a BufferedReader, I have to convert from char[] to byte[] initially:

public NetMessage decompressMsg(NetMessage nMsg) throws IOException {
    ByteArrayOutputStream baOut = new ByteArrayOutputStream();
    OutputStreamWriter osWriter = new OutputStreamWriter(baOut);
    osWriter.write(nMsg.getRawMsg());  //.getRawMsg() returns a char[] of the raw data
    osWriter.close();
    ByteArrayInputStream baIn = new ByteArrayInputStream(baOut.toByteArray());
    GZIPInputStream gzIn = new GZIPInputStream(baIn);
    byte[] buffer = new byte[128];
    int nReadBytes = gzIn.read(buffer);
    String sDecompMsg = new String(buffer);

    while (nReadBytes > -1) {
        buffer = new byte[128];
        nReadBytes = gzInput.read(buffer);
        String sTemp = new String(buffer);
        sDecompMsg += sTemp;
    }

    nMsg.setRawMsg(sDecompMsg.toCharArray());

    return nMsg;
}

当我尝试构造GZIPInputStream时,它将引发异常.我已经知道我从生成的解压缩缓冲区重构消息的方式是错误的(仅从Java方面的测试),但这是我首先要解决的问题! :) 任何帮助,将不胜感激.我怀疑这可能与压缩级别设置有关.在C#中,我使用默认值,但是AFAIK在Java端没有这样的设置. BufferedReader是使用ASCII编码创建的.

It throws the exception when I try to construct the GZIPInputStream. I already know the way I am reconstructing the message from the resulting decompressed buffer is wrong (from testing on the Java side only), but that is a problem I need to reach first! :) Any help would be appreciated. I suspect it might have something to do with the compression level settings; in C# I am using the default, but AFAIK there is no such setting on the Java side. The BufferedReader is created with an ASCII encoding.

推荐答案

由于数据是由BufferedReader读取的,因此我必须首先将char []转换为byte []:

Because the data is read in by a BufferedReader, I have to convert from char[] to byte[] initially:

那是一个巨大问题.您已经压缩了数据.它是任意的二进制数据.您应该绝对不将其视为文本.我不清楚NetMessage是什么,但是基本上您需要访问通过网络发送的原始二进制数据-否则就没有机会对其进行解压缩.

That's a huge problem. You've compressed the data. It's arbitrary binary data. You should absolutely not be treating it as text. It's not clear to me what NetMessage is, but basically you'll need to get access to the raw binary data you're sending over the network - otherwise you've got no chance of decompressing it.

这篇关于通过网络将压缩后的数据从C#发送到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆