【问题标题】:read gzip file to byte[] all at once一次将 gzip 文件读取到 byte[]
【发布时间】:2013-04-16 13:18:01
【问题描述】:

在代码中

GZIPInputStream gzis= new GZIPInputStream(bais);
byte[] bBodyUnzipped= new byte[10240];
gzis.read(bBodyUnzipped);

,如何通过知道文件解压缩长度来优化磁盘空间使用而不创建大字节[]?

根据this answer没有这种方法。

想法是用这个byte[]来调用

CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
String sBodyUnzipped= decoder.decode(ByteBuffer.wrap(bBodyUnzipped)).toString();

出于这个原因,我需要一个包含所有内容且没有多余零的字节[]。

【问题讨论】:

    标签: java byte gzipinputstream


    【解决方案1】:

    读入一个较小的byte 数组。

    【讨论】:

    • 我通常使用 1024 或 2048 的缓冲区
    【解决方案2】:

    你不能只使用 Apache commons IOUtils 吗?

    【讨论】:

    • 我可以使用哪种方法?谢谢。
    • toByteArray(InputStream 输入)
    【解决方案3】:

    如果 zip 包含二进制信息,您可以逐字节处理它

        InputStream is = new BufferedInputStream(new GZIPInputStream(
                new FileInputStream("zip")));
        for (int b; (b = is.read()) != -1;) {
            // process byte
        }
    

    如果 zip 是文本,则逐行处理,例如

        Scanner sc = new Scanner(new GZIPInputStream(new FileInputStream("zip")));
        while(sc.hasNextLine()) {
            String line = sc.nextLine();
            // process line
        }
    

    【讨论】:

      【解决方案4】:

      我认为你想要这个:

          public void gzip(String path) {
                  GZIPInputStream in = null;
                  try {
                      in = new GZIPInputStream(
                              new FileInputStream(new File(path)));
                      byte[] read = new byte[in.available()];
                      in.read(read);
                      System.out.println(read);
                  }catch (Exception e) {
                      System.out.println(e);
                  }
                  finally {
                      try {
                          in.close();
                      }catch (Exception e) {
                          System.out.println(e);
                      }
                  }
              }
      

      请参阅:http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html 了解更多信息

      【讨论】:

      • 是的,这就是 a 想要的。但是 in.available 方法被 InflaterInputStream (see java doc) 类覆盖,它只返回 0 或 1。
      • 好的。我总是使用这种方法,因为我认为它与用于 fileInputStream 的方法相同。谢谢你的建议。也许使用循环我们可以解决这个问题,因为 in.read 给了我们一些有用的信息。我试试看,如果你已经找到了可以解决的请告诉我
      【解决方案5】:

      我还没有找到一次阅读所有内容的方法。另一种方法是按块读取:

          private static String unzip(GZIPInputStream gzis) {
          CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
          byte[] bBodyUnzipped= new byte[1024];
          String sBodyUnzipped= null;
          int offset= 0;
          int bodyLength= 0;
          do {
              bodyLength= gzis.read(bBodyUnzipped, offset, 1024);
              sBodyUnzipped+= decoder.decode(ByteBuffer.wrap(bBodyUnzipped, 0, bodyLength)).toString();
              offset+= bodyLength;
          } while(bodyLength < 0);
          return sBodyUnzipped;
      }
      

      【讨论】:

        【解决方案6】:
        public  byte[] readGZFile(File file) {
        
            byte[] fileData = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(file));
                int bufsize=1024;
                byte [] buf=new byte[bufsize];
                int readbytes=0;
                readbytes=in.read(buf);
                while(readbytes!=-1){
                    baos.write(buf, 0,readbytes);
                    readbytes=in.read(buf);
                }
                baos.flush();
                return baos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return fileData;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-21
          • 2015-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多