如何在Java中动态提取.gz文件? [英] How to extract .gz file Dynamically in Java?

查看:135
本文介绍了如何在Java中动态提取.gz文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.newegg.com/Siteindex_USA.xml 的许多网址中提供.gz文件,如下所示:

In http://www.newegg.com/Siteindex_USA.xml lots of urls of .gz-files are given, like this:

<loc>
    http://www.newegg.com//Sitemap/USA/newegg_sitemap_product01.xml.gz
</loc>

我想动态提取这些。我不想在本地存储它们,我只想提取它们并将包含的数据存储在数据库中。

I want to extract these dynamically. I don't want to store them locally, I just want to extract them and store the contained data in a database.

修改:

我正在异常

private void processGzip(URL url, byte[] response) throws MalformedURLException,
            IOException, UnknownFormatException {

        if (DEBUG) System.out.println("Processing gzip");

        InputStream is = new ByteArrayInputStream(response);

        // Remove .gz ending
        String xmlUrl = url.toString().replaceFirst("\\.gz$", "");

        if (DEBUG) System.out.println("XML url = " + xmlUrl);

        InputStream decompressed = new GZIPInputStream(is);
        InputSource in = new InputSource(decompressed);
        in.setSystemId(xmlUrl);         
        processXml(url, in);
        decompressed.close();
    }


推荐答案

只需将输入流包装在 GZIPInputStream ,它会在您阅读时解压缩数据。

Simply wrap the input stream in GZIPInputStream, and it'll decompress the data as you're reading it.

这篇关于如何在Java中动态提取.gz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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