From ee8f8567ea76e5a20c192bece7a307e47bf86ce3 Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Thu, 21 May 2015 22:22:32 +0200 Subject: [PATCH] DRYed ReplayFileIO code to add/remove files from Replay File --- .../registry/ReplayFileAppender.java | 6 +- .../replaymod/utils/ReplayFileIO.java | 66 ++++--------------- 2 files changed, 15 insertions(+), 57 deletions(-) diff --git a/src/main/java/eu/crushedpixel/replaymod/registry/ReplayFileAppender.java b/src/main/java/eu/crushedpixel/replaymod/registry/ReplayFileAppender.java index eb2ac086..e3590029 100644 --- a/src/main/java/eu/crushedpixel/replaymod/registry/ReplayFileAppender.java +++ b/src/main/java/eu/crushedpixel/replaymod/registry/ReplayFileAppender.java @@ -43,11 +43,7 @@ public class ReplayFileAppender extends Thread { if(mv != null) { if(mv.getRight().canWrite()) { try { - if(mv.getLeft().getLeft() == null) { - ReplayFileIO.removeFileFromZip(mv.getRight(), mv.getLeft().getRight()); - } else { - ReplayFileIO.addFileToZip(mv.getRight(), mv.getLeft().getLeft(), mv.getLeft().getRight()); - } + ReplayFileIO.addFileToZip(mv.getRight(), mv.getLeft().getLeft(), mv.getLeft().getRight()); } catch(Exception e) { e.printStackTrace(); filesToMove.add(mv); diff --git a/src/main/java/eu/crushedpixel/replaymod/utils/ReplayFileIO.java b/src/main/java/eu/crushedpixel/replaymod/utils/ReplayFileIO.java index b8f35519..e38cd1b8 100755 --- a/src/main/java/eu/crushedpixel/replaymod/utils/ReplayFileIO.java +++ b/src/main/java/eu/crushedpixel/replaymod/utils/ReplayFileIO.java @@ -301,11 +301,6 @@ public class ReplayFileIO { } public static void addFileToZip(File zipFile, File toAdd, String fileName) throws IOException { - if(toAdd == null) { - removeFileFromZip(zipFile, fileName); - return; - } - // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile()); // delete it, otherwise you cannot rename your existing zip to it. @@ -334,57 +329,24 @@ public class ReplayFileIO { zin.close(); // Compress the files - InputStream in = new FileInputStream(toAdd); - // Add ZIP entry to output stream. - out.putNextEntry(new ZipEntry(fileName)); - // Transfer bytes from the file to the ZIP file + if(toAdd != null) { + InputStream in = new FileInputStream(toAdd); + // Add ZIP entry to output stream. + out.putNextEntry(new ZipEntry(fileName)); + // Transfer bytes from the file to the ZIP file - int len; - if(fileName.equals("thumb")) out.write(uniqueBytes); + int len; + if(fileName.equals("thumb")) out.write(uniqueBytes); - while((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - // Complete the entry - out.closeEntry(); - in.close(); - - // Complete the ZIP file - out.close(); - - zipFile.delete(); - tempFile.renameTo(zipFile); - - toAdd.delete(); - } - - public static void removeFileFromZip(File zipFile, String toRemove) throws IOException { - // get a temp file - File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile()); - // delete it, otherwise you cannot rename your existing zip to it. - - byte[] buf = new byte[1024]; - - ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempFile)); - - ZipEntry entry = zin.getNextEntry(); - while(entry != null) { - String name = entry.getName(); - boolean isFile = name.equalsIgnoreCase(toRemove); - if(!isFile) { - // Add ZIP entry to output stream. - out.putNextEntry(new ZipEntry(name)); - // Transfer bytes from the ZIP file to the output file - int len; - while((len = zin.read(buf)) > 0) { - out.write(buf, 0, len); - } + while((len = in.read(buf)) > 0) { + out.write(buf, 0, len); } - entry = zin.getNextEntry(); + // Complete the entry + out.closeEntry(); + in.close(); + + toAdd.delete(); } - // Close the streams - zin.close(); // Complete the ZIP file out.close();