From 43a12342e9a8ea00e0135da81d24baf1d51d1445 Mon Sep 17 00:00:00 2001 From: NikolayHD Date: Mon, 30 Jul 2018 22:40:00 +0300 Subject: [PATCH] Fix incorrect path when extracting downloaded .zip --- .../src/forge/assets/AssetsDownloader.java | 9 +++-- .../main/java/forge/deck/NetDeckCategory.java | 6 ++-- .../forge/download/GuiDownloadZipService.java | 36 +++++++++++-------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java index d0089a33f80..92695c4fdc7 100644 --- a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java +++ b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java @@ -46,11 +46,14 @@ public class AssetsDownloader { } if (SOptionPane.showConfirmDialog(message, "New Version Available", "Update Now", "Update Later")) { String filename = "forge-android-" + version + "-signed-aligned.apk"; - String apkFile = new GuiDownloadZipService("", "update", + + GuiDownloadZipService downloadService = new GuiDownloadZipService("", "update", "https://releases.cardforge.org/forge/forge-gui-android/" + version + "/" + filename, - Forge.getDeviceAdapter().getDownloadsDir(), null, splashScreen.getProgressBar()).download(filename); + Forge.getDeviceAdapter().getDownloadsDir(), null, splashScreen.getProgressBar()); + + File apkFile = downloadService.download(filename); if (apkFile != null) { - Forge.getDeviceAdapter().openFile(apkFile); + Forge.getDeviceAdapter().openFile(apkFile.getPath()); Forge.exit(true); return; } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java index 75fcdbec995..146647a8680 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java @@ -89,10 +89,10 @@ public class NetDeckCategory extends StorageBase { String downloadLoc = c.getFullPath(); GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); + protected void copyInputStream(InputStream in, File outFile) throws IOException { + super.copyInputStream(in, outFile); - Deck deck = DeckSerializer.fromFile(new File(outPath)); + Deck deck = DeckSerializer.fromFile(outFile); if (deck != null) { c.map.put(deck.getName(), deck); } diff --git a/forge-gui/src/main/java/forge/download/GuiDownloadZipService.java b/forge-gui/src/main/java/forge/download/GuiDownloadZipService.java index 3d1e11ace8a..7fb0fb6b58b 100644 --- a/forge-gui/src/main/java/forge/download/GuiDownloadZipService.java +++ b/forge-gui/src/main/java/forge/download/GuiDownloadZipService.java @@ -9,6 +9,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Paths; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -69,8 +70,11 @@ public class GuiDownloadZipService extends GuiDownloadService { public void downloadAndUnzip() { filesExtracted = 0; - String zipFilename = download("temp.zip"); - if (zipFilename == null) { return; } + + File file = download("temp.zip"); + if (file == null) { + return; + } //if assets.zip downloaded successfully, unzip into destination folder try { @@ -83,15 +87,15 @@ public class GuiDownloadZipService extends GuiDownloadService { progressBar.reset(); progressBar.setDescription("Deleting old " + desc + "..."); if (deleteFolder.equals(destFolder)) { //move zip file to prevent deleting it - final String oldZipFilename = zipFilename; - zipFilename = deleteDir.getParentFile().getAbsolutePath() + File.separator + "temp.zip"; - Files.move(new File(oldZipFilename), new File(zipFilename)); + final File oldFile = file; + file = Paths.get(deleteDir.getParentFile().getAbsolutePath(), "temp.zip").toFile(); + Files.move(oldFile, file); } FileUtil.deleteDirectory(deleteDir); } } - final ZipFile zipFile = new ZipFile(zipFilename); + final ZipFile zipFile = new ZipFile(file); final Enumeration entries = zipFile.entries(); progressBar.reset(); @@ -109,13 +113,14 @@ public class GuiDownloadZipService extends GuiDownloadService { try { final ZipEntry entry = entries.nextElement(); - final String path = destFolder + entry.getName(); + final File unzippedEntry = Paths.get(destFolder, entry.getName()).toFile(); + if (entry.isDirectory()) { - new File(path).mkdir(); + unzippedEntry.mkdir(); progressBar.setValue(++count); continue; } - copyInputStream(zipFile.getInputStream(entry), path); + copyInputStream(zipFile.getInputStream(entry), unzippedEntry); progressBar.setValue(++count); filesExtracted++; } @@ -130,7 +135,7 @@ public class GuiDownloadZipService extends GuiDownloadService { } zipFile.close(); - new File(zipFilename).delete(); + file.delete(); } catch (final Exception e) { e.printStackTrace(); @@ -140,7 +145,7 @@ public class GuiDownloadZipService extends GuiDownloadService { } } - public String download(final String filename) { + public File download(final String filename) { GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while downloading progressBar.reset(); @@ -175,7 +180,7 @@ public class GuiDownloadZipService extends GuiDownloadService { FileUtil.ensureDirectoryExists(destFolder); // output stream to write file - final String destFile = destFolder + filename; + final File destFile = Paths.get(destFolder, filename).toFile(); final OutputStream output = new FileOutputStream(destFile); int count; @@ -195,9 +200,10 @@ public class GuiDownloadZipService extends GuiDownloadService { input.close(); if (cancel) { - new File(destFile).delete(); + destFile.delete(); return null; } + return destFile; } catch (final Exception ex) { @@ -209,10 +215,10 @@ public class GuiDownloadZipService extends GuiDownloadService { } } - protected void copyInputStream(final InputStream in, final String outPath) throws IOException{ + protected void copyInputStream(final InputStream in, final File outFile) throws IOException{ final byte[] buffer = new byte[1024]; int len; - final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath)); + final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); while((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len);