mcMMO/src/main/java/net/shatteredlands/shatt/backup/ZipLibrary.java

135 lines
4.5 KiB
Java
Raw Normal View History

2012-05-01 00:48:59 +02:00
package net.shatteredlands.shatt.backup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
2012-05-01 07:40:47 +02:00
2012-05-01 00:48:59 +02:00
public class ZipLibrary {
2012-07-09 16:55:33 +02:00
private static String BackupDirectory = mcMMO.getMainDirectory() + "backup" + File.separator;
2012-05-01 01:32:50 +02:00
private static File BackupDir = new File(BackupDirectory);
2012-07-09 16:55:33 +02:00
private static File FlatFileDirectory = new File(mcMMO.getFlatFileDirectory());
private static File ModFileDirectory = new File(mcMMO.getModDirectory());
private static File ConfigFile = new File(mcMMO.getMainDirectory() + "config.yml");
private static File TreasuresFile = new File(mcMMO.getMainDirectory() + "treasures.yml");
private static File AdvancedConfigFile = new File(mcMMO.getMainDirectory() + "advanced.yml");
private static File SpoutFile = new File(mcMMO.getMainDirectory() + "spout.yml");
private static File RepairFile = new File(mcMMO.getMainDirectory() + "repair.vanilla.yml");
2012-05-01 07:43:00 +02:00
2012-05-01 01:32:50 +02:00
public static void mcMMObackup() throws IOException {
if (Config.getInstance().getUseMySQL()) {
2013-02-13 04:18:47 +01:00
mcMMO.p.getLogger().info("No Backup performed, in SQL Mode.");
return;
}
try {
if (BackupDir.mkdir()) {
mcMMO.p.getLogger().info("Created Backup Directory.");
2012-05-01 01:32:50 +02:00
}
} catch (Exception e) {
mcMMO.p.getLogger().severe(e.toString());
}
2012-05-01 01:32:50 +02:00
//Generate the proper date for the backup filename
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
File fileZip = new File(BackupDirectory + File.separator + dateFormat.format(date) + ".zip");
//Create the Source List, and add directories/etc to the file.
List<File> sources = new ArrayList<File>();
sources.add(FlatFileDirectory);
sources.add(ConfigFile);
sources.add(TreasuresFile);
sources.add(AdvancedConfigFile);
sources.add(RepairFile);
2012-05-01 01:32:50 +02:00
if (ModFileDirectory.exists()) {
sources.add(ModFileDirectory);
}
if (SpoutFile.exists()) {
sources.add(SpoutFile);
}
2012-05-01 01:32:50 +02:00
//Actually do something
2013-02-13 04:18:47 +01:00
mcMMO.p.getLogger().info("Backing up your mcMMO Configuration... ");
2012-05-01 01:32:50 +02:00
packZip(fileZip, sources);
}
private static void packZip(File output, List<File> sources) throws IOException {
2012-05-01 00:48:59 +02:00
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
for (File source : sources) {
if (source.isDirectory()) {
zipDir(zipOut, "", source);
2012-05-01 01:32:50 +02:00
}
else {
2012-05-01 00:48:59 +02:00
zipFile(zipOut, "", source);
}
}
2012-05-01 01:32:50 +02:00
2012-05-01 00:48:59 +02:00
zipOut.flush();
zipOut.close();
2013-02-13 04:18:47 +01:00
mcMMO.p.getLogger().info("Backup Completed.");
2012-05-01 00:48:59 +02:00
}
private static String buildPath(String path, String file) {
if (path == null || path.isEmpty()) {
return file;
2012-05-01 01:32:50 +02:00
}
2013-01-10 04:43:21 +01:00
return path + File.separator + file;
2012-05-01 00:48:59 +02:00
}
private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
if (!dir.canRead()) {
2013-02-13 04:18:47 +01:00
mcMMO.p.getLogger().severe("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)");
2012-05-01 00:48:59 +02:00
return;
}
File[] files = dir.listFiles();
path = buildPath(path, dir.getName());
for (File source : files) {
if (source.isDirectory()) {
zipDir(zos, path, source);
2012-05-01 01:32:50 +02:00
}
else {
2012-05-01 00:48:59 +02:00
zipFile(zos, path, source);
}
}
}
private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
if (!file.canRead()) {
2013-02-13 04:18:47 +01:00
mcMMO.p.getLogger().severe("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
2012-05-01 00:48:59 +02:00
return;
}
2012-05-01 01:32:50 +02:00
2012-05-01 00:48:59 +02:00
zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4092];
int byteCount = 0;
2012-06-05 15:57:10 +02:00
2012-05-01 00:48:59 +02:00
while ((byteCount = fis.read(buffer)) != -1) {
zos.write(buffer, 0, byteCount);
}
fis.close();
zos.closeEntry();
}
}