How to download WordPress with Java -
i want download wordpress java.
my code looks this:
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)); } }
i using url download latest version of wordpress: http://wordpress.org/latest.tar.gz
but when try extracting tar.gz file error saying file not in gzip format.
i read issues uncompressing tar.gz file , looks when download wordpress need have cookie enabled accept terms , services.
how this?
or incorrectly downloading tar.gz file?
here tar.gz extracting code:
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(); } } }
}
thanks in advance.
change
sun.net.www.protocol.http.httpurlconnection
java.net.httpurlconnection
add
fos.close()
afterdest.close()
you must call
currententry = tarinput.getnexttarentry();
inside while loop, too.
there nothing cookie enabled or accept terms , services.
here complete code. please try , compare code:
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/"); } }
Comments
Post a Comment