From 2f474f9472d0a1e327ab11933adef0840a782ab5 Mon Sep 17 00:00:00 2001 From: smashyalts Date: Sat, 2 Mar 2024 00:47:43 +0100 Subject: [PATCH] stup --- .../chunkydedicated/ChunkyDedicated.java | 6 +- .../chunkydedicated/ZipDirectory.java | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ChunkyDedicated.java b/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ChunkyDedicated.java index 24d7c2d..0dba236 100644 --- a/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ChunkyDedicated.java +++ b/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ChunkyDedicated.java @@ -66,11 +66,7 @@ public void onEnable() { return; } getLogger().info("All tasks finished, uploading files to r2 and closing server..."); - try { - ZipDirectory.main("world"); - } catch (IOException e) { - throw new RuntimeException(e); - } + ZipDirectory.main("world"); DiscordWebhook webhook = new DiscordWebhook(getConfig().getString("webhook")); webhook.setContent("https://maps.r2.game.smd.gg/"+ "World." + key + ".zip"); webhook.setAvatarUrl("https://avatars.githubusercontent.com/u/108903815?s=400&u=80787b5c250845ab8ddbc4b9105c841714af3943&v=4"); diff --git a/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ZipDirectory.java b/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ZipDirectory.java index 9410c4d..9dad3a4 100644 --- a/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ZipDirectory.java +++ b/chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ZipDirectory.java @@ -4,28 +4,32 @@ import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDirectory { - public static void main(String args) throws IOException { - String sourceFile = args; - FileOutputStream fos = new FileOutputStream("world.zip"); - ZipOutputStream zipOut = new ZipOutputStream(fos); + private static AtomicLong totalSize = new AtomicLong(0); + private static AtomicLong zippedSize = new AtomicLong(0); - File fileToZip = new File(sourceFile); + public static void main(String args) { + String sourceFile = args; + try (FileOutputStream fos = new FileOutputStream("world.zip"); + ZipOutputStream zipOut = new ZipOutputStream(fos)) { - ForkJoinPool pool = new ForkJoinPool(); - pool.invoke(new ZipAction(fileToZip, fileToZip.getName(), zipOut)); + File fileToZip = new File(sourceFile); + totalSize.set(getTotalSize(fileToZip)); - zipOut.close(); - fos.close(); + ForkJoinPool pool = new ForkJoinPool(); + pool.invoke(new ZipAction(fileToZip, fileToZip.getName(), zipOut)); - pool.shutdown(); // Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted - pool.awaitQuiescence(60, TimeUnit.SECONDS); // Wait for all tasks to complete for up to 60 seconds + pool.shutdown(); + pool.awaitQuiescence(60, TimeUnit.SECONDS); - // Suggest to the JVM that it's a good time to run the Garbage Collector - System.gc(); + System.gc(); + } catch (IOException e) { + e.printStackTrace(); + } } static class ZipAction extends RecursiveAction { @@ -58,9 +62,11 @@ protected void compute() { } File[] children = fileToZip.listFiles(); - for (File childFile : children) { - ZipAction action = new ZipAction(childFile, fileName + childFile.getName(), zipOut); - action.fork(); // Start the action asynchronously + if (children != null) { + for (File childFile : children) { + ZipAction action = new ZipAction(childFile, fileName + childFile.getName(), zipOut); + action.fork(); + } } } else { synchronized (zipOut) { @@ -71,6 +77,8 @@ protected void compute() { int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); + zippedSize.addAndGet(length); + printProgress(); } } catch (IOException e) { e.printStackTrace(); @@ -79,4 +87,21 @@ protected void compute() { } } } + + private static long getTotalSize(File file) { + long size = 0; + if (file.isDirectory()) { + for (File child : file.listFiles()) { + size += getTotalSize(child); + } + } else { + size = file.length(); + } + return size; + } + + private static void printProgress() { + double progress = (double) zippedSize.get() / totalSize.get() * 100; + System.out.printf("Progress: %.2f%%\n", progress); + } } \ No newline at end of file