Embedded resource folder listing?

Discussion in 'Plugin Development' started by timsavage, Jun 13, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    Is it possible to get a list of the files/folders contained in a folder that is an embedded resource in my jar file?

    I'm implementing language localization in my plugin, and I have a folder named language in my jar, that contains sub-folders for each translation, such as 'en-US' and 'es-ES', which in turn contain a messages.yml file with the actual strings that are used in various messages output by the plugin.

    I'd like to install any new sub-folders that don't already exist on loading of the plugin, without overwriting the existing files which may have been customized by the server admin.
  2. timsavage You want to save a resource without overriding the files already there?

    Hmm, how do we save the resource again? Ah, here it is in Plugin javadocs. Okay, it seems to take a String and a boolean. What do these do?

    resourcePath the embedded resource path to look for within the plugin's .jar file. (No preceding slash).
    That seems reasonable enough for the String. Now what does the boolean do?

    if true, the embedded resource will overwrite the contents of an existing file.

  3. Offline


    I actually do know how to read javadocs, but thanks. I'm able to write the files just fine, and pass false as the second parameter to prevent overwriting existing files.

    If you read my post more carefully, you will see that what I am actually asking is if it is possible to get a list of subfolders contained within a folder that is an embedded resource. Perhaps you overlooked the first line of my post.

    To clarify, I have a folder as an embedded resource, named 'language'. Within this folder are a number of subfolders, such as 'en-US', 'es-ES', 'de-DE', etc. Within each of these folders is a yaml file, 'messages.yml', that contains the strings that are the various messages the plugin outputs. Each separate one contains the translated versions of the messages for the language/localization specified by the subfolder name.

    I would like to get a list of the first level of subfolders contained in the 'language' folder. Then I can loop through each one, checking if it is already installed, and if not, install it. This way, subsequent versions of my plugin may contain additional translations, which will then be installed, but the existing files, which may have been modified by the server admin, will not be overwritten.

    If I just try to write the entire 'language' folder using saveResource(), it will either overwrite everything, or write nothing, depending on the 'replace' value being true of false. I would like to only write the subfolders and their contents that do not already exist.

    I already have this working, but I have to define the list of subfolders manually in my code. I am simply looking for a way to read the names of the subfolders into a list, so I won't have to remember to update the manually coded list every time a new translation is added and a new version of the plugin is released.
  4. I don't think the bukkit API exposes a direct way to do this.

    However, Java has an API for reading zip files (which a .jar is), and JavaPlugin has the method getFile() to get a File object for the plugin's jar.

    Using this, you should be able to read the plugin jar and get a list of the directories.
  5. Offline


    I made a resource extractor some time ago, I made som changes so let me know if there are bugs. ;)

    Here a usage example:
    1. // Only files that end with "txt" will be extracted.
    2. ResourceExtractor extract = new ResourceExtractor(this, new File(this.getDataFolder() + File.separator + "lang"), "language", ".*\\.(txt)$");
    4. try {
    5. extract.extract();
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
Thread Status:
Not open for further replies.

Share This Page