Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

java - List of files inside a zip folder and its subfolder

I am looking a way to get the list of files inside a zip file. I created a method to get the list of files inside a directory but I am also looking a way to get files inside a zip as well instead of showing just zip file.

here is my method:

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After having searched for a better answer for a while, I finally found a better way to do this. You can actually do the same thing in a more generic way using the Java NIO API (Since 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.
});

This approach has the advantage that you can travers ANY file system this way: your normal windows or Unix filesystem, the file system contain contained within a zip or a jar, or any other really.

You can then trivially read the contents of any Path via the Files class, using methods like Files.copy(), File.readAllLines(), File.readAllBytes(), etc...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...