ZipLibrary.java 3.6 KB

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