Advertisement
Guest User

Libgdx Jar Ressource walker

a guest
Sep 25th, 2018
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. /**
  2. * This class helps to get a list of FileHandle inside the project assets folder (when the project is running from within a Jar).
  3. * See {@link #listFromJarIfNecessary(String)} or {@link #listFromJarIfNecessary(JarFileFilter)}
  4. */
  5. public class JarUtils {
  6.  
  7. private static final String JAR_EXTENSION = ".jar";
  8.  
  9. public static FileHandle[] listFromJarIfNecessary(String folderPath) {
  10. return listFromJarIfNecessary(new JarFileFilter(folderPath));
  11. }
  12.  
  13. public static FileHandle[] listFromJarIfNecessary(JarFileFilter jarFileFilter) {
  14. if (shouldLoadFromJar()) {
  15. Array<FileHandle> files = getFileHandleArrayList(jarFileFilter);
  16. return getFileHandlesArray(files);
  17. } else {
  18. // Simply load it the libGDX way.
  19. return Gdx.files.internal(jarFileFilter.getFolderPath()).list(jarFileFilter);
  20. }
  21. }
  22.  
  23. /**
  24. * @return True if the project runs of desktop and if it is run from a jar file false otherwise.
  25. */
  26. private static boolean shouldLoadFromJar() {
  27. return (Gdx.app.getType() == Application.ApplicationType.Desktop || Gdx.app.getType() == Application.ApplicationType.HeadlessDesktop)
  28. && getJarFile() != null;
  29. }
  30.  
  31. /**
  32. * @return The project jar file if any, null otherwise.
  33. */
  34. private static File getJarFile() {
  35. try {
  36. File jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI());
  37. if (jarFile.getAbsolutePath().endsWith(JAR_EXTENSION)) {
  38. return jarFile; // Only return the file if it is a jar file.
  39. }
  40. } catch (URISyntaxException e) {
  41. e.printStackTrace();
  42. }
  43. return null;
  44. }
  45.  
  46. /**
  47. * @param jarFileFilter The filter for the jar files.
  48. * @return An Array object filled with the FileHandle matching the given filter.
  49. */
  50. private static Array<FileHandle> getFileHandleArrayList(JarFileFilter jarFileFilter) {
  51. File jarFile = getJarFile();
  52. if (jarFile == null) return new Array<>(); // We check even if this should never happen.
  53.  
  54. Array<FileHandle> files = new Array<>();
  55. try (ZipFile zf = new ZipFile(jarFile.getAbsoluteFile())) {
  56. Enumeration<? extends ZipEntry> e = zf.entries();
  57. String filePrefixPath = jarFileFilter.getFolderPath();
  58. while (e.hasMoreElements()) { // Go through all the entries.
  59. ZipEntry zipEntry = e.nextElement();
  60. if (zipEntry.getName().length() <= jarFileFilter.getFolderPath().length())
  61. continue;// Skip path that are not long enough to match the folder name.
  62.  
  63. String simpleName = zipEntry.getName().substring(filePrefixPath.length()); // Get the file name without the folder prefix.
  64. if (zipEntry.getName().startsWith(filePrefixPath) && jarFileFilter.acceptZipEntry(simpleName)) {
  65. // If the folder prefix match and the filter accept the file short name, we add it to the list.
  66. FileHandle internal = Gdx.files.internal(zipEntry.getName());
  67. files.add(internal);
  68. }
  69. }
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. return files;
  74. }
  75.  
  76. /**
  77. * Convert an Array object of FileHandle to an array structure of FileHandle.
  78. *
  79. * @param files An Array object filled with FileHandle.
  80. * @return A array structure of FileHandle.
  81. */
  82. private static FileHandle[] getFileHandlesArray(Array<FileHandle> files) {
  83. FileHandle fileHandle[] = new FileHandle[files.size];
  84. for (int i = 0; i < files.size; i++) {
  85. fileHandle[i] = files.get(i);
  86. }
  87. return fileHandle;
  88. }
  89.  
  90. /**
  91. * This class define a new filter method that will be used for filtering zip entries (file within your final jar).
  92. */
  93. public static class JarFileFilter implements FileFilter {
  94.  
  95. private static final String FOLDER_SEPARATOR = "/"; // To avoid getting map subfolders files.
  96.  
  97. /**
  98. * Because the jar entries displays the full path of the file (from within the jar), this folderPath helps you
  99. * to get the file short name. Ex: to list entries from the folder "folder1/folder2/" you should set this folderPath to
  100. * "folder1/folder2/" this will help you to strip this part from your file name in the acceptZipEntry method.
  101. */
  102. private final String folderPath;
  103. /**
  104. * Whether to accept the file located in a subfolder of the given folderPath.
  105. */
  106. private boolean acceptSubFoldersFiles;
  107.  
  108. public JarFileFilter(String folderPath, boolean acceptSubFoldersFiles) {
  109. this.folderPath = folderPath;
  110. this.acceptSubFoldersFiles = acceptSubFoldersFiles;
  111. }
  112.  
  113. public JarFileFilter(String folderPath) {
  114. this(folderPath, false);
  115. }
  116.  
  117. /**
  118. * This method will be called on zip entries when the project is run from a jar file.
  119. *
  120. * @param zipEntryShortName The short name for this file.
  121. * @return True if the file with this short name should be accepted.
  122. */
  123. public boolean acceptZipEntry(String zipEntryShortName) {
  124. if (acceptSubFoldersFiles) {
  125. return true;
  126. }
  127. // If we only accept file from the same directory then we should not encounter any folder separator character.
  128. return !zipEntryShortName.contains(FOLDER_SEPARATOR);
  129.  
  130. }
  131.  
  132. public String getFolderPath() {
  133. return folderPath;
  134. }
  135.  
  136. @Override
  137. public boolean accept(File pathname) {
  138. // This method will be called when the project is running outside a jar file so we accept all the file by default here.
  139. return true;
  140. }
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement