Moving a file from jar to the plugin folder

Discussion in 'Plugin Development' started by Briggybros, Oct 10, 2013.

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

    Briggybros

    Hello, I'm trying to move a file from my plugin jar to the plugin folder. The file is a schematic, so that users can customise their experience to great extent. This is the code I use:
    Code:java
    1. public static void loadTemplates() {
    2. try {
    3. File fDoor = new File(DQ.templatesFolder,
    4. "dqdoor.schematic");
    5. if (fDoor.exists()) {
    6. door = loadSchematic(fDoor);
    7. } else {
    8. InputStream in = DQ.class.getResourceAsStream("dqdoor.schematic");
    9. if (in == null)
    10. System.out.println("Input is null");
    11. fDoor.createNewFile();
    12. OutputStream out = new FileOutputStream(fDoor);
    13.  
    14. int read = 0;
    15. byte[] bytes = new byte[1024];
    16.  
    17. while ((read = in.read(bytes)) != -1) { //this line
    18. out.write(bytes, 0, read);
    19. }
    20.  
    21. in.close();
    22. out.flush();
    23. out.close();
    24.  
    25. System.out.println("Door output to " + fDoor.getAbsolutePath());
    26. door = loadSchematic(fDoor);
    27.  
    28. }
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }


    However, I get an exception on the line shown above.
    Code:
    2013-10-10 23:40:36 [SEVERE] java.util.zip.ZipException: invalid LOC header (bad signature)
    2013-10-10 23:40:36 [SEVERE]    at java.util.zip.ZipFile.read(Native Method)
    2013-10-10 23:40:36 [SEVERE]    at java.util.zip.ZipFile.access$1400(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at java.util.zip.ZipFile$ZipFileInputStream.read(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at java.util.zip.InflaterInputStream.read(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at java.io.FilterInputStream.read(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at java.io.FilterInputStream.read(Unknown Source)
    2013-10-10 23:40:36 [SEVERE]    at com.toomuchminecraft.dq.Templates.loadTemplates(Templates.java:135)
    2013-10-10 23:40:36 [SEVERE]    at com.toomuchminecraft.dq.DQ.loadConfig(DQ.java:48)
    2013-10-10 23:40:36 [SEVERE]    at com.toomuchminecraft.dq.DQ.onEnable(DQ.java:32)
    2013-10-10 23:40:36 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    2013-10-10 23:40:36 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    2013-10-10 23:40:36 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:282)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:264)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:609)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.Bukkit.reload(Bukkit.java:277)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    2013-10-10 23:40:37 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:512)
    2013-10-10 23:40:37 [SEVERE]    at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:263)
    2013-10-10 23:40:37 [SEVERE]    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:228)
    2013-10-10 23:40:37 [SEVERE]    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    2013-10-10 23:40:37 [SEVERE]    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    2013-10-10 23:40:37 [SEVERE]    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    The method is called on enable and the directory that it's going into exists. What could be going wrong? I've used this method before with zip files with success, what about the bukkit server prevents this?
     
  2. From your main plugin class, you can call a method from the Bukkit API called "saveResource", which does that job for you so you don't have to worry about streams and stuff.
    I have never used it but that's kind of its purpose, so you could try it.

    The thrown exception is about something different, though. It looks like Java is not able to parse your JAR file properly. A google search for the exception message shows this: http://stackoverflow.com/questions/3804505/reading-zip-file-gives-an-invalid-loc-header-exception

    It might as well just be your way of exporting your JAR file. Are you using some kind of fancy program for that? Maybe it uses a non-standard format that Java has some problems with.
     
  3. Offline

    Briggybros

    Bone008 Have you got JAR and ZIP mixed up or do you actaully mean it's a problem with the jar? (If so I'm just exporting with eclipse)
     
  4. A JAR and ZIP file actually have the same format, the JAR file can just contain additional metadata in the META-INF folder. Parsing either of them is essentially the same thing, there isn't really a technical difference.

    So yes, I've been talking about the JAR file, which I assume contains your resource file.
     
  5. Offline

    Briggybros

    Fixed the problem, it must have been my way of moving it out of the jar, the bukkit's saveResource method made everything work :)
     
Thread Status:
Not open for further replies.

Share This Page