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;
|
|
|
|
|
2012-06-07 00:02:22 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2012-05-01 09:07:17 +02:00
|
|
|
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");
|
2013-02-06 01:02:56 +01:00
|
|
|
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 {
|
2012-05-01 13:54:37 +02:00
|
|
|
if (Config.getInstance().getUseMySQL()) {
|
2013-04-13 21:53:00 +02:00
|
|
|
mcMMO.p.debug("This server is running in SQL Mode.");
|
|
|
|
mcMMO.p.debug("Only config files will be backed up.");
|
2012-05-01 13:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (BackupDir.mkdir()) {
|
2013-04-13 21:53:00 +02:00
|
|
|
mcMMO.p.debug("Created Backup Directory.");
|
2012-05-01 01:32:50 +02:00
|
|
|
}
|
2013-02-15 13:53:25 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2012-06-07 00:02:22 +02:00
|
|
|
mcMMO.p.getLogger().severe(e.toString());
|
2012-05-01 13:54:37 +02:00
|
|
|
}
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// Generate the proper date for the backup filename
|
2012-05-01 01:32:50 +02:00
|
|
|
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");
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// Create the Source List, and add directories/etc to the file.
|
2012-05-01 01:32:50 +02:00
|
|
|
List<File> sources = new ArrayList<File>();
|
2013-02-15 13:53:25 +01:00
|
|
|
if (!Config.getInstance().getUseMySQL()) {
|
|
|
|
sources.add(FlatFileDirectory);
|
|
|
|
}
|
|
|
|
|
2012-05-01 01:32:50 +02:00
|
|
|
sources.add(ConfigFile);
|
2012-05-01 14:28:54 +02:00
|
|
|
sources.add(TreasuresFile);
|
2013-02-06 01:02:56 +01:00
|
|
|
sources.add(AdvancedConfigFile);
|
|
|
|
sources.add(RepairFile);
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2012-05-27 16:23:13 +02:00
|
|
|
if (ModFileDirectory.exists()) {
|
|
|
|
sources.add(ModFileDirectory);
|
|
|
|
}
|
|
|
|
|
2013-02-06 01:02:56 +01:00
|
|
|
if (SpoutFile.exists()) {
|
|
|
|
sources.add(SpoutFile);
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// Actually do something
|
2013-04-13 21:53:00 +02:00
|
|
|
mcMMO.p.debug("Backing up your mcMMO Configuration... ");
|
2012-05-01 01:32:50 +02:00
|
|
|
|
|
|
|
packZip(fileZip, sources);
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:54:37 +02:00
|
|
|
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-04-13 21:53:00 +02:00
|
|
|
mcMMO.p.debug("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();
|
|
|
|
}
|
|
|
|
}
|