Feat: split project zip in chunks

This commit is contained in:
Sheepit Renderfarm
2023-09-19 17:14:49 +00:00
parent 14c3e1ecc1
commit e803da9a3d
7 changed files with 139 additions and 48 deletions

View File

@@ -22,13 +22,18 @@ package com.sheepit.client;
import com.sheepit.client.Error.ServerCode;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.LocalFileHeader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@@ -36,12 +41,10 @@ import java.io.StringWriter;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
@@ -77,6 +80,64 @@ public class Utils {
return 0;
}
public static int unzipChunksIntoDirectory(List<String> full_path_chunks, String destinationDirectory, char[] password, Log log) {
try {
// STEP 1: assemble the chunks into an actual zip (in RAM)
ByteArrayOutputStream unzippedData = new ByteArrayOutputStream();
for (String full_path_chunk: full_path_chunks) {
byte[] data = Files.readAllBytes(Paths.get(full_path_chunk));
unzippedData.write(data);
}
byte[] full_data = unzippedData.toByteArray();
// STEP 2: unzip the zip like before
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(full_data));
if (password != null) {
zipInputStream.setPassword(password);
}
LocalFileHeader fileHeader = null;
while ((fileHeader = zipInputStream.getNextEntry()) != null) {
String outFilePath = destinationDirectory + File.separator + fileHeader.getFileName();
File outFile = new File(outFilePath);
//Checks if the file is a directory
if (fileHeader.isDirectory()) {
outFile.mkdirs();
continue;
}
File parentDir = outFile.getParentFile();
if (parentDir.exists() == false) {
parentDir.mkdirs();
}
FileOutputStream os = new FileOutputStream(outFile);
int readLen = -1;
byte[] buff = new byte[1024];
//Loop until End of File and write the contents to the output stream
while ((readLen = zipInputStream.read(buff)) != -1) {
os.write(buff, 0, readLen);
}
os.close();
}
zipInputStream.close();
}
catch (IOException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
log.debug("Utils::unzipChunksIntoDirectory exception " + e + " stacktrace: " + sw.toString());
return -1;
}
return 0;
}
public static String md5(String path_of_file_) {
Md5 md5 = new Md5();
return md5.get(path_of_file_);