我想用 Java下载WordPress 。
我的代码如下所示:
public void file(String surl, String pathToSave) throws IOException {
URL url = new URL(surl);
sun.net.www.protocol.http.HttpURLConnection con = (HttpURLConnection) url.openConnection();
try (InputStream stream = con.getInputStream()) {
Files.copy(stream, Paths.get(pathToSave));
}
}
我正在使用此网址下载最新版本的 WordPress:http ://wordpress.org/latest.tar.gz
但是当我尝试提取 tar.gz 文件时,我收到一条错误消息,指出该文件不是 gzip 格式。
我阅读了解压缩 tar.gz 文件的问题,看起来当我下载 WordPress 时,我需要启用 cookie 才能接受条款和服务。
我该怎么做?
还是我错误地下载了 tar.gz 文件?
这是我的 tar.gz 提取代码:
public class Unzip {
public static int BUFFER = 2048;
public void tar(String pathToTar, String outputPath) throws IOException {
File tarFile = new File(pathToTar);
TarArchiveInputStream tarInput =
new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarFile)));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
while(currentEntry != null) {
if (currentEntry.isDirectory()) {
File f = new File(outputPath + currentEntry.getName());
f.mkdirs();
}
else {
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(outputPath
+ currentEntry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
while ((count = tarInput.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.close();
}
}
}
}
提前致谢。