ZipLibrary.java 4.3 KB

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