Commit 89db7652 authored by Riegel's avatar Riegel
Browse files

Refactor: Rework zipping algorithm

parent be520dc3
Pipeline #10903 passed with stage
in 5 minutes and 8 seconds
...@@ -4,15 +4,14 @@ import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel; ...@@ -4,15 +4,14 @@ import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.zip.CityGmlZipArchive; import de.hft.stuttgart.citydoctor2.zip.CityGmlZipArchive;
import de.hft.stuttgart.citydoctor2.zip.CityGmlZipEntry; import de.hft.stuttgart.citydoctor2.zip.CityGmlZipEntry;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
...@@ -23,9 +22,9 @@ public class ArchivePacker { ...@@ -23,9 +22,9 @@ public class ArchivePacker {
private ArchivePacker(){} private ArchivePacker(){}
public static void packArchive(String targetPath, CityGmlZipArchive archive){ public static void packArchive(String targetPath, CityGmlZipArchive archive){
if (!targetPath.endsWith(".zip")){ Path outputPath = Path.of(targetPath);
targetPath = targetPath.concat(".zip"); outputPath = outputPath.getParent().resolve(
} FilenameUtils.removeExtension(outputPath.getFileName().toString()) + ".zip");
Path tmpDir = null; Path tmpDir = null;
try{ try{
tmpDir = Files.createTempDirectory("zipTmp"); tmpDir = Files.createTempDirectory("zipTmp");
...@@ -35,10 +34,10 @@ public class ArchivePacker { ...@@ -35,10 +34,10 @@ public class ArchivePacker {
continue; continue;
} }
CityDoctorModel model = entry.getModel(); CityDoctorModel model = entry.getModel();
String filename = tmpDir.toString() + File.separator + entry.getFullFileName(); Path filePath = tmpDir.resolve(entry.getFullFileName());
model.saveAs(filename, model.isValidated()); model.saveAs(filePath.toString(), model.isValidated());
} }
zipDirectory(targetPath, tmpDir.toString()); zipFolder(tmpDir.toFile(), outputPath.toFile());
}catch (Exception e){ }catch (Exception e){
logger.error(e); logger.error(e);
...@@ -53,45 +52,46 @@ public class ArchivePacker { ...@@ -53,45 +52,46 @@ public class ArchivePacker {
} }
} }
private static void zipDirectory(String targetPath, String sourcePath){
File directory = new File(sourcePath); public static void zipFolder(File folder, File zipFile) throws IOException {
if (!directory.exists()){ zipFolder(folder, new FileOutputStream(zipFile));
throw new IllegalStateException("Directory " + sourcePath + " does not exist");
} }
List<String> fileList = new ArrayList<>(Arrays.asList(directory.list()));
byte[] buffer = new byte[1024];
try (FileOutputStream fos = new FileOutputStream(targetPath); public static void zipFolder(File folder, OutputStream outputStream) throws IOException {
ZipOutputStream zos = new ZipOutputStream(fos)){ try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
for (String file : fileList){ processFolder(folder, zipOutputStream, folder.getPath().length() + 1);
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
Path sourceFile = Path.of(sourcePath+File.separator+file);
try (FileInputStream in = new FileInputStream(sourceFile.toAbsolutePath().toString())){
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
} }
} }
private static void processFolder(File folder, ZipOutputStream zipOutputStream, int prefixLength)
throws IOException {
for (File file : folder.listFiles()) {
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(file.getPath().substring(prefixLength));
zipOutputStream.putNextEntry(zipEntry);
try (FileInputStream inputStream = new FileInputStream(file)) {
IOUtils.copy(inputStream, zipOutputStream);
}
zipOutputStream.closeEntry();
} else if (file.isDirectory()) {
processFolder(file, zipOutputStream, prefixLength);
} }
logger.info("Successfully created zip-archive");
} catch (IOException e) {
logger.error(e);
} }
} }
public static void packAndDeleteDirectory(String directoryPath) throws IOException { public static void packAndDeleteDirectory(String directoryPath) throws IOException {
Path path = Path.of(directoryPath); Path sourcePath = Path.of(directoryPath);
if (!path.toFile().exists()) { if (!Files.exists(sourcePath)) {
throw new IllegalStateException("Directory " + directoryPath + " does not exist"); throw new IllegalStateException("Directory " + directoryPath + " does not exist");
} }
if (!path.toFile().isDirectory()) {
if (!Files.isDirectory(sourcePath)) {
throw new IllegalStateException("Path " + directoryPath + " is not a directory"); throw new IllegalStateException("Path " + directoryPath + " is not a directory");
} }
String outputPath = path.getParent().resolve(path.getFileName() + ".zip").toString(); Path outputPath = sourcePath.getParent().resolve(sourcePath.getFileName() + ".zip");
zipDirectory(outputPath, directoryPath); zipFolder(sourcePath.toFile(), outputPath.toFile());
FileUtils.deleteDirectory(path.toFile()); FileUtils.deleteDirectory(sourcePath.toFile());
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment