ZipLibrary.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 static String BackupDirectory = mcMMO.mainDirectory + "backup" + File.separator;
  17. private static File BackupDir = new File(BackupDirectory);
  18. private static File FlatFileDirectory = new File(mcMMO.flatFileDirectory);
  19. private static File ModFileDirectory = new File(mcMMO.modDirectory);
  20. private static File UsersFile = new File(mcMMO.usersFile);
  21. private static File ConfigFile = new File(mcMMO.mainDirectory + "config.yml");
  22. private static File TreasuresFile = new File(mcMMO.mainDirectory + "treasures.yml");
  23. private static File Leaderboards = new File(mcMMO.leaderboardDirectory);
  24. public static void mcMMObackup() throws IOException {
  25. if (Config.getInstance().getUseMySQL()) {
  26. System.out.println("No Backup performed, in SQL Mode.");
  27. return;
  28. }
  29. try {
  30. if (BackupDir.mkdir()) {
  31. mcMMO.p.getLogger().info("Created Backup Directory.");
  32. }
  33. } catch (Exception e) {
  34. mcMMO.p.getLogger().severe(e.toString());
  35. }
  36. //Generate the proper date for the backup filename
  37. Date date = new Date();
  38. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
  39. File fileZip = new File(BackupDirectory + File.separator + dateFormat.format(date) + ".zip");
  40. //Create the Source List, and add directories/etc to the file.
  41. List<File> sources = new ArrayList<File>();
  42. sources.add(FlatFileDirectory);
  43. sources.add(UsersFile);
  44. sources.add(ConfigFile);
  45. sources.add(TreasuresFile);
  46. sources.add(Leaderboards);
  47. if (ModFileDirectory.exists()) {
  48. sources.add(ModFileDirectory);
  49. }
  50. //Actually do something
  51. System.out.println("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. System.out.println("Backup Completed.");
  68. }
  69. private static String buildPath(String path, String file) {
  70. if (path == null || path.isEmpty()) {
  71. return file;
  72. }
  73. else {
  74. return path + File.separator + file;
  75. }
  76. }
  77. private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
  78. if (!dir.canRead()) {
  79. System.out.println("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)");
  80. return;
  81. }
  82. File[] files = dir.listFiles();
  83. path = buildPath(path, dir.getName());
  84. for (File source : files) {
  85. if (source.isDirectory()) {
  86. zipDir(zos, path, source);
  87. }
  88. else {
  89. zipFile(zos, path, source);
  90. }
  91. }
  92. }
  93. private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
  94. if (!file.canRead()) {
  95. System.out.println("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
  96. return;
  97. }
  98. zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
  99. FileInputStream fis = new FileInputStream(file);
  100. byte[] buffer = new byte[4092];
  101. int byteCount = 0;
  102. while ((byteCount = fis.read(buffer)) != -1) {
  103. zos.write(buffer, 0, byteCount);
  104. }
  105. fis.close();
  106. zos.closeEntry();
  107. }
  108. }