ZipLibrary.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 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. if (BackupDir.mkdir()) {
  23. try {
  24. mcMMO.p.getLogger().info("Created Backup Directory.");
  25. } catch (Exception e) {
  26. mcMMO.p.getLogger().severe(e.toString());
  27. }
  28. }
  29. //Generate the proper date for the backup filename
  30. Date date = new Date();
  31. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
  32. File fileZip = new File(BackupDirectory + File.separator + dateFormat.format(date) + ".zip");
  33. //Create the Source List, and add directories/etc to the file.
  34. List<File> sources = new ArrayList<File>();
  35. sources.add(FlatFileDirectory);
  36. sources.add(UsersFile);
  37. sources.add(ConfigFile);
  38. sources.add(Leaderboards);
  39. //Actually do something
  40. System.out.println("Backing up your mcMMO Configuration... ");
  41. packZip(fileZip, sources);
  42. }
  43. private static void packZip(File output, List<File> sources) throws IOException
  44. {
  45. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
  46. zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
  47. for (File source : sources) {
  48. if (source.isDirectory()) {
  49. zipDir(zipOut, "", source);
  50. }
  51. else {
  52. zipFile(zipOut, "", source);
  53. }
  54. }
  55. zipOut.flush();
  56. zipOut.close();
  57. System.out.println("Backup Completed.");
  58. }
  59. private static String buildPath(String path, String file) {
  60. if (path == null || path.isEmpty()) {
  61. return file;
  62. }
  63. else {
  64. return path + "/" + file;
  65. }
  66. }
  67. private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
  68. if (!dir.canRead()) {
  69. System.out.println("Cannot read " + dir.getCanonicalPath() + " (maybe because of permissions)");
  70. return;
  71. }
  72. File[] files = dir.listFiles();
  73. path = buildPath(path, dir.getName());
  74. for (File source : files) {
  75. if (source.isDirectory()) {
  76. zipDir(zos, path, source);
  77. }
  78. else {
  79. zipFile(zos, path, source);
  80. }
  81. }
  82. }
  83. private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
  84. if (!file.canRead()) {
  85. System.out.println("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
  86. return;
  87. }
  88. zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
  89. FileInputStream fis = new FileInputStream(file);
  90. byte[] buffer = new byte[4092];
  91. int byteCount = 0;
  92. while ((byteCount = fis.read(buffer)) != -1) {
  93. zos.write(buffer, 0, byteCount);
  94. }
  95. fis.close();
  96. zos.closeEntry();
  97. }
  98. }