JavaWGETBZ2文件

JavaWGETBZ2文件,java,webget,bz2,Java,Webget,Bz2,我试图从维基百科上获取一些bz2文件,我不在乎它们是保存为bz2还是解压,因为我可以在本地解压它们 当我打电话时: public static void getZip(String theUrl, String filename) throws IOException { URL gotoUrl = new URL(theUrl); try (InputStreamReader isr = new InputStreamReader(new BZip2CompressorInpu

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

当我打电话时:

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;
    }
}
我得到解压缩文件的一部分,从未超过+-883K。
当我不使用
BZip2CompressorInputStream
时,例如:

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()
时,如:

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;
    }
}
最后,当我对
inputstream
outputstream
执行bzip2操作时,我得到一个有效的bzip2文件,但大小与第一个文件相同,使用:

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
文件?

bz2文件包含字节,而不是字符。你不能用读卡器读它,就好像它包含字符一样

由于您只需下载文件并将其保存在本地,因此只需

Files.copy(gotoUrl.openStream(), Paths.get(fileName));

不要认为bz2文件包含字符和行。没有。这是一种二进制文件格式。以字节形式读取所有内容,以字节形式写入所有内容:
Files.copy(gotour.openStream(),path.get(fileName))应该是您所需要的全部。啊!一行!太棒了!我真的应该试着用nio来做这个。。。羞愧me@JBNizet你能从你的评论中作出回答吗?然后我就可以结束这个问题了我真希望我所有的问题都能这样回答:-)