如何在Java中动态提取.gz文件?
在http://www.newegg.com/Siteindex_USA.xml中给出了很多.gz文件的URL,如下所示:
http://www.newegg.com//Sitemap/USA/newegg_sitemap_product01.xml.gz
我想动态提取这些。 我不想在本地存储它们,我只想提取它们并将包含的数据存储在数据库中。
修改:
我越来越exception了
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
,它就会在您读取数据时解压缩数据。