Java在不解压的情况下重命名zip/gz文件内容。

huangapple go评论82阅读模式
英文:

java renaming contents of zip/gz file without inflating

问题

对于.zip文件,在Java中我们有ZipInputStream和ZipOutputStream用于从zip文件中读取和写入。

zin = new ZipInputStream(new FileInputStream("xx.zip"));
ZipEntry entry =  zin.getNextEntry();
while (entry != null) {
    String name = entry.getName();
    File file = new File(name);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    byte[] bytesIn = new byte[1024];
    int read;
    while ((read = zin.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
    // 文件重命名的代码... 在这里 ...
    // . . . 
    entry = zin.getNextEntry();
}
zin.close();

类似地,要使用GZIPInputStream解压缩.gz文件。由于.gz文件只有一个内容,所以不需要zipentry。我们可以直接使用名称解压缩它,将名称作为.gz文件(不带后缀名)。

我的问题是:

我能否在不解压缩内容的情况下重命名.zip(以及.gz)的内容?对于.gz文件,因为.gz文件和内容具有相同的名称,如果重命名.gz文件,内容也会被重命名。

对于.zip文件,我能否简单地重命名ZipEntry而不必完全解压缩?

请提供建议。

英文:

For .zip file we have, in Java, ZipInputStream and ZipOutputStream for reading from and writing to zip file.

    zin = new ZipInputStream(new FileInputStream("xx.zip"));
    ZipEntry entry =  zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        File file = new File(name);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        byte[] bytesIn = new byte[1024];
        while ((int read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
        // code for renaming file ... here ...
        // . . . 
        entry = zin.getNextEntry();
    }
    zin.close();

And, similarly for inflating .gz using GZIPInputStream. Since, .gz files have only one content, the zipentry is not required. We may directly inflate it using name as .gz file (without the suffix).
My question is:

Can I rename the content of .zip (andd .gz also) without inflating the content(s)? In .gz file, since we have same name of .gz file and the content, if we rename the .gz file, that would rename its content too.

For .zip file, can I simply rename ZipEntry without completely inflating?

Please suggest.

答案1

得分: 1

Zip4j具有重命名zip文件中文件的功能。尽管它仅支持zip文件,不支持gzip。

英文:

Zip4j has a feature to rename files in the zip. It supports only zip files though, and not gzip.

huangapple
  • 本文由 发表于 2020年5月5日 14:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61606950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定