Skip to content

How to rename entries when copying a zip file #135

Open
@lstreepy

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions