Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

trying to rename internal file within a zip file without having to extract and then re-zip programatically.

example. test.zip contains test.txt, i want to change it so that test.zip will contain newtest.txt(test.txt renamed to newtest.txt, contents remain the same)

came across this link that works but unfortunately it expects test.txt to exist on the system. In the example the srcfile should exist on the server.

Blockquote Rename file in zip with zip4j

Then icame across zipnote on Linux that does the trick but unfortunately the version i have doesnt work for files >4GB.

Any suggestions on how to accomplish this? prefereably in java.

This should be possible using Java 7 Zip FileSystem provider, something like:

// syntax defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/directoryPath/file.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap())) {
    Path sourceURI      = zipfs.getPath("/pathToDirectoryInsideZip/file.txt");
    Path destinationURI = zipfs.getPath("/pathToDirectoryInsideZip/renamed.txt");          
    Files.move(sourceURI, destinationURI); 
                this worked but only after a change, this statement did not work FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.emptyMap())) -- mismatch in the type. I used the below from the java doc for this class
– md1980
                Jan 24, 2015 at 22:09
                Map<String,String> env = new HashMap<>(); 		   env.put("capacity", "16G"); 		   env.put("blockSize", "4k");
– md1980
                Jan 24, 2015 at 22:09
                FileSystem zipfs = FileSystems.newFileSystem(uri, env)                          althought at this point i dont know what hte capacity and block size stand for. If you do, please share.
– md1980
                Jan 24, 2015 at 22:10
                Type mismatch happened because the type inference cannot do it manually, I updated the example.
– Crazyjavahacking
                Jan 24, 2015 at 22:46
                The capacity and blockSize parameters are made up for the purpose of memory filesystem. They have no meaning in terms of Zip filesystem. Valid params for Zip file system
– Crazyjavahacking
                Jan 24, 2015 at 22:47

Using zip4j, I am modifying and re-writing the file headers inside of the central directory section to avoid rewriting the entire zip file:

ArrayList<FileHeader> FHs = (ArrayList<FileHeader>) zipFile.getFileHeaders();
FHs.get(0).setFileName("namename.mp4");
FHs.get(0).setFileNameLength("namename.mp4".getBytes("UTF-8").length);
zipFile.updateHeaders ();
//where updateHeaders is :
    public void updateHeaders() throws ZipException, IOException {
        checkZipModel();
        if (this.zipModel == null) {
            throw new ZipException("internal error: zip model is null");
        if (Zip4jUtil.checkFileExists(file)) {
            if (zipModel.isSplitArchive()) {
                throw new ZipException("Zip file already exists. Zip file format does not allow updating split/spanned files");
        long offset = zipModel.getEndCentralDirRecord().getOffsetOfStartOfCentralDir();
        HeaderWriter headerWriter = new HeaderWriter();
        SplitOutputStream splitOutputStream = new SplitOutputStream(new File(zipModel.getZipFile()), -1);
        splitOutputStream.seek(offset);
        headerWriter.finalizeZipFile(zipModel, splitOutputStream);
        splitOutputStream.close();

The name field in the local file header section remains unchanged, so there will be a mismatch exception in this library.
It's tricky but maybe problematic, I don't know..

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.