0

I am wondering if there is a way, instead of just adding a file to a zip, to rename a file when adding it. I know of the write method:

(while ((len = zin.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

I have searched all over the internet and there is no information about it. Is there any class es, or methods Java has? The only reason I don't want to rename then add it is because I'm working on windows with an aux file. My whole code is:

File tempFile = File.createTempFile(zipFile.getName(), null);
    tempFile.delete();

    boolean renameOk=zipFile.renameTo(tempFile);
    if (!renameOk)
    {
        throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 17, len);
            }
        }
        entry = zin.getNextEntry();
    }        
    zin.close();
    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
4

0 に答える 0