Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.util.zip.ZipEntry;
  6. import java.util.zip.ZipInputStream;
  7.  
  8. public class unzipFile {
  9.  
  10.     public void extractFile(String filePath, String destination) {
  11.  
  12.         try {
  13.             FileInputStream inputStream = new FileInputStream(filePath);
  14.             ZipInputStream zipStream = new ZipInputStream(inputStream);
  15.             ZipEntry zEntry = null;
  16.             while ((zEntry = zipStream.getNextEntry()) != null) {
  17.  
  18.                 if (zEntry.isDirectory()) {
  19.                     hanldeDirectory(zEntry.getName(), destination);
  20.                 } else {
  21.                     FileOutputStream fout = new FileOutputStream(
  22.                             destination + "/" + zEntry.getName());
  23.                     BufferedOutputStream bufout = new BufferedOutputStream(fout);
  24.                     byte[] buffer = new byte[1024];
  25.                     int read = 0;
  26.                     while ((read = zipStream.read(buffer)) != -1) {
  27.                         bufout.write(buffer, 0, read);
  28.                     }
  29.  
  30.                     zipStream.closeEntry();
  31.                     bufout.close();
  32.                     fout.close();
  33.                 }
  34.             }
  35.             zipStream.close();
  36.             System.out.println("Unzip" + " " + "Unzipping complete. path :  " + destination);
  37.         } catch (Exception e) {
  38.             System.out.println("Unzip" +" " + "Unzipping failed");
  39.             e.printStackTrace();
  40.         }
  41.  
  42.     }
  43.  
  44.     public void hanldeDirectory(String dir, String destination) {
  45.         File f = new File(destination + dir);
  46.         if (!f.isDirectory()) {
  47.             f.mkdirs();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement