ZipLibrary.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package net.shatteredlands.shatt.backup;
  2. import com.gmail.nossr50.config.Config;
  3. import com.gmail.nossr50.config.MainConfig;
  4. import com.gmail.nossr50.mcMMO;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. import java.util.zip.Deflater;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipOutputStream;
  16. public class ZipLibrary {
  17. private static String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
  18. private static File BACKUP_DIR = new File(BACKUP_DIRECTORY);
  19. private static File FLAT_FILE_DIRECTORY = new File(mcMMO.getFlatFileDirectory());
  20. private static File MOD_FILE_DIRECTORY = new File(mcMMO.getModDirectory());
  21. /* private static File CONFIG_FILE = new File(mcMMO.getMainDirectory() + "config.yml");
  22. private static File EXPERIENCE_FILE = new File(mcMMO.getMainDirectory() + "experience.yml");
  23. //private static File TREASURE_FILE = new File(mcMMO.getMainDirectory() + "treasures.yml");
  24. private static File ADVANCED_FILE = new File(mcMMO.getMainDirectory() + "advanced.yml");
  25. private static File REPAIR_FILE = new File(mcMMO.getMainDirectory() + "repair.vanilla.yml");*/
  26. public static void mcMMOBackup() throws IOException {
  27. if (MainConfig.getInstance().getUseMySQL()) {
  28. mcMMO.p.debug("This server is running in SQL Mode.");
  29. mcMMO.p.debug("Only config files will be backed up.");
  30. }
  31. try {
  32. if (BACKUP_DIR.mkdir()) {
  33. mcMMO.p.debug("Created Backup Directory.");
  34. }
  35. }
  36. catch (Exception e) {
  37. mcMMO.p.getLogger().severe(e.toString());
  38. }
  39. // Generate the proper date for the backup filename
  40. Date date = new Date();
  41. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
  42. File fileZip = new File(BACKUP_DIRECTORY + File.separator + dateFormat.format(date) + ".zip");
  43. // Create the Source List, and add directories/etc to the file.
  44. List<File> sources = new ArrayList<File>();
  45. sources.add(FLAT_FILE_DIRECTORY);
  46. sources.addAll(mcMMO.getConfigManager().getConfigFiles()); //Config File Backups
  47. if (MOD_FILE_DIRECTORY.exists()) {
  48. sources.add(MOD_FILE_DIRECTORY);
  49. }
  50. // Actually do something
  51. mcMMO.p.debug("Backing up your mcMMO Configuration... ");
  52. packZip(fileZip, sources);
  53. }
  54. private static void packZip(File output, List<File> sources) throws IOException {
  55. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
  56. zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
  57. for (File source : sources) {
  58. if (source.isDirectory()) {
  59. zipDir(zipOut, "", source);
  60. }
  61. else {
  62. zipFile(zipOut, "", source);
  63. }
  64. }
  65. zipOut.flush();
  66. zipOut.close();
  67. mcMMO.p.debug("Backup Completed.");
  68. }
  69. private static String buildPath(String path, String file) {
  70. if (path == null || path.isEmpty()) {
  71. return file;
  72. }
  73. return path + File.separator + file;
  74. }
  75. private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
  76. if (!dir.canRead()) {
  77. mcMMO.p.getLogger().severe("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)");
  78. return;
  79. }
  80. File[] files = dir.listFiles();
  81. path = buildPath(path, dir.getName());
  82. for (File source : files) {
  83. if (source.isDirectory()) {
  84. zipDir(zos, path, source);
  85. }
  86. else {
  87. zipFile(zos, path, source);
  88. }
  89. }
  90. }
  91. private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
  92. if (!file.canRead()) {
  93. mcMMO.p.getLogger().severe("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
  94. return;
  95. }
  96. zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
  97. FileInputStream fis = new FileInputStream(file);
  98. byte[] buffer = new byte[4092];
  99. int byteCount;
  100. while ((byteCount = fis.read(buffer)) != -1) {
  101. zos.write(buffer, 0, byteCount);
  102. }
  103. fis.close();
  104. zos.closeEntry();
  105. }
  106. }