0

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));
    }
}

この URL を使用して、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();
        }
    }
}

}

前もって感謝します。

4

1 に答える 1

1
  1. sun.net.www.protocol.http.HttpURLConnectionに変更java.net.HttpURLConnection

  2. fos.close()後に追加dest.close()

  3. currentEntry = tarInput.getNextTarEntry();while ループ内でも呼び出す必要があります。

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/");
    }
}
于 2015-08-03T14:37:43.003 に答える