1

I want to write an Program where it will fetch all the Zip files present in an folder and unzip into an destination folder. I was able to write an program where i can unzip one single zip file but i want to unzip all the zip files present in that folder how can i do it?

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • 1
    Possible duplicate of [Read all files in a folder](http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder) – Dodd10x Mar 16 '17 at 16:25
  • Can you post your code? There is a great function for this and it would work well in a loop but we need to see what you're trying. – Ross Keddy Mar 16 '17 at 16:28

2 Answers2

2

Its not pretty but you get the idea.

  1. Using the NIO Files api from Java 7 stream the directory filtering the out the zip files
  2. Use the ZIP api to access each ZipEntry in the archive
  3. Using the NIO api write the files to the specified directory

    public class Unzipper {
    
      public static void main(String [] args){
        Unzipper unzipper = new Unzipper();
        unzipper.unzipZipsInDirTo(Paths.get("D:/"), Paths.get("D:/unzipped"));
      }
    
      public void unzipZipsInDirTo(Path searchDir, Path unzipTo ){
    
        final PathMatcher matcher = searchDir.getFileSystem().getPathMatcher("glob:**/*.zip");
        try (final Stream<Path> stream = Files.list(searchDir)) {
            stream.filter(matcher::matches)
                    .forEach(zipFile -> unzip(zipFile,unzipTo));
        }catch (IOException e){
            //handle your exception
        }
      }
    
     public void unzip(Path zipFile, Path outputPath){
        try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) {
    
            ZipEntry entry = zis.getNextEntry();
    
            while (entry != null) {
    
                Path newFilePath = outputPath.resolve(entry.getName());
                if (entry.isDirectory()) {
                    Files.createDirectories(newFilePath);
                } else {
                    if(!Files.exists(newFilePath.getParent())) {
                        Files.createDirectories(newFilePath.getParent());
                    }
                    try (OutputStream bos = Files.newOutputStream(outputPath.resolve(newFilePath))) {
                        byte[] buffer = new byte[Math.toIntExact(entry.getSize())];
    
                        int location;
    
                        while ((location = zis.read(buffer)) != -1) {
                            bos.write(buffer, 0, location);
                        }
                    }
                }
                entry = zis.getNextEntry();
            }
        }catch(IOException e){
            throw new RuntimeException(e);
            //handle your exception
        }
      }
    }
    
Udo Held
  • 12,314
  • 11
  • 67
  • 93
tprebs
  • 201
  • 2
  • 5
0

You could use the Java 7 Files API

Files.list(Paths.get("/path/to/folder"))
    .filter(c -> c.endsWith(".zip"))
    .forEach(c -> unzip(c));
Rainer
  • 761
  • 5
  • 20
  • 1
    Technically, this processes zip files in the directory and all subdirectories, not just the specified directory. [Files.list](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#list-java.nio.file.Path-) would more accurately address the question. – VGR Mar 16 '17 at 17:01
  • Thanks for the response Rainer, what should i use in .filter? .filter() I have given source and destination folder for the 1st and last line of your code but not sure how to filter the zip files in the 2nd line of your code. – BATMAN_2008 Mar 18 '17 at 08:34