extracting .zip files

Discussion in 'Plugin Development' started by supercas240, Sep 12, 2018.

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

    supercas240

    Okay I know this isn't really a bukkit question and more a generic java question but might as well ask ;)

    so basically the summery is that I'm extracting a .zip file from one folder into another. I'm using the java sample code for this, because i've never worked with java.util.zip before. it seems straightforward enough, but still my code is running into some weird issues

    okay so:
    Code:
    package com.supercas240.RaidEvents.Main;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class UnzipUtility {
        /**
         * Size of the buffer to read/write data
         */
        private static final int BUFFER_SIZE = 4096;
        /**
         * Extracts a zip file specified by the zipFilePath to a directory specified by
         * destDirectory (will be created if does not exists)
         * @param zipFilePath
         * @param destDirectory
         * @throws IOException
         */
        public void unzip(String zipFilePath, String destDirectory) throws IOException {
            File destDir = new File(destDirectory);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
           
            ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
            ZipEntry entry = zipIn.getNextEntry();
           
            // iterates over entries in the zip file
            while (entry != null) {
                String filePath = destDirectory + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, filePath);
                } else {
                    // if the entry is a directory, make the directory
                    File dir = new File(filePath);
                    dir.mkdir();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
            zipIn.close();
        }
        /**
         * Extracts a zip entry (file entry)
         * @param zipIn
         * @param filePath
         * @throws IOException
         */
        private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            byte[] bytesIn = new byte[BUFFER_SIZE];
            int read = 0;
            while ((read = zipIn.read(bytesIn)) != -1) {
                bos.write(bytesIn, 0, read);
            }
            bos.close();
        }
    }
    is my class setup for extracting the file. currently for testing, in the main class I'm calling this class by:
    Code:
    public class Main extends JavaPlugin implements Listener {
    
       
        public String zipFilePath = this.getServer().getWorldContainer().getAbsolutePath() + "\\plugins\\RaidEvents\\world_RaidEvents.zip";
        public String destDirectory = this.getServer().getWorldContainer().getAbsolutePath();
    
        UnzipUtility unzip = new UnzipUtility();
    
        public void onEnable() {
    
       
    
            try {
                unzip.unzip(zipFilePath, destDirectory);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        }
    before I explain my error ill just show it:
    [​IMG]
    so what the code basically does is: it gets the destination directory, if it doesnt exist it makes it:
    Code:
     File destDir = new File(destDirectory);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
    then it starts itterating trough all the files in the .zip, and if its a directory it creates it, if its a file it copies it:

    Code:
      ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
            ZipEntry entry = zipIn.getNextEntry();
           
            // iterates over entries in the zip file
            while (entry != null) {
                String filePath = destDirectory + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, filePath);
                } else {
                    // if the entry is a directory, make the directory
                    File dir = new File(filePath);
                    dir.mkdir();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
    so it seems like that would work wouldnt it? still if we look at the error it seems like it's not creating the directories when it itterates through them, and when it reaches villagers.dat it cant find the directory to put it in? i'm a bit lost at this point. the zip file directory is correct, it's called "world_RaidEvents.zip", and it's located in my plugins folder, RaidEvents
     
  2. I’d like to help; but I’ve not used the zip utils before either. I don’t think villages.dat is necessary for every world... so perhaps it doesn’t exist...? But before you keep trying to use zip, what is it you’re trying to do- reload a world and undo any changes in it? If so, you’re able to unload chunks without saving them, prevent chunks from saving in ChunkUnloadEvent... and there’s Bukkit.unloadWorld(world, save), where you can specify “whether to save the chunks before unloading”... that might mean saving the currently loaded chunks, or not saving the world at all since it was last opened, I’m not sure.
     
Thread Status:
Not open for further replies.

Share This Page