Skip to content

Commit

Permalink
added support to download and extract zip file.
Browse files Browse the repository at this point in the history
  • Loading branch information
JuKu committed Apr 23, 2017
1 parent 44aa9f1 commit 72660c1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
49 changes: 46 additions & 3 deletions core/src/main/java/com/jukusoft/updater/ui/UpdaterUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import com.jukusoft.updater.font.BitmapFontFactory;
import com.jukusoft.updater.skin.SkinFactory;
import com.jukusoft.updater.update.VersionInfo;
import com.jukusoft.updater.utils.FileUtils;
import com.jukusoft.updater.utils.GameTime;
import com.jukusoft.updater.utils.PlatformUtils;
import com.jukusoft.updater.utils.*;
import javafx.application.Platform;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipFile;

/**
* Created by Justin on 22.04.2017.
Expand Down Expand Up @@ -232,6 +231,9 @@ protected void startGame () {

System.exit(1);
}

//exit launcher after start game
System.exit(0);
}

protected void updateGame () {
Expand All @@ -243,9 +245,50 @@ protected void updateGame () {

this.isUpdating = true;

//update progressbar
this.updateText = "Get ZIP url...";
this.filledBar.setValue(2);
this.filledBar.setText("2%");

//get URL to zip file
String zipPath = this.versionInfo.getNewestVersionZIPURL();
System.out.println("path to ZIP file: " + zipPath);

//update progressbar
this.updateText = "Download ZIP file...";
this.filledBar.setValue(20);
this.filledBar.setText("20%");

//delete old update files, if neccessary
if (new File("./update.zip").exists()) {
new File("./update.zip").delete();
}

//download zip
try {
WebsiteUtils.downloadFile(zipPath, "./update.zip");
} catch (IOException e) {
e.printStackTrace();
isUpdating = false;

this.startButton.setVisible(true);

return;
}

//update progressbar
this.updateText = "Extract ZIP file...";
this.filledBar.setValue(50);
this.filledBar.setText("50%");

ZIPUtils.unZipIt("./update.zip", ".");

//update progressbar
this.updateText = "Done...";
this.filledBar.setValue(100);
this.filledBar.setText("100%");

this.startButton.setVisible(true);
}

}
27 changes: 24 additions & 3 deletions core/src/main/java/com/jukusoft/updater/utils/WebsiteUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.jukusoft.updater.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -27,4 +26,26 @@ public static String getWebsiteContent (String url) throws IOException {
return a.toString();
}

public static void downloadFile (String fileURL, String outputFile) throws IOException {
URL url = new URL(fileURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(outputFile);

copy(in, out, 1024);

out.close();
}

public static void copy (InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}

}
61 changes: 61 additions & 0 deletions core/src/main/java/com/jukusoft/updater/utils/ZIPUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.jukusoft.updater.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
* Created by Justin on 23.04.2017.
*/
public class ZIPUtils {

public static boolean unZipIt(String zipFile, String outputFolder){

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){

String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);

System.out.println("file unzip : "+ newFile.getAbsoluteFile());

//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();

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

System.out.println("Done");
} catch(IOException ex){
ex.printStackTrace();

return false;
}

return true;
}

}

0 comments on commit 72660c1

Please sign in to comment.