123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package net.shatteredlands.shatt.backup;
- import com.gmail.nossr50.config.Config;
- import com.gmail.nossr50.config.MainConfig;
- import com.gmail.nossr50.mcMMO;
- 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 {
- private static String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
- private static File BACKUP_DIR = new File(BACKUP_DIRECTORY);
- private static File FLAT_FILE_DIRECTORY = new File(mcMMO.getFlatFileDirectory());
- private static File MOD_FILE_DIRECTORY = new File(mcMMO.getModDirectory());
- /* private static File CONFIG_FILE = new File(mcMMO.getMainDirectory() + "config.yml");
- private static File EXPERIENCE_FILE = new File(mcMMO.getMainDirectory() + "experience.yml");
- //private static File TREASURE_FILE = new File(mcMMO.getMainDirectory() + "treasures.yml");
- private static File ADVANCED_FILE = new File(mcMMO.getMainDirectory() + "advanced.yml");
- private static File REPAIR_FILE = new File(mcMMO.getMainDirectory() + "repair.vanilla.yml");*/
- public static void mcMMOBackup() throws IOException {
- if (MainConfig.getInstance().getUseMySQL()) {
- mcMMO.p.debug("This server is running in SQL Mode.");
- mcMMO.p.debug("Only config files will be backed up.");
- }
- try {
- if (BACKUP_DIR.mkdir()) {
- mcMMO.p.debug("Created Backup Directory.");
- }
- }
- catch (Exception e) {
- mcMMO.p.getLogger().severe(e.toString());
- }
- // 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(BACKUP_DIRECTORY + 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(FLAT_FILE_DIRECTORY);
- sources.addAll(mcMMO.getConfigManager().getConfigFiles()); //Config File Backups
- if (MOD_FILE_DIRECTORY.exists()) {
- sources.add(MOD_FILE_DIRECTORY);
- }
- // Actually do something
- mcMMO.p.debug("Backing up your mcMMO Configuration... ");
- packZip(fileZip, sources);
- }
- private static void packZip(File output, List<File> sources) throws IOException {
- ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
- zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
- for (File source : sources) {
- if (source.isDirectory()) {
- zipDir(zipOut, "", source);
- }
- else {
- zipFile(zipOut, "", source);
- }
- }
- zipOut.flush();
- zipOut.close();
- mcMMO.p.debug("Backup Completed.");
- }
- private static String buildPath(String path, String file) {
- if (path == null || path.isEmpty()) {
- return file;
- }
- return path + File.separator + file;
- }
- private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
- if (!dir.canRead()) {
- mcMMO.p.getLogger().severe("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)");
- return;
- }
- File[] files = dir.listFiles();
- path = buildPath(path, dir.getName());
- for (File source : files) {
- if (source.isDirectory()) {
- zipDir(zos, path, source);
- }
- else {
- zipFile(zos, path, source);
- }
- }
- }
- private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
- if (!file.canRead()) {
- mcMMO.p.getLogger().severe("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
- return;
- }
- zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
- FileInputStream fis = new FileInputStream(file);
- byte[] buffer = new byte[4092];
- int byteCount;
- while ((byteCount = fis.read(buffer)) != -1) {
- zos.write(buffer, 0, byteCount);
- }
- fis.close();
- zos.closeEntry();
- }
- }
|