重命名 zip 文件中的文件名 [英] renaming file name inside a zip file

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

问题描述

尝试重命名 zip 文件中的内部文件,而无需以编程方式解压缩然后重新压缩.

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

示例.test.zip 包含 test.txt,我想更改它以便 test.zip 包含 newtest.txt(test.txt 重命名为 newtest.txt,内容保持不变)

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)

发现此链接有效,但不幸的是它希望系统上存在 test.txt.在示例中,srcfile 应该存在于服务器上.

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 使用 zip4j 重命名 zip 文件>

Blockquote Rename file in zip with zip4j

然后在 Linux 上的 zipnote 上使用 icame 可以解决问题,但不幸的是,我的版本不适用于大于 4GB 的文件.

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

有关如何完成此操作的任何建议?最好是java.

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

推荐答案

这应该可以使用 Java 7 Zip FileSystem provider,例如:

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); 
}

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

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