Coverage Report - org.owasp.dependencycheck.utils.ExtractionUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtractionUtil
0%
0/57
0%
0/20
8.333
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.io.BufferedInputStream;
 21  
 import java.io.BufferedOutputStream;
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.util.logging.Level;
 28  
 import java.util.logging.Logger;
 29  
 import java.util.zip.ZipEntry;
 30  
 import java.util.zip.ZipInputStream;
 31  
 import org.owasp.dependencycheck.Engine;
 32  
 import static org.owasp.dependencycheck.utils.FileUtils.getFileExtension;
 33  
 
 34  
 /**
 35  
  *
 36  
  * @author Jeremy Long <jeremy.long@owasp.org>
 37  
  */
 38  
 public final class ExtractionUtil {
 39  
 
 40  
     /**
 41  
      * The logger.
 42  
      */
 43  0
     private static final Logger LOGGER = Logger.getLogger(ExtractionUtil.class.getName());
 44  
     /**
 45  
      * The buffer size to use when extracting files from the archive.
 46  
      */
 47  
     private static final int BUFFER_SIZE = 4096;
 48  
 
 49  
     /**
 50  
      * Private constructor for a utility class.
 51  
      */
 52  0
     private ExtractionUtil() {
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Extracts the contents of an archive into the specified directory.
 57  
      *
 58  
      * @param archive an archive file such as a WAR or EAR
 59  
      * @param extractTo a directory to extract the contents to
 60  
      * @throws ExtractionException thrown if an exception occurs while extracting the files
 61  
      */
 62  
     public static void extractFiles(File archive, File extractTo) throws ExtractionException {
 63  0
         extractFiles(archive, extractTo, null);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Extracts the contents of an archive into the specified directory. The files are only extracted if they are
 68  
      * supported by the analyzers loaded into the specified engine. If the engine is specified as null then all files
 69  
      * are extracted.
 70  
      *
 71  
      * @param archive an archive file such as a WAR or EAR
 72  
      * @param extractTo a directory to extract the contents to
 73  
      * @param engine the scanning engine
 74  
      * @throws ExtractionException thrown if there is an error extracting the files
 75  
      */
 76  
     public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException {
 77  0
         if (archive == null || extractTo == null) {
 78  0
             return;
 79  
         }
 80  
 
 81  0
         FileInputStream fis = null;
 82  0
         ZipInputStream zis = null;
 83  
 
 84  
         try {
 85  0
             fis = new FileInputStream(archive);
 86  0
         } catch (FileNotFoundException ex) {
 87  0
             LOGGER.log(Level.FINE, null, ex);
 88  0
             throw new ExtractionException("Archive file was not found.", ex);
 89  0
         }
 90  0
         zis = new ZipInputStream(new BufferedInputStream(fis));
 91  
         ZipEntry entry;
 92  
         try {
 93  0
             while ((entry = zis.getNextEntry()) != null) {
 94  0
                 if (entry.isDirectory()) {
 95  0
                     final File d = new File(extractTo, entry.getName());
 96  0
                     if (!d.exists() && !d.mkdirs()) {
 97  0
                         final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath());
 98  0
                         throw new ExtractionException(msg);
 99  
                     }
 100  0
                 } else {
 101  0
                     final File file = new File(extractTo, entry.getName());
 102  0
                     final String ext = getFileExtension(file.getName());
 103  0
                     if (engine == null || engine.supportsExtension(ext)) {
 104  0
                         BufferedOutputStream bos = null;
 105  
                         FileOutputStream fos;
 106  
                         try {
 107  0
                             fos = new FileOutputStream(file);
 108  0
                             bos = new BufferedOutputStream(fos, BUFFER_SIZE);
 109  
                             int count;
 110  0
                             final byte[] data = new byte[BUFFER_SIZE];
 111  0
                             while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
 112  0
                                 bos.write(data, 0, count);
 113  
                             }
 114  0
                             bos.flush();
 115  0
                         } catch (FileNotFoundException ex) {
 116  0
                             LOGGER.log(Level.FINE, null, ex);
 117  0
                             final String msg = String.format("Unable to find file '%s'.", file.getName());
 118  0
                             throw new ExtractionException(msg, ex);
 119  0
                         } catch (IOException ex) {
 120  0
                             LOGGER.log(Level.FINE, null, ex);
 121  0
                             final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
 122  0
                             throw new ExtractionException(msg, ex);
 123  
                         } finally {
 124  0
                             if (bos != null) {
 125  
                                 try {
 126  0
                                     bos.close();
 127  0
                                 } catch (IOException ex) {
 128  0
                                     LOGGER.log(Level.FINEST, null, ex);
 129  0
                                 }
 130  
                             }
 131  
                         }
 132  
                     }
 133  0
                 }
 134  
             }
 135  0
         } catch (IOException ex) {
 136  0
             final String msg = String.format("Exception reading archive '%s'.", archive.getName());
 137  0
             LOGGER.log(Level.FINE, msg, ex);
 138  0
             throw new ExtractionException(msg, ex);
 139  
         } finally {
 140  0
             try {
 141  0
                 zis.close();
 142  0
             } catch (IOException ex) {
 143  0
                 LOGGER.log(Level.FINEST, null, ex);
 144  0
             }
 145  0
         }
 146  0
     }
 147  
 }