2012-05-01 00:48:59 +02:00
|
|
|
package net.shatteredlands.shatt.backup;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.config.Config;
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
2012-05-01 00:48:59 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
public class ZipLibrary {
|
2020-07-13 20:39:03 +02:00
|
|
|
private static final String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
|
|
|
|
private static final File BACKUP_DIR = new File(BACKUP_DIRECTORY);
|
|
|
|
private static final File FLAT_FILE_DIRECTORY = new File(mcMMO.getFlatFileDirectory());
|
|
|
|
private static final File MOD_FILE_DIRECTORY = new File(mcMMO.getModDirectory());
|
|
|
|
private static final File CONFIG_FILE = new File(mcMMO.getMainDirectory() + "config.yml");
|
|
|
|
private static final File EXPERIENCE_FILE = new File(mcMMO.getMainDirectory() + "experience.yml");
|
|
|
|
private static final File TREASURE_FILE = new File(mcMMO.getMainDirectory() + "treasures.yml");
|
|
|
|
private static final File ADVANCED_FILE = new File(mcMMO.getMainDirectory() + "advanced.yml");
|
|
|
|
private static final File REPAIR_FILE = new File(mcMMO.getMainDirectory() + "repair.vanilla.yml");
|
2013-11-01 16:28:42 +01: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 {
|
2013-11-01 16:28:42 +01:00
|
|
|
if (BACKUP_DIR.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");
|
2013-11-01 16:28:42 +01:00
|
|
|
File fileZip = new File(BACKUP_DIRECTORY + File.separator + dateFormat.format(date) + ".zip");
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
// Create the Source List, and add directories/etc to the file.
|
2020-07-13 21:31:30 +02:00
|
|
|
List<File> sources = new ArrayList<>();
|
2013-02-15 13:53:25 +01:00
|
|
|
|
2013-11-01 16:28:42 +01:00
|
|
|
sources.add(FLAT_FILE_DIRECTORY);
|
|
|
|
sources.add(CONFIG_FILE);
|
|
|
|
sources.add(EXPERIENCE_FILE);
|
|
|
|
sources.add(TREASURE_FILE);
|
|
|
|
sources.add(ADVANCED_FILE);
|
|
|
|
sources.add(REPAIR_FILE);
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2013-11-01 16:28:42 +01:00
|
|
|
if (MOD_FILE_DIRECTORY.exists()) {
|
|
|
|
sources.add(MOD_FILE_DIRECTORY);
|
2012-05-27 16:23:13 +02:00
|
|
|
}
|
|
|
|
|
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];
|
2013-11-01 16:28:42 +01:00
|
|
|
int byteCount;
|
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();
|
|
|
|
}
|
|
|
|
}
|