java

时间:2016-05-24 04:55:21

标签: java zlib

我有一个java代码,它解压缩(zlib压缩)在套接字连接中收到的字节...系统能够在服务器PC为32位时解压缩并使用zlib.dll但是相同的代码在服务器时抛出DataFormatException改为64位处理器&操作系统 - 以及服务器系统使用zlib64.dll进行压缩。

(这就是服务器端系统公司对我们说的话)

根据服务器端系统 - 它将检查服务器处理器&操作系统是32位或64位,然后相应地压缩数据包并发送它。 我的代码:

 Inflater inflater = new Inflater();
              inflater.setInput(message);
              ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compHeader.MsgLen);
              byte[] buffer = new byte[1024];  
              while (!inflater.finished()) {  
            int count = 0;  
                  try {
                      count = inflater.inflate(buffer); // ERROR LINE - THROWS DATAFORMATEXCEPTION of java.util.zip java package
                  } catch (DataFormatException ex) {
                      Logger.getLogger(BroadCastManager.class.getName()).log(Level.SEVERE, null, ex);
                  }
                outputStream.write(buffer, 0, count);  
               }  
              try {  
                  outputStream.close();
              } catch (IOException ex) {
                  Logger.getLogger(BroadCastManager.class.getName()).log(Level.SEVERE, null, ex);
              }

你能提供一个可以同时处理的代码 - 32位和64位吗?或者说这个问题可能是什么原因?

1 个答案:

答案 0 :(得分:1)

数据不是用zlib压缩而是用gzip压缩的吗?

以下面的例子为例

echo "hello zlib" > /tmp/in
gzip /tmp/in
# will result in /tmp/in.gz

如果您尝试像

那样解压缩它
byte[] buffer = new byte[10000];
byte[] message = Files.readAllBytes(Paths.get("/tmp/in.gz"));
Inflater inflater = new Inflater();
inflater.setInput(message);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.out.println("read input as zlib");
try {
    outputStream.write(buffer, 0, inflater.inflate(buffer));
    Files.write(Paths.get("/tmp/out.txt"), outputStream.toByteArray(),
        StandardOpenOption.CREATE);
} catch (DataFormatException ex) {
    System.out.println("ex = " + ex);
}

...抛出异常java.util.zip.DataFormatException: incorrect header check

如果你像gzip一样解压缩

byte[] buffer = new byte[10000];
Path gzipPath = Paths.get("/tmp/in.gz");
System.out.println("read input as gzip");
try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(gzipPath))){
    int read = gzip.read(buffer);
    buffer = Arrays.copyOfRange(buffer, 0, read);
    Files.write(Paths.get("/tmp/gzip.inflated"), buffer,
        StandardOpenOption.CREATE);
}

...将创建文件/tmp/gzip.inflated