DRYed ReplayFileIO code to add/remove files from Replay File
This commit is contained in:
@@ -43,11 +43,7 @@ public class ReplayFileAppender extends Thread {
|
|||||||
if(mv != null) {
|
if(mv != null) {
|
||||||
if(mv.getRight().canWrite()) {
|
if(mv.getRight().canWrite()) {
|
||||||
try {
|
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) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
filesToMove.add(mv);
|
filesToMove.add(mv);
|
||||||
|
|||||||
@@ -301,11 +301,6 @@ public class ReplayFileIO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void addFileToZip(File zipFile, File toAdd, String fileName) throws IOException {
|
public static void addFileToZip(File zipFile, File toAdd, String fileName) throws IOException {
|
||||||
if(toAdd == null) {
|
|
||||||
removeFileFromZip(zipFile, fileName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get a temp file
|
// get a temp file
|
||||||
File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile());
|
File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile());
|
||||||
// delete it, otherwise you cannot rename your existing zip to it.
|
// delete it, otherwise you cannot rename your existing zip to it.
|
||||||
@@ -334,6 +329,7 @@ public class ReplayFileIO {
|
|||||||
zin.close();
|
zin.close();
|
||||||
// Compress the files
|
// Compress the files
|
||||||
|
|
||||||
|
if(toAdd != null) {
|
||||||
InputStream in = new FileInputStream(toAdd);
|
InputStream in = new FileInputStream(toAdd);
|
||||||
// Add ZIP entry to output stream.
|
// Add ZIP entry to output stream.
|
||||||
out.putNextEntry(new ZipEntry(fileName));
|
out.putNextEntry(new ZipEntry(fileName));
|
||||||
@@ -349,43 +345,9 @@ public class ReplayFileIO {
|
|||||||
out.closeEntry();
|
out.closeEntry();
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
// Complete the ZIP file
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
zipFile.delete();
|
|
||||||
tempFile.renameTo(zipFile);
|
|
||||||
|
|
||||||
toAdd.delete();
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entry = zin.getNextEntry();
|
|
||||||
}
|
|
||||||
// Close the streams
|
|
||||||
zin.close();
|
|
||||||
|
|
||||||
// Complete the ZIP file
|
// Complete the ZIP file
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user