Add Main.java
parent
fb6055ddc4
commit
a16f671630
|
|
@ -0,0 +1,154 @@
|
|||
package updater;
|
||||
|
||||
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.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final String windowTitle = "Network Control Update";
|
||||
private static final String url = "http://10.10.0.52/radek/test.zip";
|
||||
private static final String jarApp = "NetworkControlClient.jar";
|
||||
|
||||
private static Window okno = new Window();
|
||||
|
||||
public static void main(String[] args) {
|
||||
okno.setTitle(windowTitle);
|
||||
okno.setLocationRelativeTo(null);
|
||||
okno.setVisible(true);
|
||||
|
||||
try {
|
||||
okno.setIconImage(ImageIO.read(new File("img/app.png")));
|
||||
} catch (IOException ex) {
|
||||
System.err.println("APP icon exception");
|
||||
}
|
||||
|
||||
Thread t = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
// stahovani aktualizacniho souboru
|
||||
System.out.print("Stahovani souboru ...");
|
||||
Window.label.setText("Stahování aktualizace ...");
|
||||
download(url);
|
||||
System.out.println(" hotovo");
|
||||
|
||||
// rozbaleni souboru
|
||||
System.out.print("Aktualizace ...");
|
||||
Window.label.setText("Aktualizace ...");
|
||||
unZipIt("update.zip");
|
||||
System.out.println(" hotovo");
|
||||
|
||||
// spusteni programu po aktualizaci
|
||||
launch(jarApp);
|
||||
|
||||
// ukonceni programu
|
||||
System.exit(0);
|
||||
}
|
||||
;
|
||||
};
|
||||
t.run();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void download(String urlFile){
|
||||
try {
|
||||
URL url = new URL(urlFile);
|
||||
HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
|
||||
// zjisteni veliksoti souboru
|
||||
long completeFileSize = httpConnection.getContentLength();
|
||||
// nastaveni velikosti progress bar
|
||||
Window.pb.setMaximum((int) completeFileSize);
|
||||
// stahovani
|
||||
BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream());
|
||||
FileOutputStream fos = new FileOutputStream("update.zip");
|
||||
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
|
||||
byte[] data = new byte[1024];
|
||||
long downloadedFileSize = 0;
|
||||
int x = 0;
|
||||
while ((x = in.read(data, 0, 1024)) >= 0) {
|
||||
downloadedFileSize += x;
|
||||
// vypocet progressu
|
||||
final int currentProgress = (int) downloadedFileSize;
|
||||
// update progress bar
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Window.pb.setValue(currentProgress);
|
||||
}
|
||||
});
|
||||
bout.write(data, 0, x);
|
||||
}
|
||||
bout.close();
|
||||
in.close();
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void unZipIt(String zipFile) {
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
try {
|
||||
|
||||
//get the zip file content
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
|
||||
//get the zipped file list entry
|
||||
ZipEntry ze = zis.getNextEntry();
|
||||
|
||||
while (ze != null) {
|
||||
if (ze.isDirectory()) {
|
||||
System.out.println("Creating Directory:" + ze.getName());
|
||||
FileSystem fileSystem = FileSystems.getDefault();
|
||||
Files.createDirectories(fileSystem.getPath(ze.getName()));
|
||||
} else {
|
||||
String fileName = ze.getName();
|
||||
File newFile = new File(fileName);
|
||||
|
||||
System.out.println("file unzip : " + newFile.getAbsoluteFile());
|
||||
|
||||
FileOutputStream fos = new FileOutputStream(newFile);
|
||||
int len;
|
||||
while ((len = zis.read(buffer)) > 0) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
|
||||
fos.close();
|
||||
|
||||
}
|
||||
ze = zis.getNextEntry();
|
||||
}
|
||||
|
||||
zis.closeEntry();
|
||||
zis.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void launch(String jar) {
|
||||
String[] run = {"java", "-jar", jar};
|
||||
try {
|
||||
Runtime.getRuntime().exec(run);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue