【问题标题】:List of files inside a zip folder and its subfolderzip 文件夹及其子文件夹中的文件列表
【发布时间】:2017-11-04 11:01:21
【问题描述】:

我正在寻找一种方法来获取 zip 文件中的文件列表。我创建了一种方法来获取目录中的文件列表,但我也在寻找一种方法来获取 zip 中的文件,而不是只显示 zip 文件。

这是我的方法:

public ArrayList<String> listFiles(File f, String min, String max) {
    try {
        // parse input strings into date format
        Date minDate = sdf.parse(min);
        Date maxDate = sdf.parse(max);
        //
        File[] list = f.listFiles();
        for (File file : list) {
            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            if (file.isFile()) {
                String fileDateString = sdf.format(file.lastModified());
                Date fileDate = sdf.parse(fileDateString);
                if (fileDate.after(minDate) && fileDate.before(maxDate)) {
                    lss.add("'" + file.getAbsolutePath() + 
                        "'" + " Size KB:" + kilobytes + " Last Modified: " +
                        sdf.format(file.lastModified()));
                }
            } else if (file.isDirectory()) {
                listFiles(file.getAbsoluteFile(), min, max);
            }
        }
    } catch (Exception e) {
        e.getMessage();
    }
    return lss;
}

【问题讨论】:

标签: java file io zip


【解决方案1】:

在寻找更好的答案一段时间后,我终于找到了一个更好的方法来做到这一点。实际上,您可以使用 Java NIO API(自 Java 7 起)以更通用的方式执行相同的操作。

// this is the URI of the Zip file itself
URI zipUri = ...; 
FileSystem zipFs = FileSystems.newFileSystem(zipUri, Collections.emptyMap());

// The path within the zip file you want to start from
Path root = zipFs.getPath("/"); 

Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
        // You can do anything you want with the path here
        System.out.println(path);
        // the BasicFileAttributes object has lots of useful meta data
        // like file size, last modified date, etc...
        return FileVisitResult.CONTINUE;
    }

    // The FileVisitor interface has more methods that 
    // are useful for handling directories.
});

这种方法的优点是您可以通过这种方式遍历任何文件系统:普通的 windows 或 Unix 文件系统,文件系统包含在 zip 或 jar 中,或任何其他文件系统中。

然后您可以通过Files 类使用Files.copy()File.readAllLines()File.readAllBytes() 等方法轻松读取任何Path 的内容...

【讨论】:

    【解决方案2】:

    您可以使用ZipFile.entries()方法通过迭代读取文件列表,如下所示:

    File[] fList = directory.listFiles();
    
    for (File file : fList)
    {
        ZipFile myZipFile = new ZipFile(fList.getName());
    
        Enumeration zipEntries = myZipFile.entries();
    
        while (zipEntries.hasMoreElements())
        {
            System.out.println(((ZipEntry) zipEntries.nextElement()).getName());
            // you can do what ever you want on each zip file
        }
    }
    

    【讨论】:

    • 我不想手动给出 zip 文件名,我只想在给出目录路径时动态行为,然后方法应该对这个目录中的所有 zip 文件执行此操作
    猜你喜欢
    • 1970-01-01
    • 2014-05-08
    • 2019-06-19
    • 1970-01-01
    • 2016-07-24
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多