Skip to content

Commit

Permalink
stup
Browse files Browse the repository at this point in the history
  • Loading branch information
smashyalts committed Mar 7, 2024
1 parent 2f474f9 commit 6431e80
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 74 deletions.
10 changes: 10 additions & 0 deletions chunky-dedicated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down Expand Up @@ -124,5 +129,10 @@
<version>1.3.38</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.5-5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,75 +1,68 @@
package com.funniray.chunkydedicated;
package com.funniray.chunkydedicated;

import java.io.*;
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;
import java.io.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectory {
private static AtomicLong totalSize = new AtomicLong(0);
private static AtomicLong zippedSize = new AtomicLong(0);
public class ZipDirectory {
private static AtomicLong totalSize = new AtomicLong(0);
private static AtomicLong zippedSize = new AtomicLong(0);
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public static void main(String args) {
String sourceFile = args;
try (FileOutputStream fos = new FileOutputStream("world.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos)) {
zipOut.setLevel(Deflater.BEST_COMPRESSION);
File fileToZip = new File(sourceFile);
totalSize.set(getTotalSize(fileToZip));

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));
scheduler.scheduleAtFixedRate(ZipDirectory::printProgress, 0, 30, TimeUnit.SECONDS);

ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new ZipAction(fileToZip, fileToZip.getName(), zipOut));
pool.shutdown();
pool.awaitQuiescence(60, TimeUnit.SECONDS);

pool.shutdown();
pool.awaitQuiescence(60, TimeUnit.SECONDS);
scheduler.shutdown();

System.gc();
} catch (IOException e) {
e.printStackTrace();
System.gc();
} catch (IOException e) {
e.printStackTrace();
}
}
}

static class ZipAction extends RecursiveAction {
private File fileToZip;
private String fileName;
private ZipOutputStream zipOut;

ZipAction(File fileToZip, String fileName, ZipOutputStream zipOut) {
this.fileToZip = fileToZip;
this.fileName = fileName;
this.zipOut = zipOut;
}
static class ZipAction extends RecursiveAction {
private File fileToZip;
private String fileName;
private ZipOutputStream zipOut;

@Override
protected void compute() {
if (fileToZip.isHidden()) {
return;
ZipAction(File fileToZip, String fileName, ZipOutputStream zipOut) {
this.fileToZip = fileToZip;
this.fileName = fileName;
this.zipOut = zipOut;
}
if (fileToZip.isDirectory()) {
if (!fileName.endsWith("/")) {
fileName += "/";

@Override
protected void compute() {
if (fileToZip.isHidden()) {
return;
}
synchronized (zipOut) {
try {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} catch (IOException e) {
e.printStackTrace();
if (fileToZip.isDirectory()) {
if (!fileName.endsWith("/")) {
fileName += "/";
}
}

File[] children = fileToZip.listFiles();
if (children != null) {
for (File childFile : children) {
ZipAction action = new ZipAction(childFile, fileName + childFile.getName(), zipOut);
action.fork();
File[] children = fileToZip.listFiles();
if (children != null) {
for (File childFile : children) {
ZipAction action = new ZipAction(childFile, fileName + childFile.getName(), zipOut);
action.compute(); // Call compute directly instead of forking
}
}
}
} else {
synchronized (zipOut) {
} else {
try (FileInputStream fis = new FileInputStream(fileToZip)) {
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
Expand All @@ -78,30 +71,29 @@ protected void compute() {
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
zippedSize.addAndGet(length);
printProgress();
}
zipOut.closeEntry(); // Close the entry after writing the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

private static long getTotalSize(File file) {
long size = 0;
if (file.isDirectory()) {
for (File child : file.listFiles()) {
size += getTotalSize(child);
private static long getTotalSize(File file) {
long size = 0;
if (file.isDirectory()) {
for (File child : file.listFiles()) {
size += getTotalSize(child);
}
} else {
size = file.length();
}
} else {
size = file.length();
return size;
}
return size;
}

private static void printProgress() {
double progress = (double) zippedSize.get() / totalSize.get() * 100;
System.out.printf("Progress: %.2f%%\n", progress);
}
}
private static void printProgress() {
double progress = (double) zippedSize.get() / totalSize.get() * 100;
System.out.printf("Progress: %.2f%%\n", progress);
}
}

0 comments on commit 6431e80

Please sign in to comment.