java - 如何使用 Java 下载 WordPress

标签 java wordpress apache-commons apache-commons-compress

我要下载WordPress使用 Java。

我的代码如下所示:

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 格式。

我读了这个Issues uncompressing a tar.gz file看起来当我下载 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();
        }
    }
}

}

提前致谢。

最佳答案

  1. sun.net.www.protocol.http.HttpURLConnection 更改为 java.net.HttpURLConnection

  2. dest.close()后添加fos.close()

  3. 您也必须在 while 循环内调用 currentEntry = tarInput.getNextTarEntry();

没有任何启用cookie接受条款和服务

这是我的完整代码。 请尝试一下并将其与您的代码进行比较:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

public class Downloader {

    public static final int BUFFER = 2048;

    private void download(String surl, String pathToSave) throws IOException {
        URL url = new URL(surl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        try (InputStream stream = con.getInputStream()) {
            Files.copy(stream, Paths.get(pathToSave));
        }
    }

    private void unGz(String pathToGz, String outputPath) throws IOException {
        FileInputStream fin = new FileInputStream(pathToGz);
        BufferedInputStream in = new BufferedInputStream(fin);
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            try (GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in)) {
                final byte[] buffer = new byte[BUFFER];
                int n = 0;
                while (-1 != (n = gzIn.read(buffer))) {
                    out.write(buffer, 0, n);
                }
            }
        }
    }

    public void unTarGz(String pathToTar, String outputPath) throws IOException {
        File tarFile = new File(pathToTar);
        TarArchiveInputStream tarInput
                = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarFile)));

        TarArchiveEntry currentEntry;
        while ((currentEntry = tarInput.getNextTarEntry()) != null) {
            if (currentEntry.isDirectory()) {
                File f = new File(outputPath + currentEntry.getName());
                f.mkdirs();
            } else {
                int count;
                byte data[] = new byte[BUFFER];
                try (FileOutputStream fos = new FileOutputStream(outputPath
                        + currentEntry.getName())) {
                    try (BufferedOutputStream dest = new BufferedOutputStream(fos,
                            BUFFER)) {
                        while ((count = tarInput.read(data, 0, BUFFER)) != -1) {
                            dest.write(data, 0, count);
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        Downloader down = new Downloader();
        down.download("https://wordpress.org/latest.tar.gz", "/tmp/latest.tar.gz");
        down.unTarGz("/tmp/latest.tar.gz", "/tmp/untar/");
    }
}

关于java - 如何使用 Java 下载 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593663/

相关文章:

php - Wordpress - 分页帖子,按 : meta_key only from current category 排序

php - 按类别和标签列出 WordPress 帖子

javascript - jQuery 删除 Chrome 中最近的问题

java - 是否有任何泛型版本的 apache 公共(public)对象池?

Java:哪个 Apache Digester 类?

java - Maven 对 apache commons-text 的依赖?

java - 等待线程在 shutdownhook 中写入更改

java - java中的可点击图像按钮,没有android中的xml

java - 改造 call.enqueue 代码 200,但 response.body() 为空

java - 为 Spring WebSocket 设置 CSRF