Unzip and load world to directory

Discussion in 'Spigot Plugin Development' started by MCCoding, Jun 23, 2016.

  1. I've been looking at this for over an hour now and cannot grasp on why it's not working, I'm just trying to unzip a world from a zip file located elsewhere and pasting the contents into a directory where I can tell spigot to load it. It seems to load some of the files fine, but as it gets the the data folder it just throws FileNotFoundExceptions.

    For some reason that I can't figure why is that the data file in the file that is getting pasted into the new director is a less2pay file and not a directory like it should be, where it's a directory in the zip file i'm extracting. It seems extract all the other files fine except the data director for some reason.

    If anyone knows of another way to do this or can see or have any idea on why it's not working I would appreciate your thoughts, here is the code that I currently have

    Unzip Method
    Code (Text):

        public static void unzipToDirectory(String zipFile, String outputFolder) {
            byte[] buffer = new byte[1024];
            try {

                //create output directory is not exists
                File folder = new File(outputFolder);
                if (!folder.exists()) {
                    folder.mkdir();
                }

                //get the zip file content
                ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
                //get the zipped file list entry
                ZipEntry ze = zis.getNextEntry();

                while (ze != null) {

                    String fileName = ze.getName();
                    File newFile = new File(outputFolder + File.separator + fileName);
                    System.out.println("file unzip : " + newFile.getAbsoluteFile());

                    //create all non exists folders
                    //else you will hit FileNotFoundException for compressed folder
                    new File(newFile.getParent()).mkdirs();

                    FileOutputStream fos = new FileOutputStream(newFile);

                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }

                    fos.close();
                    ze = zis.getNextEntry();
                }

                zis.closeEntry();
                zis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
     

    Execution Line

    Code (Text):
    UtilZip.unzipToDirectory(Server.getInstance().getDataFolder() + "/maps/worlds/" + getName() + ".zip", getFolder());


    getName() is equal to the name of the map/zip file I'm trying to unzip
    getFolder() is the name of the folder that I'm unzipping into

    Stacktrace
    Code (Text):
    [13:23:57] [Craft Scheduler Thread - 3/INFO]: file unzip : E:\Coding\Custom plugin server 1.4.7\Map-1\Kingdom\data
    [13:23:57] [Craft Scheduler Thread - 3/INFO]: file unzip : E:\Coding\Custom plugin server 1.4.7\Map-1\Kingdom\data\Monument.dat
    [13:23:57] [Craft Scheduler Thread - 3/WARN]: java.io.FileNotFoundException: E:\Coding\Custom plugin server 1.4.7\Map-1\Kingdom\data\Monument.dat (The system cannot find the path specified)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.io.FileOutputStream.open(Native Method)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.io.FileOutputStream.<init>(Unknown Source)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.io.FileOutputStream.<init>(Unknown Source)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at com.maploader.core.common.util.UtilZip.unzipToDirectory(UtilZip.java:30)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at com.maploader.server.world.WorldData.unzipData(WorldData.java:107)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at com.maploader.server.world.WorldData$1.run(WorldData.java:114)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at org.bukkit.craftbukkit.v1_9_R2.scheduler.CraftTask.run(CraftTask.java:71)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at org.bukkit.craftbukkit.v1_9_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    [13:23:57] [Craft Scheduler Thread - 3/WARN]:     at java.lang.Thread.run(Unknown Source)


    UtilZip.java:30

    Code (Text):
      FileOutputStream fos = new FileOutputStream(newFile);

    WorldData.java:107

    Code (Text):
    UtilZip.unzipToDirectory(Server.getInstance().getDataFolder() + "/maps/worlds/" + getName() + ".zip", getFolder());
     
  2. Pretty sure that you aren't able to access folders outside the server directory (could be completely wrong, but I remember reading this somewhere).
     
  3. @MCCoding what is the file structure of your zip file? And in what path are you trying to extract it?

    Also, you seem to have forgotten to create the files themselves (you only create the parent directory atm)
     
  4. The zip file is just the world folder and all the data within that the world directory itself, so I've just right clicked and compressed it to a zip. The path that the zip file is extracting to the directory where spigot is located so where all the other worlds are, end nether etc.

    It should too be creating all the other files, just under the file output stream, at least I think it as it seems to be creating the session file and uid file of the worlds.

    EDIT; Here is where I got my unzip method from if that is any help, so I only assume it works, every other upzip methods that I had got gave me the same error.
     
  5. Incorrect. The plugin runs on the server, the server can access any file that the current user has access to which means that the plugin can access any file that the user has access to.