Skip to content

Commit

Permalink
stup
Browse files Browse the repository at this point in the history
  • Loading branch information
smashyalts committed Mar 1, 2024
1 parent 5373516 commit 2f474f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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);
}
}

0 comments on commit 2f474f9

Please sign in to comment.