Solved Exception caused by InputStream

Discussion in 'Spigot Plugin Development' started by tom2208, Mar 15, 2020.

  1. Hey there i'm trying to fix this issue for hours.. Now i try to get some help here.
    I have this method copying a file from my plugin jar into a folder (server/plugins/pluginname/locales/copiedFile.example).
    Code (Java):
          /**
         * Export a resource embedded into a Jar file to the local file path.
         *
         * @param resourceName ie.: "/xyz.properties"
         * @return The path to the exported resource
         * @throws Exception
         */

        static public void ExportResource(String resourceName) throws Exception {
            InputStream stream = null;
            OutputStream resStreamOut = null;
            String jarFolder;
            try {
                stream = ChestCleaner.class.getResourceAsStream("/pluginname/main/" + resourceName);
                if(stream == null) {
                    throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
                }

                int readBytes;
                byte[] buffer = new byte[4096];
                jarFolder = new File(ChestCleaner.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
                resStreamOut = new FileOutputStream(jarFolder + "/pluginname/locales/"+ resourceName);
                while ((readBytes = stream.read(buffer)) > 0) {
                    resStreamOut.write(buffer, 0, readBytes);
                }
            } catch (Exception ex) {
                throw ex;
            } finally {
                if(stream != null)
                    stream.close();
                if(resStreamOut != null) {
                    resStreamOut.close();
                }
            }

        }
    Now i try to run this method in the onEnable() method of my JavaPlugin extending class:
    Code (Text):
            try {
                ExportResource("pluginname.properties");
            } catch (Exception e) {
                e.printStackTrace();
            }
    And i get an Error Message + the generated file is empty:
    Code (Text):
    java.util.zip.ZipException: invalid stored block lengths
    [16:24:39] [Server thread/WARN]:        at java.util.zip.InflaterInputStream.read(Unknown Source)
    [16:24:39] [Server thread/WARN]:        at java.io.FilterInputStream.read(Unknown Source)
    [16:24:39] [Server thread/WARN]:        at java.io.FilterInputStream.read(Unknown Source)
    The error occurs on the line with the while head: ( "while ((readBytes = stream.read(buffer)) > 0) {" ).

    Do you know the reason why this occurs and how i could fix it?
     
    #1 tom2208, Mar 15, 2020
    Last edited: Mar 15, 2020
    • Useful Useful x 1