Need to count the number of files located within each zip file in a directory folder in JAVA

Viewed 412

I have a folder which has a series of Zip files within it. I am trying to iterate through the folder and count the number of files that are in each zip file. I have created two pieces of code, I am just not sure how to put them together to get my desired results. Both codes are placed into try/catch blocks and they both work perfectly independently. This is using Eclipse, written in Java.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;
import java.io.File;
import java.util.List;
 
public class KZF {
 
       public static void main(String[] args) {
             // TODO Auto-generated method stub
 
             // Try/Catch Block counts the number of files within a given zip file
             try {
 
                    ZipFile zipFile = new ZipFile(
                           "C:\\Users\\username\\Documents\\Temp\\AllKo\\Policy.zip");
 
                    int NumberOfFiles = zipFile.size() - 1;
                    // String name = zipFile.getName();
                    Path path = Paths
                           .get("C:\\Users\\username\\Documents\\Temp\\AllKo\\Policy.zip");
                    Path filename = path.getFileName();
 
                    System.out.print("The number of files in: ");
                    // System.out.print(name);
                    System.out.print(filename.toString());
                    System.out.print(" are: ");
                    System.out.print(NumberOfFiles + " file(s)");
 
                    zipFile.close();
 
             }
 
             catch (IOException ioe) {
 
                    System.out.println("Error opening zip file" + ioe);
             }
 
             // ----------------------------------------------------------------------------------------------------------
             // Creates list of every file specified folder
 
             
              String dirLocation = "C:\\Users\\username\\Documents\\Temp\\AllKo";
             
               try { List<File> files = Files.list(Paths.get(dirLocation))
              .map(Path::toFile) .collect(Collectors.toList());
             
               files.forEach(System.out::println);
             
               } catch(IOException e) { Error }
             
 
       }
 
}
1 Answers

You must be careful about opening/closing streams, so you can try something like this:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class KZF
{
    static int findNumberOfFiles(File file) {
        try (ZipFile zipFile = new ZipFile(file)) {
            return zipFile.stream().filter(z -> !z.isDirectory()).count();
        } catch (Exception e) {
            return -1;
        }
    }

    static String createInfo(File file) {
        int tot = findNumberOfFiles(file);
        return (file.getName() + ": " + (tot >= 0 ? tot + " files" : "Error reading zip file"));
    }

    public static void main(String[] args) throws IOException {
        String dirLocation = "C:\\Users\\username\\Documents\\Temp\\AllKo";
        try (Stream<Path> files = Files.list(Paths.get(dirLocation))) {
            files
            .filter(path -> path.toFile().isFile())
            .filter(path -> path.toString().toLowerCase().endsWith(".zip"))
            .map(Path::toFile)
            .map(KZF::createInfo)
            .forEach(System.out::println);
        }
    }
}
Related