forked from funniray/chunky-dedicated
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added retrying and increased client timeout thresholds
- Loading branch information
1 parent
8a349f0
commit 9a4995c
Showing
3 changed files
with
121 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 53 additions & 30 deletions
83
chunky-dedicated/src/main/java/com/funniray/chunkydedicated/ZipDirectory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,72 @@ | ||
package com.funniray.chunkydedicated; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.*; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.RecursiveAction; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
public class ZipDirectory { | ||
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); | ||
|
||
File fileToZip = new File(sourceFile); | ||
zipFile(fileToZip, fileToZip.getName(), zipOut); | ||
|
||
ForkJoinPool pool = new ForkJoinPool(); | ||
pool.invoke(new ZipAction(fileToZip, fileToZip.getName(), zipOut)); | ||
|
||
zipOut.close(); | ||
fos.close(); | ||
System.gc(); | ||
} | ||
|
||
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException { | ||
if (fileToZip.isHidden()) { | ||
return; | ||
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; | ||
} | ||
if (fileToZip.isDirectory()) { | ||
if (fileName.endsWith("/")) { | ||
zipOut.putNextEntry(new ZipEntry(fileName)); | ||
zipOut.closeEntry(); | ||
} else { | ||
zipOut.putNextEntry(new ZipEntry(fileName + "/")); | ||
zipOut.closeEntry(); | ||
} | ||
File[] children = fileToZip.listFiles(); | ||
for (File childFile : children) { | ||
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); | ||
|
||
@Override | ||
protected void compute() { | ||
if (fileToZip.isHidden()) { | ||
return; | ||
} | ||
} else { | ||
FileInputStream fis = new FileInputStream(fileToZip); | ||
ZipEntry zipEntry = new ZipEntry(fileName); | ||
zipOut.putNextEntry(zipEntry); | ||
byte[] bytes = new byte[1024]; | ||
int length; | ||
while ((length = fis.read(bytes)) >= 0) { | ||
zipOut.write(bytes, 0, length); | ||
if (fileToZip.isDirectory()) { | ||
if (!fileName.endsWith("/")) { | ||
fileName += "/"; | ||
} | ||
try { | ||
zipOut.putNextEntry(new ZipEntry(fileName)); | ||
zipOut.closeEntry(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
File[] children = fileToZip.listFiles(); | ||
for (File childFile : children) { | ||
ZipAction action = new ZipAction(childFile, fileName + childFile.getName(), zipOut); | ||
action.fork(); // Start the action asynchronously | ||
} | ||
} else { | ||
try (FileInputStream fis = new FileInputStream(fileToZip)) { | ||
ZipEntry zipEntry = new ZipEntry(fileName); | ||
zipOut.putNextEntry(zipEntry); | ||
byte[] bytes = new byte[1024]; | ||
int length; | ||
while ((length = fis.read(bytes)) >= 0) { | ||
zipOut.write(bytes, 0, length); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
fis.close(); | ||
} | ||
}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters