Advertisement
Guest User

Resource Extractor

a guest
Jun 14th, 2014
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /**
  2. *
  3. * This software is part of the TheEnderBox
  4. *
  5. * TheEnderBox is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * any later version.
  9. *
  10. * TheEnderBox is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with TheEnderBox. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package me.cybermaxke.enderbox.api.resource;
  20.  
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.lang.reflect.Method;
  26. import java.util.Enumeration;
  27. import java.util.jar.JarEntry;
  28. import java.util.jar.JarFile;
  29.  
  30. import org.apache.commons.lang.Validate;
  31.  
  32. import org.bukkit.plugin.java.JavaPlugin;
  33.  
  34. public final class ResourceExtractor {
  35. protected final JavaPlugin plugin;
  36. protected final File extractfolder;
  37.  
  38. protected final String folderpath;
  39. protected final String regex;
  40.  
  41. /**
  42. * You can extract complete folders of resources from your plugin jar to a target folder. You
  43. * can append a regex to match the file names.
  44. * @param plugin The plugin the files will be extracted from.
  45. * @param extractfolder The folder where the files will be extracted to.
  46. * @param folderpath The path where the files are inside in the jar located.
  47. * @param regex A regex to match the file names. This can be 'null' if you don't want to use it.
  48. */
  49. public ResourceExtractor(JavaPlugin plugin, File extractfolder, String folderpath, String regex) {
  50. Validate.notNull(plugin, "The plugin cannot be null!");
  51. Validate.notNull(plugin, "The extract folder cannot be null!");
  52. Validate.notNull(plugin, "The folder path cannot be null!");
  53.  
  54. this.extractfolder = extractfolder;
  55. this.folderpath = folderpath;
  56. this.plugin = plugin;
  57. this.regex = regex;
  58. }
  59.  
  60. /**
  61. * Starts extracting the files.
  62. * @throws IOException
  63. */
  64. public void extract() throws IOException {
  65. this.extract(false, true);
  66. }
  67.  
  68. /**
  69. * Starts extracting the files.
  70. * @param override Whether you want to override the old files.
  71. * @throws IOException
  72. */
  73. public void extract(boolean override) throws IOException {
  74. this.extract(override, true);
  75. }
  76.  
  77. /**
  78. * Starts extracting the files.
  79. * @param override Whether you want to override the old files.
  80. * @param subpaths Whether you want to create sub folders if it's also found in the jar file.
  81. * @throws IOException
  82. */
  83. public void extract(boolean override, boolean subpaths) throws IOException {
  84. File jarfile = null;
  85.  
  86. /**
  87. * Get the jar file from the plugin.
  88. */
  89. try {
  90. Method method = JavaPlugin.class.getDeclaredMethod("getFile");
  91. method.setAccessible(true);
  92.  
  93. jarfile = (File) method.invoke(this.plugin);
  94. } catch (Exception e) {
  95. throw new IOException(e);
  96. }
  97.  
  98. /**
  99. * Make the folders if missing.
  100. */
  101. if (!this.extractfolder.exists()) {
  102. this.extractfolder.mkdirs();
  103. }
  104.  
  105. JarFile jar = new JarFile(jarfile);
  106.  
  107. /**
  108. * Loop through all the entries.
  109. */
  110. Enumeration<JarEntry> entries = jar.entries();
  111. while (entries.hasMoreElements()) {
  112. JarEntry entry = entries.nextElement();
  113. String path = entry.getName();
  114.  
  115. /**
  116. * Not in the folder.
  117. */
  118. if (!path.startsWith(this.folderpath)) {
  119. continue;
  120. }
  121.  
  122. if (entry.isDirectory()) {
  123. if (subpaths) {
  124. File file = new File(this.extractfolder, entry.getName().replaceFirst(this.folderpath, ""));
  125.  
  126. if (!file.exists()) {
  127. file.mkdirs();
  128. }
  129. }
  130. } else {
  131. File file;
  132.  
  133. /**
  134. * Use the right path.
  135. */
  136. if (subpaths) {
  137. file = new File(this.extractfolder, path.replaceFirst(this.folderpath, ""));
  138. } else {
  139. file = new File(this.extractfolder, path.substring(path.indexOf(File.separatorChar), path.length()));
  140. }
  141.  
  142. String name = file.getName();
  143.  
  144. /**
  145. * Be sure that the file is valid.
  146. */
  147. if (this.regex == null || name.matches(this.regex)) {
  148. if (file.exists() && override) {
  149. file.delete();
  150. }
  151.  
  152. if (!file.exists()) {
  153. /**
  154. * Copy the file to the path.
  155. */
  156. InputStream is = jar.getInputStream(entry);
  157. FileOutputStream fos = new FileOutputStream(file);
  158.  
  159. while (is.available() > 0) {
  160. fos.write(is.read());
  161. }
  162.  
  163. fos.close();
  164. is.close();
  165. }
  166. }
  167. }
  168. }
  169.  
  170. jar.close();
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement