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

Refactor: Rework zipping algorithm

2 merge requests!28Version 3.17.0 Release,!26Add ZIP-archive support
Pipeline #10903 passed with stage
in 5 minutes and 8 seconds
Showing with 35 additions and 35 deletions
+35 -35
......@@ -4,15 +4,14 @@ import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.zip.CityGmlZipArchive;
import de.hft.stuttgart.citydoctor2.zip.CityGmlZipEntry;
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.Logger;
import java.io.*;
import java.nio.file.Files;
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.ZipOutputStream;
......@@ -23,9 +22,9 @@ public class ArchivePacker {
private ArchivePacker(){}
public static void packArchive(String targetPath, CityGmlZipArchive archive){
if (!targetPath.endsWith(".zip")){
targetPath = targetPath.concat(".zip");
}
Path outputPath = Path.of(targetPath);
outputPath = outputPath.getParent().resolve(
FilenameUtils.removeExtension(outputPath.getFileName().toString()) + ".zip");
Path tmpDir = null;
try{
tmpDir = Files.createTempDirectory("zipTmp");
......@@ -35,10 +34,10 @@ public class ArchivePacker {
continue;
}
CityDoctorModel model = entry.getModel();
String filename = tmpDir.toString() + File.separator + entry.getFullFileName();
model.saveAs(filename, model.isValidated());
Path filePath = tmpDir.resolve(entry.getFullFileName());
model.saveAs(filePath.toString(), model.isValidated());
}
zipDirectory(targetPath, tmpDir.toString());
zipFolder(tmpDir.toFile(), outputPath.toFile());
}catch (Exception e){
logger.error(e);
......@@ -53,45 +52,46 @@ public class ArchivePacker {
}
}
private static void zipDirectory(String targetPath, String sourcePath){
File directory = new File(sourcePath);
if (!directory.exists()){
throw new IllegalStateException("Directory " + sourcePath + " does not exist");
public static void zipFolder(File folder, File zipFile) throws IOException {
zipFolder(folder, new FileOutputStream(zipFile));
}
public static void zipFolder(File folder, OutputStream outputStream) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
processFolder(folder, zipOutputStream, folder.getPath().length() + 1);
}
List<String> fileList = new ArrayList<>(Arrays.asList(directory.list()));
byte[] buffer = new byte[1024];
}
try (FileOutputStream fos = new FileOutputStream(targetPath);
ZipOutputStream zos = new ZipOutputStream(fos)){
for (String file : fileList){
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 {
Path path = Path.of(directoryPath);
if (!path.toFile().exists()) {
Path sourcePath = Path.of(directoryPath);
if (!Files.exists(sourcePath)) {
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");
}
String outputPath = path.getParent().resolve(path.getFileName() + ".zip").toString();
zipDirectory(outputPath, directoryPath);
Path outputPath = sourcePath.getParent().resolve(sourcePath.getFileName() + ".zip");
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