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();
Ajay
  • 437
  • 7
  • 22
  • Change the values of the `ZipEntry`... – MadProgrammer Mar 04 '13 at 00:09
  • What do you mean by change the values of the `ZipEntry`? Do you mean change the 0 in (buf, 0, len)? – Ajay Mar 04 '13 at 00:16
  • 1
    This depends. If you a are writing multiple files to a Zip file, each file should have a `ZipEntry`. The `ZipEntry`, amongst other things, describes the directory and name of the file you are adding. When you create a new `ZipEntry`, simply change the name it's name – MadProgrammer Mar 04 '13 at 00:18
  • I can't figure out how to do that. – Ajay Mar 04 '13 at 00:24
  • Post a runnable example of what you are doing – MadProgrammer Mar 04 '13 at 00:28
  • 1
    Okay, I'm confused, what are you trying to rename? The entries in the zip file or the actual file your adding? I would, also, use the temp file as the location for the new zip. This means if anything goes wrong, you've not lost the original file... – MadProgrammer Mar 04 '13 at 01:37
  • 1
    At the risk of repeating what MadProgrammer has said, I think maybe you are under the impression that entry names are part of the ZipOutputStream. They are not. The name you pass to the ZipEntry constructor *is* the new filename, period. So to rename a file, pass a different string when constructing your ZipEntry. – VGR Mar 04 '13 at 12:54
  • Thanks guys, ajayajayaj – Ajay Mar 06 '13 at 01:15

0 Answers0