Java ZipFile uncompress a zip file

Description

Java ZipFile uncompress a zip file

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {

   public static void main(String[] args) {
      ZipFile file = null;/*w w w  . ja v  a2 s .co m*/
      try {
         file = new ZipFile("file.zip");
         FileSystem fileSystem = FileSystems.getDefault();
         Enumeration<? extends ZipEntry> entries = file.entries();
         String uncompressedDirectory = "uncompressed/";
         Files.createDirectory(fileSystem.getPath(uncompressedDirectory));
         while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
               System.out.println("Creating Directory:" + uncompressedDirectory + entry.getName());
               Files.createDirectories(fileSystem.getPath(uncompressedDirectory + entry.getName()));
            } else {
               InputStream is = file.getInputStream(entry);
               System.out.println("File :" + entry.getName());
               BufferedInputStream bis = new BufferedInputStream(is);

               String uncompressedFileName = uncompressedDirectory + entry.getName();
               Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
               Files.createFile(uncompressedFilePath);
               FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
               while (bis.available() > 0) {
                  fileOutput.write(bis.read());
               }
               fileOutput.close();
               System.out.println("Written :" + entry.getName());
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related