I have a zip file containing a folder structure like
I would like to rename folder main-folder
to let's say versionXY
inside that very zip file using Java.
Is there a simpler way than extracting the whole zip file and recreating a new one using the new folder names?
Zip is an archive format, so mutating generally involves rewriting the file.
Some particular features of zip also get in the way (zip is full of "features"). As well as the central directory at the end of the archive, each component file is preceded by its file name. Zip doesn't have a concept of directories - file names are just strings that happen to include "/"
characters (and substrings such as "../"
.
So, you really need to copy the file using ZipInputStream
and ZipOutputStream
, renaming as you go. If you really wanted to you could rewrite the file in place doing your own buffering. The process does cause the contents to be recompressed as the standard API has no means of obtaining the data in compressed form.
Edit: @Doval points out that @megasega's answer uses Zip File System Provider in NIO, new (relative to this answer) in Java SE 7. It's performance will likely be not great, as were the archive file systems in RISC OS' GUI of thirty years ago.
I think you'll be able to find help for this task using the Commons Compress, especially ZipArchiveEntry
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With