java zip 为二进制格式然后解压

标签 java ems

我有一个任务

  1. 从本地读取一个zip文件到二进制消息中

  2. 通过 EMS 将二进制消息作为字符串传输(由 java API 完成)

  3. 接收传输的二进制消息作为字符串(由 java API 完成)

  4. 解压二进制消息然后打印出来

我遇到的问题是在解压缩消息时出现 DataFormatException。

我不知道哪一部分出了问题。

我用它来将文件读入二进制消息:

static String readFile_Stream(String fileName) throws IOException {
    File file = new File(fileName);
    byte[] fileData = new byte[(int) file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(fileData);
    String content = "";
    System.out.print("Sent message: ");
    for(byte b : fileData)
    {
        System.out.print(getBits(b));
        content += getBits(b);
    }
    in.close();
    return content;
}

static String getBits(byte b)
{
    String result = "";
    for(int i = 0; i < 8; i++)
        result = ((b & (1 << i)) == 0 ? "0" : "1") + result;
    return result;
}

我用它来解压缩消息:

private static byte[] toByteArray(String input)
{       
    byte[] byteArray = new byte[input.length()/8];
    for (int i=0;i<input.length()/8;i++)
    {
        String read_data = input.substring(i*8, i*8+8); 
        short a = Short.parseShort(read_data, 2);
        byteArray[i] = (byte) a;                    
    }       
    return byteArray;       
}

public static byte[] unzipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;

    Inflater oInflate = new Inflater(false);
    oInflate.setInput(file);

    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (! oInflate.finished() ){
            byte[] byRead = new byte[4 * 1024];
            int iBytesRead = oInflate.inflate(byRead);
            if (iBytesRead == byRead.length){
                oZipStream.write(byRead);
            }
            else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        byReturn = oZipStream.toByteArray();
    }
    catch (DataFormatException ex){
        throw new IOException("Attempting to unzip file that is not zipped.");
    }
    finally {
        oZipStream.close();
    }
    return byReturn;
}

我得到的消息是

java.io.IOException: Attempting to unzip file that is not zipped.
at com.sourcefreak.example.test.TibcoEMSQueueReceiver.unzipByteArray(TibcoEMSQueueReceiver.java:144)
at com.sourcefreak.example.test.TibcoEMSQueueReceiver.main(TibcoEMSQueueReceiver.java:54)

经检查,二进制消息在传输后没有损坏。 请帮助找出问题。

最佳答案

您是否尝试过使用 InflaterInputStream?根据我的经验,直接使用 Inflater 是相当棘手的。您可以使用它开始:

public static byte[] unzipByteArray(byte[] file) throws IOException {
  InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(file));
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  byte[] buffer = new byte[512];
  int length = 0;
  while ((length = iis.read(buffer, 0, buffer.length) != 0) {
    baos.write(buffer, 0, length);
  }

  iis.close();
  baos.close();

  return baos.toByteArray();
}

关于java zip 为二进制格式然后解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24324391/

相关文章:

c# - 当有多个连接可用时,无法处理连接问题(数据库或外部系统)

css - Em 字体大小在我的 h1 顶部增加了边距?

spring-boot - 带 Spring Boot 的 Tibco jms

Java string.replaceAll() 无法将\R 识别为字符类的一部分

java - 如何在 Eclipse Neon 中轻松地进行 Git 提交

java - 如何将字符串和图像数据从一个 Activity 传递到 Android 中的另一个 Activity?

java - CXF 消息的上下文位于拦截器之外

java - 如何在 Java 中逐行读取文本文件并分离每一行的内容?

c# - .Net 中的 JMS 等价物

tibco - 在 EMS 中运行脚本