package com.tsc.j2v8_tsc;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
/**
*
* @author Kushan
*/
public class AssetCompiler {
static String _URL = "https://registry.npmjs.org/npm/-/npm-4.0.3.tgz";
public static void main(String[] args) throws Exception {
File file = new File("C:\\Manju\\games\\filess", "npm-4.0.3.tgz");
if (!file.exists()) {
file.createNewFile();
}
Vertx vtx = Vertx.vertx();
HttpClient client = vtx.createHttpClient();
try {
client.getNow(_URL, res -> {
res.bodyHandler(body -> {
System.out.println("Writing content...");
vtx.fileSystem().writeFileBlocking(file.getPath(), body);
});
res.endHandler(end -> {
System.out.println("Writing content ends...");
unzip(file);
client.close();
});
res.exceptionHandler(err -> {
err.printStackTrace();
client.close();
});
});
} catch (Exception e) {
e.printStackTrace();
}
}
private static void unzip(File file) {
InputStream is;
try {
is = new FileInputStream(file);
TarArchiveInputStream tar = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry;
while ((entry = tar.getNextTarEntry()) != null) {
final File outputFile = new File(file.getParentFile(), entry.getName());
if (entry.isDirectory()) {
System.out.println(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
System.out.println(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
if (!outputFile.exists()) {
outputFile.getParentFile().mkdirs();
}
System.out.println(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(tar, outputFileStream);
}
}
tar.close();
is.close();
} catch (Exception ex) {
Logger.getLogger(AssetCompiler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.tsc.j2v8_tsc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
/**
*
* @author Kushan
*/
public class AssetCompiler {
static String _URL = "https://registry.npmjs.org/npm/-/npm-4.0.3.tgz";
public static void main(String[] args) throws Exception {
File file = new File("C:\\Manju\\games\\filess", "npm-4.0.3.tgz");
if (!file.exists()) {
file.createNewFile();
}
HttpClient client = HttpClients.createDefault();
HttpGet download = new HttpGet(_URL);
client.execute(download, (HttpResponse response) -> {
System.out.println(response.getEntity().getContentLength());
IOUtils.copy(response.getEntity().getContent(), new FileOutputStream(file));
unzip(file);
return null;
});
}
private static void unzip(File file) {
final byte[] signature = new byte[12];
try {
InputStream in = new FileInputStream(file);
in.mark(signature.length);
int l = in.read(signature);
in.reset();
CompressorInputStream cis;
if (BZip2CompressorInputStream.matches(signature, l)) {
cis = new BZip2CompressorInputStream(in);
} else if (GzipCompressorInputStream.matches(signature, l)) {
cis = new GzipCompressorInputStream(in);
} else {
cis = new LZMACompressorInputStream(in);
}
TarArchiveInputStream ais = new TarArchiveInputStream(cis);
TarArchiveEntry entry;
while ((entry = ais.getNextTarEntry()) != null) {
final File outputFile = new File(file.getParentFile(), entry.getName());
if (entry.isDirectory()) {
System.out.println(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
System.out.println(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
if (!outputFile.exists()) {
outputFile.getParentFile().mkdirs();
}
System.out.println(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(ais, outputFileStream);
}
}
ais.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}HttpClient getNow(String requestURI, Handler<HttpClientResponse> responseHandler)
requestURI - the relative URIresponseHandler - the response handler