gpt4 book ai didi

Java Wget Bz2 文件

转载 作者:行者123 更新时间:2023-12-02 04:26:59 24 4
gpt4 key购买 nike

我正在尝试从维基百科获取一些 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;
}
}

最后,当我对 inputstreamoutputstream 进行 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));

关于Java Wget Bz2 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com