diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/utils/ArchivePacker.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/utils/ArchivePacker.java index b20881e1607f937d8b56b03d5e98503e887860fc..fa7aebf0b12ccaa49174a3b25b5d31f02a57df80 100644 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/utils/ArchivePacker.java +++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/utils/ArchivePacker.java @@ -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 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()); } }