zeroturnaround / zt-zip

ZeroTurnaround ZIP Library
http://www.zeroturnaround.com/
Apache License 2.0
1.38k stars 251 forks source link

How to rename entries when copying a zip file #135

Open lstreepy opened 4 years ago

lstreepy commented 4 years ago

I am trying to rename entries in a zip file. Best would be to do it in place, but I can't even get it to work when copying to a new zip file. Here is the code for my attempt:

        final File output = new File("output.zip");
        if (!output.exists()) output.createNewFile();

        Zips.get(new File("input.zip"))
                .destination(output)
                .nameMapper(name -> {
                    final String newName = name.startsWith("my_package")
                            ? name.replace("my_package", "another_pkg")
                            : name;
                    System.out.println("Process: " + name + " ==> " + newName);
                    return newName;
                })
                .process();

However, when I run this, I get this exception:

Exception in thread "main" org.zeroturnaround.zip.ZipException: java.io.IOException: Stream closed
    at org.zeroturnaround.zip.ZipExceptionUtil.rethrow(ZipExceptionUtil.java:11)
    at org.zeroturnaround.zip.Zips.iterateExistingExceptRemoved(Zips.java:540)
    at org.zeroturnaround.zip.Zips.processAllEntries(Zips.java:384)
    at org.zeroturnaround.zip.Zips.process(Zips.java:364)
    at gov.raptor.commons.file.FileUtils.main(FileUtils.java:1086)
Caused by: java.io.IOException: Stream closed
Caused by: java.io.IOException: Stream closed

    at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at org.zeroturnaround.zip.commons.IOUtils.copyLarge(IOUtils.java:322)
    at org.zeroturnaround.zip.commons.IOUtils.copyLarge(IOUtils.java:299)
    at org.zeroturnaround.zip.commons.IOUtils.copy(IOUtils.java:274)
    at org.zeroturnaround.zip.ZipEntryUtil.addEntry(ZipEntryUtil.java:131)
    at org.zeroturnaround.zip.ZipEntryUtil.copyEntry(ZipEntryUtil.java:115)
    at org.zeroturnaround.zip.Zips$CopyingCallback.process(Zips.java:655)
    at org.zeroturnaround.zip.ZipEntryOrInfoAdapter.process(ZipEntryOrInfoAdapter.java:26)
    at org.zeroturnaround.zip.Zips.iterateExistingExceptRemoved(Zips.java:529)
    ... 3 more

Investigating the code in Zips, it appears that the name mapper causes it to try to find the renamed entry in the SOURCE zip instead of changing the name in the destination zip. That seems counter-intuitive at best, and may be a bug.

I'm sure that I have missed something, but I can't find a way to rename an entry in a zip file. I see that you 3rd functional requirement was to support renaming entries, but I can't figure it out with the documentation I can find.

I apologize if this is my lack of knowledge on how to use your library (which has been awesome for all my other needs).

Thanks, Larry.

nemecec commented 4 years ago

It seems you have indeed stumbled upon a bug -- I created a PR with unit test to demonstrate this, based on your code.

This library is optimized towards packing and unpacking use-cases and ZIP->ZIP transformations are not very well supported. I guess a workaround for now would be to unpack the archive to file system and then pack it again. During unpacking and packing, you can supply your NameMapper to perform renames.

I also briefly looked at fixing the bug myself but it seems that it needs a bigger design change in the internals of zt-zip. I hope @toomasr or @reinra will have a chance to look at this at some point.