From 23daaccecc9ecf187f02c80616ac6beb0752dac2 Mon Sep 17 00:00:00 2001 From: Heather White <66441550+HeatherComputer@users.noreply.github.com> Date: Sun, 22 Sep 2024 15:46:04 +0100 Subject: [PATCH] fix lack of a buffer when backing up world before restoration, and autocleanup --- .../cli/AdvancedBackupsCLI.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java b/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java index 4365745..6aaa907 100644 --- a/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java +++ b/src/main/java/co/uk/mommyheather/advancedbackups/cli/AdvancedBackupsCLI.java @@ -123,7 +123,7 @@ public static void main(String args[]) throws IllegalBackupException { } type = CLIIOHelpers.getBackupType(type); - if (type.equals("snapshot (command-made only)")) type = "snapshots"; + if ("snapshot (command-made only)".equals(type)) type = "snapshots"; /*/ if (backupLocation.startsWith(Pattern.quote(File.separator)) || backupLocation.indexOf(":") == 1) { @@ -146,7 +146,7 @@ public static void main(String args[]) throws IllegalBackupException { Arrays.asList(new String[]{"Export backup as zip", "Restore single file", "Restore entire world"}) ); - if (restore.equals("Export backup as zip")) { + if ("Export backup as zip".equals(restore)) { exportMode = true; } @@ -263,7 +263,7 @@ private static int getBackupDate(File backupDir, boolean exportMode) throws IOEx throw new IOException(String.format("Selected backup directory %s is empty, or is a file!", backupDir.getAbsolutePath())); } ArrayList fileNameList = new ArrayList(Arrays.asList(fileNameArray)); //i need to do this. i hate this. - fileNameList.removeIf((name) -> ( + fileNameList.removeIf(name -> ( name.endsWith("json") || name.contains("incomplete") || name.contains("DS_Store") @@ -779,7 +779,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) thro Files.walkFileTree(root, new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { - if (file.getFileName().toString().equals("level.dat")) levelDatPathWrapper.add(file); + if ("level.dat".equals(file.getFileName().toString())) levelDatPathWrapper.add(file); return FileVisitResult.CONTINUE; } }); @@ -856,6 +856,7 @@ private static String backupExistingWorld(File worldDir, boolean export) { FileOutputStream outputStream = new FileOutputStream(out); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream.setLevel(4); + byte[] bytes = new byte[1024]; Files.walkFileTree(worldDir.toPath(), new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { @@ -866,8 +867,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { return FileVisitResult.CONTINUE; } zipOutputStream.putNextEntry(new ZipEntry(targetFile.toString())); - byte[] bytes = Files.readAllBytes(file); - zipOutputStream.write(bytes, 0, bytes.length); + FileInputStream is = new FileInputStream(file.toFile()); + while (true) { + int i = is.read(bytes); + if (i < 0) break; + zipOutputStream.write(bytes, 0, i); + } + is.close(); + //byte[] bytes = Files.readAllBytes(file); + //zipOutputStream.write(bytes, 0, bytes.length); zipOutputStream.closeEntry(); } catch (IOException e) {