Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to rename entries when copying a zip file #135

Open
lstreepy opened this issue Sep 16, 2020 · 1 comment
Open

How to rename entries when copying a zip file #135

lstreepy opened this issue Sep 16, 2020 · 1 comment

Comments

@lstreepy
Copy link

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
Copy link
Collaborator

nemecec commented Sep 17, 2020

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.

toomasr added a commit that referenced this issue Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants