java压缩到二进制格式,然后解压缩

java压缩到二进制格式,然后解压缩,java,ems,Java,Ems,我有一个任务 将zip文件从本地读入二进制消息 以字符串形式通过EMS传输二进制消息(由java API完成) 以字符串形式接收传输的二进制消息(由java API完成) 解压缩二进制消息,然后将其打印出来 我面临的问题是解压缩消息时出现DataFormatException 我不知道哪一部分出了问题 我使用此命令将文件读入二进制消息: static String readFile_Stream(String fileName) throws IOException { File fil

我有一个任务

  • 将zip文件从本地读入二进制消息

  • 以字符串形式通过EMS传输二进制消息(由java API完成)

  • 以字符串形式接收传输的二进制消息(由java API完成)

  • 解压缩二进制消息,然后将其打印出来

  • 我面临的问题是解压缩消息时出现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;
    }
    
    检查后,二进制消息在传输后不会损坏。
    请帮助解决问题。

    您是否尝试过使用InflaterInputStream?根据我的经验,直接使用充气机是相当棘手的。您可以使用此选项开始:

    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();
    }
    

    我终于解决了这个问题

    问题是原始文件是一个.zip文件,所以在进一步处理之前,我应该使用zipInputStream解压该文件

    public static byte[] unzipByteArray(byte[] file) throws IOException {
    
        // create a buffer to improve copy performance later.
        byte[] buffer = new byte[2048];
        byte[] content ;
    
        // open the zip file stream
        InputStream theFile = new ByteArrayInputStream(file);
        ZipInputStream stream = new ZipInputStream(theFile);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
    
        try
        {
    
            ZipEntry entry;
            while((entry = stream.getNextEntry())!=null)
            {
                //String s = String.format("Entry: %s len %d added %TD", entry.getName(), entry.getSize(), new Date(entry.getTime()));
                //System.out.println(s);
    
                // Once we get the entry from the stream, the stream is
                // positioned read to read the raw data, and we keep
                // reading until read returns 0 or less.
                //String outpath = outdir + "/" + entry.getName();
    
                try
                {
                    //output = new FileOutputStream(outpath);
    
                    int len = 0;
                    while ((len = stream.read(buffer)) > 0)
                    {
                        output.write(buffer, 0, len);
                    }
                }
                finally
                {
                    // we must always close the output file
                    if(output!=null) output.close();                                            
                }
            }            
        }
        finally
        {
            // we must always close the zip file.
            stream.close();            
        }
        content = output.toByteArray();
    
        return content;     
    
    }
    

    此代码适用于内部包含单个文件的zip文件。

    尝试过。但是问题变成了java.util.zip.ZipException:未知的压缩方法
    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();
    }
    
    public static byte[] unzipByteArray(byte[] file) throws IOException {
    
        // create a buffer to improve copy performance later.
        byte[] buffer = new byte[2048];
        byte[] content ;
    
        // open the zip file stream
        InputStream theFile = new ByteArrayInputStream(file);
        ZipInputStream stream = new ZipInputStream(theFile);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
    
        try
        {
    
            ZipEntry entry;
            while((entry = stream.getNextEntry())!=null)
            {
                //String s = String.format("Entry: %s len %d added %TD", entry.getName(), entry.getSize(), new Date(entry.getTime()));
                //System.out.println(s);
    
                // Once we get the entry from the stream, the stream is
                // positioned read to read the raw data, and we keep
                // reading until read returns 0 or less.
                //String outpath = outdir + "/" + entry.getName();
    
                try
                {
                    //output = new FileOutputStream(outpath);
    
                    int len = 0;
                    while ((len = stream.read(buffer)) > 0)
                    {
                        output.write(buffer, 0, len);
                    }
                }
                finally
                {
                    // we must always close the output file
                    if(output!=null) output.close();                                            
                }
            }            
        }
        finally
        {
            // we must always close the zip file.
            stream.close();            
        }
        content = output.toByteArray();
    
        return content;     
    
    }