Coverage Report - com.aurea.maven.plugins.sonic.utils.ZipUtilities
 
Classes in this File Line Coverage Branch Coverage Complexity
ZipUtilities
0%
0/33
0%
0/12
5.5
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.BufferedInputStream;
 4  
 import java.io.File;
 5  
 import java.io.FileInputStream;
 6  
 import java.io.FileOutputStream;
 7  
 import java.util.zip.ZipEntry;
 8  
 import java.util.zip.ZipInputStream;
 9  
 
 10  
 /**
 11  
  *
 12  
  */
 13  0
 public class ZipUtilities {
 14  
 
 15  
 
 16  
   /**
 17  
    * @param _filename
 18  
    *          filename
 19  
    * @param _unzipDir
 20  
    *          unzipDir
 21  
    */
 22  
   public static void doUnzipAction(final String _filename, final String _unzipDir) {
 23  
 
 24  0
     final int bufferSize = 1024;
 25  
 
 26  0
     FileOutputStream dest = null;
 27  
 
 28  
     ZipInputStream zis;
 29  
 
 30  
     try {
 31  0
       zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(new File(_filename))));
 32  
 
 33  
       ZipEntry entry;
 34  
 
 35  0
       new File(_unzipDir).mkdirs();
 36  0
       while ((entry = zis.getNextEntry()) != null) {
 37  
         int count;
 38  0
         byte[] data = new byte[bufferSize];
 39  0
         String targetFile = _unzipDir + "/" + entry.getName();
 40  
 
 41  0
         createSubDirectory(_unzipDir, entry);
 42  
 
 43  
         // getLog().info("Unzip file " + targetFile);
 44  
 
 45  0
         if (!entry.isDirectory()) {
 46  0
           dest = new FileOutputStream(targetFile);
 47  
 
 48  0
           while ((count = zis.read(data, 0, bufferSize)) != -1) {
 49  0
             dest.write(data, 0, count);
 50  
           }
 51  
 
 52  0
           dest.flush();
 53  0
           dest.close();
 54  
         }
 55  0
         dest = null;
 56  
 
 57  0
       } // while entries
 58  
 
 59  0
       zis.close();
 60  
 
 61  0
     } catch (Exception e) {
 62  
       // TODO this could be done nicer
 63  0
       e.printStackTrace();
 64  0
       throw new RuntimeException(e);
 65  0
     }
 66  0
   }
 67  
 
 68  
   private static void createSubDirectory(final String _unzipDir, final ZipEntry _entry) {
 69  
 
 70  
     /*
 71  
      * entry.isDirectory() returns true if the entry name ends with "/" When unzipping however, entries in
 72  
      * subdirectories can preceed the entry of the directory
 73  
      */
 74  0
     String name = _entry.getName();
 75  
     String fileDir;
 76  0
     if (name.indexOf("\\") > 0) {
 77  
 
 78  0
       fileDir = name.substring(0, name.lastIndexOf("\\"));
 79  
 
 80  0
     } else if (name.indexOf("/") > 0) {
 81  
 
 82  0
       fileDir = name.substring(0, name.lastIndexOf("/"));
 83  
 
 84  
     } else {
 85  
       // no subdir for this ZipEntry
 86  0
       return;
 87  
     }
 88  
 
 89  
     // check is subdir already exists
 90  0
     File newdir = new File(_unzipDir + File.separator + fileDir);
 91  0
     if (!newdir.exists()) {
 92  0
       newdir.mkdirs();
 93  
     }
 94  0
   }
 95  
 }