首页
学习
活动
专区
圈层
工具
发布

Bz2文件
EN

Stack Overflow用户
提问于 2015-08-15 11:32:33
回答 1查看 295关注 0票数 0

我试图从维基百科上获取一些bz2文件,我不在乎它们是保存为bz2还是解压缩,因为我可以在本地解压缩它们。

当我打电话:

代码语言:javascript
复制
public static void getZip(String theUrl, String filename) throws IOException {
    URL gotoUrl = new URL(theUrl);
    try (InputStreamReader isr = new InputStreamReader(new BZip2CompressorInputStream(gotoUrl.openStream())); BufferedReader in = new BufferedReader(isr)) {
        StringBuffer sb = new StringBuffer();
        String inputLine;

        // grab the contents at the URL
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine + "\r\n");
        }
        // write it locally
        Wget.createAFile(filename, sb.toString());
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        throw ioe;
    }
}

我得到了解压缩文件的一部分,不超过+- 883 K。

当我不使用BZip2CompressorInputStream时,比如:

代码语言:javascript
复制
public static void get(String theUrl, String filename) throws IOException {
    try {
        URL gotoUrl = new URL(theUrl);
        InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
        BufferedReader in = new BufferedReader(isr);

        StringBuffer sb = new StringBuffer();
        String inputLine;

        // grab the contents at the URL
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);// + "\r\n");
        }
        // write it locally
        Statics.writeOut(filename, false, sb.toString());
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        throw ioe;
    }
}

我得到了一个文件,其大小与它所设想的大小相同(与KB而不是B相比)。但是还有一条消息,即压缩文件已经损坏,在使用byte []而不是readLine()时也是如此,如下所示:

代码语言:javascript
复制
public static void getBytes(String theUrl, String filename) throws IOException {
    try {
        char [] cc = new char[1024];
        URL gotoUrl = new URL(theUrl);
        InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
        BufferedReader in = new BufferedReader(isr);

        StringBuffer sb = new StringBuffer();
        // grab the contents at the URL
        int n = 0;
        while (-1 != (n = in.read(cc))) {
            sb.append(cc);// + "\r\n");
        }
        // write it locally
        Statics.writeOut(filename, false, sb.toString());
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        throw ioe;
    }
}

最后,当我bzip2 inputstreamoutputstream时,我得到一个有效的bzip2文件,但是大小类似于第一个文件,使用:

代码语言:javascript
复制
public static void getWriteForBZ2File(String urlIn, final String filename) throws CompressorException, IOException {
    URL gotoUrl = new URL(urlIn);
    try (final FileOutputStream out = new FileOutputStream(filename);
            final BZip2CompressorOutputStream dataOutputStream = new BZip2CompressorOutputStream(out);
            final BufferedInputStream bis = new BufferedInputStream(gotoUrl.openStream());
            final CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(bis);
            final BufferedReader br2 = new BufferedReader(new InputStreamReader(input))) {
        String line = null;
        while ((line = br2.readLine()) != null) {
            dataOutputStream.write(line.getBytes());
        }
    }
}

那么,如何以bz2格式或解压缩方式获得整个bz2文件呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-15 12:11:08

bz2文件包含字节,而不是字符。你不能把它当作包含字符的读取器来阅读。

由于您只想下载文件并将其保存在本地,所以您所需要的就是

代码语言:javascript
复制
Files.copy(gotoUrl.openStream(), Paths.get(fileName));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32024282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档