如何从GZIPed数据的InputStream中获取解压缩数据的InputStream? [英] How do I get the InputStream of decompressed data from an InputStream of GZIPed data?

查看:125
本文介绍了如何从GZIPed数据的InputStream中获取解压缩数据的InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用一个返回gzip压缩文件的服务。我将数据作为一个InputStream(由 javax.activation.DataHandler.getInputStream(); 提供)来自响应。

I call a service which returns a gzipped file. I have the data as an InputStream (courtesy of javax.activation.DataHandler.getInputStream();) from the response.

我想做的是,在不向磁盘写任何内容的情况下,在归档文件中获取解压缩数据的InputStream。在这种情况下,压缩文件是一个xml文档,我试图使用 javax.xml.bind.Unmarshaller 解组,它接受一个I​​nputStream。

What I would like to do is, without writing anything to disk, get an InputStream of the decompressed data in the file that is in the archive. The compressed file in this case is an xml document that I am trying to unmarshal using javax.xml.bind.Unmarshaller which takes an InputStream.

我正在尝试将InputStream写入OutputStream(解压缩数据),然后我需要将其写回InputStream。它还没有工作,所以我想我会看看是否有更好的(我希望如此)方法。

I'm currently trying to write the InputStream to an OutputStream (decompressing the data) and then I'll need to write it back to an InputStream. It's not working yet so I thought I would see if there was a better (I would hope so) approach.

我可以将初始InputStream写入磁盘并获取gz文件,然后读取该文件,从中获取压缩文件并从那里开始,但我宁愿将其全部保存在内存中。

I can write the initial InputStream to disk and get a gz file, and then read that file, get the compressed file out of it and go from there but I'd rather keep it all in memory is possible.

更新1:这是我当前的(不工作 - 得到不是GZIP格式的例外):

Update 1: Here is my current (not working - get a "Not in GZIP format" exception):

    ByteArrayInputStream xmlInput = null;
    try {
        InputStream in = dh.getInputStream(); //dh is a javax.activation.DataHandler
        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int bytes_read = 0;
        byte[] dataBuf = new byte[4096];
        while ((bytes_read = bis.read(dataBuf)) != -1) {
            bo.write(dataBuf, 0, bytes_read);
        }
        ByteArrayInputStream bin = new ByteArrayInputStream(bo.toByteArray());
        GZIPInputStream gzipInput = new GZIPInputStream(bin);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        dataBuf = new byte[4096];;
        bytes_read = 0;
        while ((bytes_read = gzipInput.read(dataBuf)) > 0) {
            out.write(dataBuf, 0, bytes_read);
        }
        xmlInput = new ByteArrayInputStream(out.toByteArray());

如果不是写入ByteArrayOutputStream而是第一次写入FileOutputStream我得到一个压缩文件(我可以手动打开以获取xml文件)和服务(eBay)说它应该是一个gzip文件所以我不知道为什么我会收到Not in GZIP format错误。

If instead of writing to a ByteArrayOutputStream I write to a FileOutputStream the first time around I get a compressed file (which I can manually open to get the xml file within) and the service (eBay) says it should be a gzip file so I'm not sure why I get a "Not in GZIP format" error.

更新2:我尝试了一些不同的东西 - 同样的错误(不是GZIP格式)。哇,我只是想用一个分号结束那个括号。无论如何,这是我的第二次尝试,但仍然不起作用:

Update 2: I tried something a little different - same error ("Not in GZIP format"). Wow, I just tried to end that parenthesis with a semi-colon. Anyways, here is my second attempt, which still does not work:

    ByteArrayInputStream xmlInput = null;
    try {
        GZIPInputStream gzipInput = new GZIPInputStream(dh.getInputStream());
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int bytes_read = 0;
        byte[] dataBuf = new byte[4096];
        while ((bytes_read = gzipInput.read(dataBuf)) != -1) {
            bo.write(dataBuf, 0, bytes_read);
        }
        xmlInput = new ByteArrayInputStream(bo.toByteArray());


推荐答案

以下代码应该有效。请记住,您必须正确处理异常。

The following code should work. Keep in mind you'll have to handle exceptions properly.

OutputStream out = null;
InputStream in = null;
try {
   out = /* some output stream */;
   in = new java.util.GZIPInputStream(/*some stream*/);
   byte[] buffer = new byte[4096];
   int c = 0;
   while (( c = in.read(buffer, 0, 4096)) > 0) {
      out.write(buffer, 0, c);
   }
} finally {
   if (in != null) {
      in.close();
   }
   if (out != null) {
      out.close();
   }
}

这篇关于如何从GZIPed数据的InputStream中获取解压缩数据的InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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