简体   繁体   English

TrueZip-如何获取Zip存档中文件夹的大小

[英]TrueZip - How to get Size of a Folder within an Zip Archive

is there a way to get the Size of a folder with TrueZip without doing it myself recursively? 有没有一种方法可以通过TrueZip获取文件夹的大小而无需自己递归执行? I'm concerned about the runtime since I'm dealing with Archives that contain lots of files. 我担心运行时,因为我正在处理包含大量文件的存档。

Using TrueZip 7.7.9 使用TrueZip 7.7.9

OK, here is a simple test using only the standard Java 7 API; 好的,这是仅使用标准Java 7 API的简单测试; it uses the bundled JSR 203 implementation for zips: 它将捆绑的JSR 203实现用于zip:

public final class Test
{
    public static void main(final String... args)
        throws IOException
    {
        final Path zip = Paths.get("/home/fge/t.zip");

        final URI uri = URI.create("jar:" + zip.toUri());

        try (
            final FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
            final DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"));
        ) {
            for (final Path entry: stream)
                System.out.println(Files.size(entry));
        }
    }
}

The zip above only contains files at the top level, not directories. 上面的zip仅包含顶层文件,而不包含目录。

This means that you can use this API to correctly compute the size of files in a zip; 这意味着您可以使用此API正确计算zip文件的大小。 without the need to uncompress. 无需解压缩。

You use Java 7; 您使用Java 7; therefore, what you want to do is probably to use a FileVisitor which computes the size of all regular files for you. 因此,您要做的可能是使用FileVisitor来为您计算所有常规文件的大小。

Here I have made a crude hack which uses Files.size() ; 在这里,我做了一个使用Files.size()的粗略技巧; note that in a FileVisitor , when you visit a filesystem entry which is not a directory, you have an instance of BasicFileAttributes coming along from which you can retrieve the size() . 请注意,在FileVisitor ,当您访问不是目录的文件系统条目时,将具有BasicFileAttributes实例,可以从中检索size()

Javadoc here ; Javadoc 在这里 ; and use Files.walkFileTree() . 并使用Files.walkFileTree()

With Java 8, this is much simpler, you could use Files.find() instead. 使用Java 8,这要简单得多,您可以改用Files.find()

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

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