用Java重命名Zip文件中的文件/文件夹? [英] Renaming a File/Folder inside a Zip File in Java?

查看:980
本文介绍了用Java重命名Zip文件中的文件/文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件夹结构的zip文件,如

I have a zip file containing a folder structure like


  • main-folder /


    • subFolder1 /

    • subFolder2 /

    • subFolder3 /


      • file3.1

      • file3.2

      • main-folder/
        • subFolder1/
        • subFolder2/
        • subFolder3/
          • file3.1
          • file3.2

          我想重命名文件夹 main-folder 让我们说 versionXY 在使用Java的zip文件中。

          I would like to rename folder main-folder to let's say versionXY inside that very zip file using Java.

          是否有一种比使用新文件夹名称提取整个zip文件并重新创建新文件更简单的方法?

          Is there a simpler way than extracting the whole zip file and recreating a new one using the new folder names?

          推荐答案

          Zip是一种存档格式,因此变异通常涉及重写文件。

          Zip is an archive format, so mutating generally involves rewriting the file.

          拉链的一些特殊功能也会妨碍(拉链充满功能)。除了归档末尾的中央目录外,每个组件文件前面都有其文件名。 Zip没有目录的概念 - 文件名只是碰巧包含/字符的字符串(和子串,例如../\".

          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 "../".

          所以,你真的需要使用 ZipInputStream ZipOutputStream 复制文件,然后重命名。如果你真的想要你可以重写文件来进行自己的缓冲。这个过程会导致内容被重新压缩,因为标准API无法以压缩形式获取数据。

          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.

          这篇关于用Java重命名Zip文件中的文件/文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆