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

查看:1091
本文介绍了重命名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中的文件

然后在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提供程序应该可以实现,例如:

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天全站免登陆