简体   繁体   English

重命名zip文件中的文件名

[英]renaming file name inside a zip file

trying to rename internal file within a zip file without having to extract and then re-zip programatically. 尝试重命名zip文件中的内部文件,而不必解压缩然后以编程方式重新压缩。

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.zip包含test.txt,我想对其进行更改,以便test.zip将包含newtest.txt(将test.txt重命名为newtest.txt,内容保持不变)

came across this link that works but unfortunately it expects test.txt to exist on the system. 偶然发现此链接有效,但不幸的是,它希望test.txt存在于系统上。 In the example the srcfile should exist on the server. 在示例中,srcfile应该在服务器上存在。

Blockquote Rename file in zip with zip4j 使用zip4j的zip中的 Blockquote 重命名文件

Then icame across zipnote on Linux that does the trick but unfortunately the version i have doesnt work for files >4GB. 然后在Linux上的zipnote上使用icame可以达到目的,但是不幸的是,我拥有的版本不适用于大于4GB的文件。

Any suggestions on how to accomplish this? 关于如何做到这一点的任何建议? prefereably in java. 最好在Java中。

This should be possible using Java 7 Zip FileSystem provider, something like: 使用Java 7 Zip FileSystem提供程序应该可以实现,例如:

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

Using zip4j, I am modifying and re-writing the file headers inside of the central directory section to avoid rewriting the entire zip file: 我使用zip4j修改并重写中央目录部分中的文件头,以避免重写整个zip文件:

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.. 这很棘手,但可能有问题,我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM