Coverage Report - org.owasp.dependencycheck.analyzer.ArchiveAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchiveAnalyzer
34%
87/255
16%
25/148
6.824
 
 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.analyzer;
 19  
 
 20  
 import java.io.BufferedInputStream;
 21  
 import java.io.File;
 22  
 import java.io.FileFilter;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.util.Collections;
 28  
 import java.util.Enumeration;
 29  
 import java.util.List;
 30  
 import java.util.Set;
 31  
 
 32  
 import org.apache.commons.compress.archivers.ArchiveEntry;
 33  
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 34  
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 35  
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 36  
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 37  
 import org.apache.commons.compress.archivers.zip.ZipFile;
 38  
 import org.apache.commons.compress.compressors.CompressorInputStream;
 39  
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 40  
 import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
 41  
 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
 42  
 import org.apache.commons.compress.compressors.gzip.GzipUtils;
 43  
 import org.apache.commons.compress.utils.IOUtils;
 44  
 
 45  
 import org.owasp.dependencycheck.Engine;
 46  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 47  
 import org.owasp.dependencycheck.analyzer.exception.ArchiveExtractionException;
 48  
 import org.owasp.dependencycheck.dependency.Dependency;
 49  
 import org.owasp.dependencycheck.exception.InitializationException;
 50  
 import org.owasp.dependencycheck.utils.FileFilterBuilder;
 51  
 import org.owasp.dependencycheck.utils.FileUtils;
 52  
 import org.owasp.dependencycheck.utils.Settings;
 53  
 
 54  
 import org.slf4j.Logger;
 55  
 import org.slf4j.LoggerFactory;
 56  
 
 57  
 /**
 58  
  * <p>
 59  
  * An analyzer that extracts files from archives and ensures any supported files
 60  
  * contained within the archive are added to the dependency list.</p>
 61  
  *
 62  
  * @author Jeremy Long
 63  
  */
 64  8
 public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
 65  
 
 66  
     /**
 67  
      * The logger.
 68  
      */
 69  1
     private static final Logger LOGGER = LoggerFactory.getLogger(ArchiveAnalyzer.class);
 70  
     /**
 71  
      * The count of directories created during analysis. This is used for
 72  
      * creating temporary directories.
 73  
      */
 74  1
     private static int dirCount = 0;
 75  
     /**
 76  
      * The parent directory for the individual directories per archive.
 77  
      */
 78  8
     private File tempFileLocation = null;
 79  
     /**
 80  
      * The max scan depth that the analyzer will recursively extract nested
 81  
      * archives.
 82  
      */
 83  1
     private static final int MAX_SCAN_DEPTH = Settings.getInt("archive.scan.depth", 3);
 84  
     /**
 85  
      * Tracks the current scan/extraction depth for nested archives.
 86  
      */
 87  8
     private int scanDepth = 0;
 88  
 
 89  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 90  
     /**
 91  
      * The name of the analyzer.
 92  
      */
 93  
     private static final String ANALYZER_NAME = "Archive Analyzer";
 94  
     /**
 95  
      * The phase that this analyzer is intended to run in.
 96  
      */
 97  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INITIAL;
 98  
     /**
 99  
      * The set of things we can handle with Zip methods
 100  
      */
 101  1
     private static final Set<String> ZIPPABLES = newHashSet("zip", "ear", "war", "jar", "sar", "apk", "nupkg");
 102  
     /**
 103  
      * The set of file extensions supported by this analyzer. Note for
 104  
      * developers, any additions to this list will need to be explicitly handled
 105  
      * in {@link #extractFiles(File, File, Engine)}.
 106  
      */
 107  1
     private static final Set<String> EXTENSIONS = newHashSet("tar", "gz", "tgz", "bz2", "tbz2");
 108  
 
 109  
     /**
 110  
      * Detects files with extensions to remove from the engine's collection of
 111  
      * dependencies.
 112  
      */
 113  1
     private static final FileFilter REMOVE_FROM_ANALYSIS = FileFilterBuilder.newInstance().addExtensions("zip", "tar", "gz", "tgz", "bz2", "tbz2")
 114  1
             .build();
 115  
 
 116  
     static {
 117  1
         final String additionalZipExt = Settings.getString(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS);
 118  1
         if (additionalZipExt != null) {
 119  0
             final String[] ext = additionalZipExt.split("\\s*,\\s*");
 120  0
             Collections.addAll(ZIPPABLES, ext);
 121  
         }
 122  1
         EXTENSIONS.addAll(ZIPPABLES);
 123  
     }
 124  
 
 125  
     /**
 126  
      * The file filter used to filter supported files.
 127  
      */
 128  1
     private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();
 129  
 
 130  
     @Override
 131  
     protected FileFilter getFileFilter() {
 132  860
         return FILTER;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Detects files with .zip extension.
 137  
      */
 138  1
     private static final FileFilter ZIP_FILTER = FileFilterBuilder.newInstance().addExtensions("zip").build();
 139  
 
 140  
     /**
 141  
      * Returns the name of the analyzer.
 142  
      *
 143  
      * @return the name of the analyzer.
 144  
      */
 145  
     @Override
 146  
     public String getName() {
 147  22
         return ANALYZER_NAME;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Returns the phase that the analyzer is intended to run in.
 152  
      *
 153  
      * @return the phase that the analyzer is intended to run in.
 154  
      */
 155  
     @Override
 156  
     public AnalysisPhase getAnalysisPhase() {
 157  6
         return ANALYSIS_PHASE;
 158  
     }
 159  
     //</editor-fold>
 160  
 
 161  
     /**
 162  
      * Returns the key used in the properties file to reference the analyzer's
 163  
      * enabled property.
 164  
      *
 165  
      * @return the analyzer's enabled property setting key
 166  
      */
 167  
     @Override
 168  
     protected String getAnalyzerEnabledSettingKey() {
 169  2
         return Settings.KEYS.ANALYZER_ARCHIVE_ENABLED;
 170  
     }
 171  
 
 172  
     /**
 173  
      * The initialize method does nothing for this Analyzer.
 174  
      *
 175  
      * @throws InitializationException is thrown if there is an exception
 176  
      * deleting or creating temporary files
 177  
      */
 178  
     @Override
 179  
     public void initializeFileTypeAnalyzer() throws InitializationException {
 180  
         try {
 181  1
             final File baseDir = Settings.getTempDirectory();
 182  1
             tempFileLocation = File.createTempFile("check", "tmp", baseDir);
 183  1
             if (!tempFileLocation.delete()) {
 184  0
                 setEnabled(false);
 185  0
                 final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
 186  0
                 throw new InitializationException(msg);
 187  
             }
 188  1
             if (!tempFileLocation.mkdirs()) {
 189  0
                 setEnabled(false);
 190  0
                 final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
 191  0
                 throw new InitializationException(msg);
 192  
             }
 193  0
         } catch (IOException ex) {
 194  0
             setEnabled(false);
 195  0
             throw new InitializationException("Unable to create a temporary file", ex);
 196  1
         }
 197  1
     }
 198  
 
 199  
     /**
 200  
      * The close method deletes any temporary files and directories created
 201  
      * during analysis.
 202  
      *
 203  
      * @throws Exception thrown if there is an exception deleting temporary
 204  
      * files
 205  
      */
 206  
     @Override
 207  
     public void closeAnalyzer() throws Exception {
 208  1
         if (tempFileLocation != null && tempFileLocation.exists()) {
 209  1
             LOGGER.debug("Attempting to delete temporary files");
 210  1
             final boolean success = FileUtils.delete(tempFileLocation);
 211  1
             if (!success && tempFileLocation.exists()) {
 212  0
                 final String[] l = tempFileLocation.list();
 213  0
                 if (l != null && l.length > 0) {
 214  0
                     LOGGER.warn("Failed to delete some temporary files, see the log for more details");
 215  
                 }
 216  
             }
 217  
         }
 218  1
     }
 219  
 
 220  
     /**
 221  
      * Does not support parallel processing as it both modifies and iterates
 222  
      * over the engine's list of dependencies.
 223  
      *
 224  
      * @see #analyzeDependency(Dependency, Engine)
 225  
      * @see #findMoreDependencies(Engine, File)
 226  
      */
 227  
     @Override
 228  
     public boolean supportsParallelProcessing() {
 229  1
         return false;
 230  
     }
 231  
 
 232  
     /**
 233  
      * Analyzes a given dependency. If the dependency is an archive, such as a
 234  
      * WAR or EAR, the contents are extracted, scanned, and added to the list of
 235  
      * dependencies within the engine.
 236  
      *
 237  
      * @param dependency the dependency to analyze
 238  
      * @param engine the engine scanning
 239  
      * @throws AnalysisException thrown if there is an analysis exception
 240  
      */
 241  
     @Override
 242  
     public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
 243  2
         final File f = new File(dependency.getActualFilePath());
 244  2
         final File tmpDir = getNextTempDirectory();
 245  2
         extractFiles(f, tmpDir, engine);
 246  
 
 247  
         //make a copy
 248  2
         final List<Dependency> dependencySet = findMoreDependencies(engine, tmpDir);
 249  
 
 250  2
         if (dependencySet != null && !dependencySet.isEmpty()) {
 251  0
             for (Dependency d : dependencySet) {
 252  0
                 if (d.getFilePath().startsWith(tmpDir.getAbsolutePath())) {
 253  
                     //fix the dependency's display name and path
 254  0
                     final String displayPath = String.format("%s%s",
 255  0
                             dependency.getFilePath(),
 256  0
                             d.getActualFilePath().substring(tmpDir.getAbsolutePath().length()));
 257  0
                     final String displayName = String.format("%s: %s",
 258  0
                             dependency.getFileName(),
 259  0
                             d.getFileName());
 260  0
                     d.setFilePath(displayPath);
 261  0
                     d.setFileName(displayName);
 262  0
                     d.setProjectReferences(dependency.getProjectReferences());
 263  
 
 264  
                     //TODO - can we get more evidence from the parent? EAR contains module name, etc.
 265  
                     //analyze the dependency (i.e. extract files) if it is a supported type.
 266  0
                     if (this.accept(d.getActualFile()) && scanDepth < MAX_SCAN_DEPTH) {
 267  0
                         scanDepth += 1;
 268  0
                         analyze(d, engine);
 269  0
                         scanDepth -= 1;
 270  
                     }
 271  0
                 } else {
 272  0
                     for (Dependency sub : dependencySet) {
 273  0
                         if (sub.getFilePath().startsWith(tmpDir.getAbsolutePath())) {
 274  0
                             final String displayPath = String.format("%s%s",
 275  0
                                     dependency.getFilePath(),
 276  0
                                     sub.getActualFilePath().substring(tmpDir.getAbsolutePath().length()));
 277  0
                             final String displayName = String.format("%s: %s",
 278  0
                                     dependency.getFileName(),
 279  0
                                     sub.getFileName());
 280  0
                             sub.setFilePath(displayPath);
 281  0
                             sub.setFileName(displayName);
 282  
                         }
 283  0
                     }
 284  
                 }
 285  0
             }
 286  
         }
 287  2
         if (REMOVE_FROM_ANALYSIS.accept(dependency.getActualFile())) {
 288  0
             addDisguisedJarsToDependencies(dependency, engine);
 289  0
             engine.getDependencies().remove(dependency);
 290  
         }
 291  2
         Collections.sort(engine.getDependencies());
 292  2
     }
 293  
 
 294  
     /**
 295  
      * If a zip file was identified as a possible JAR, this method will add the
 296  
      * zip to the list of dependencies.
 297  
      *
 298  
      * @param dependency the zip file
 299  
      * @param engine the engine
 300  
      * @throws AnalysisException thrown if there is an issue
 301  
      */
 302  
     private void addDisguisedJarsToDependencies(Dependency dependency, Engine engine) throws AnalysisException {
 303  0
         if (ZIP_FILTER.accept(dependency.getActualFile()) && isZipFileActuallyJarFile(dependency)) {
 304  0
             final File tdir = getNextTempDirectory();
 305  0
             final String fileName = dependency.getFileName();
 306  
 
 307  0
             LOGGER.info("The zip file '{}' appears to be a JAR file, making a copy and analyzing it as a JAR.", fileName);
 308  0
             final File tmpLoc = new File(tdir, fileName.substring(0, fileName.length() - 3) + "jar");
 309  
             //store the archives sha1 and change it so that the engine doesn't think the zip and jar file are the same
 310  
             // and add it is a related dependency.
 311  0
             final String archiveSha1 = dependency.getSha1sum();
 312  
             try {
 313  0
                 dependency.setSha1sum("");
 314  0
                 org.apache.commons.io.FileUtils.copyFile(dependency.getActualFile(), tmpLoc);
 315  0
                 final List<Dependency> dependencySet = findMoreDependencies(engine, tmpLoc);
 316  0
                 if (dependencySet != null && !dependencySet.isEmpty()) {
 317  0
                     for (Dependency d : dependencySet) {
 318  
                         //fix the dependency's display name and path
 319  0
                         if (d.getActualFile().equals(tmpLoc)) {
 320  0
                             d.setFilePath(dependency.getFilePath());
 321  0
                             d.setDisplayFileName(dependency.getFileName());
 322  
                         } else {
 323  0
                             for (Dependency sub : d.getRelatedDependencies()) {
 324  0
                                 if (sub.getActualFile().equals(tmpLoc)) {
 325  0
                                     sub.setFilePath(dependency.getFilePath());
 326  0
                                     sub.setDisplayFileName(dependency.getFileName());
 327  
                                 }
 328  0
                             }
 329  
                         }
 330  0
                     }
 331  
                 }
 332  0
             } catch (IOException ex) {
 333  0
                 LOGGER.debug("Unable to perform deep copy on '{}'", dependency.getActualFile().getPath(), ex);
 334  
             } finally {
 335  0
                 dependency.setSha1sum(archiveSha1);
 336  0
             }
 337  
         }
 338  0
     }
 339  
 
 340  
     /**
 341  
      * Scan the given file/folder, and return any new dependencies found.
 342  
      *
 343  
      * @param engine used to scan
 344  
      * @param file target of scanning
 345  
      * @return any dependencies that weren't known to the engine before
 346  
      */
 347  
     private static List<Dependency> findMoreDependencies(Engine engine, File file) {
 348  2
         final List<Dependency> added = engine.scan(file);
 349  2
         return added;
 350  
     }
 351  
 
 352  
     /**
 353  
      * Retrieves the next temporary directory to extract an archive too.
 354  
      *
 355  
      * @return a directory
 356  
      * @throws AnalysisException thrown if unable to create temporary directory
 357  
      */
 358  
     private File getNextTempDirectory() throws AnalysisException {
 359  2
         dirCount += 1;
 360  2
         final File directory = new File(tempFileLocation, String.valueOf(dirCount));
 361  
         //getting an exception for some directories not being able to be created; might be because the directory already exists?
 362  2
         if (directory.exists()) {
 363  0
             return getNextTempDirectory();
 364  
         }
 365  2
         if (!directory.mkdirs()) {
 366  0
             final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath());
 367  0
             throw new AnalysisException(msg);
 368  
         }
 369  2
         return directory;
 370  
     }
 371  
 
 372  
     /**
 373  
      * Extracts the contents of an archive into the specified directory.
 374  
      *
 375  
      * @param archive an archive file such as a WAR or EAR
 376  
      * @param destination a directory to extract the contents to
 377  
      * @param engine the scanning engine
 378  
      * @throws AnalysisException thrown if the archive is not found
 379  
      */
 380  
     private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException {
 381  2
         if (archive != null && destination != null) {
 382  2
             String archiveExt = FileUtils.getFileExtension(archive.getName());
 383  2
             if (archiveExt == null) {
 384  0
                 return;
 385  
             }
 386  2
             archiveExt = archiveExt.toLowerCase();
 387  
 
 388  
             final FileInputStream fis;
 389  
             try {
 390  2
                 fis = new FileInputStream(archive);
 391  0
             } catch (FileNotFoundException ex) {
 392  0
                 LOGGER.debug("", ex);
 393  0
                 throw new AnalysisException("Archive file was not found.", ex);
 394  2
             }
 395  2
             BufferedInputStream in = null;
 396  2
             ZipArchiveInputStream zin = null;
 397  2
             TarArchiveInputStream tin = null;
 398  2
             GzipCompressorInputStream gin = null;
 399  2
             BZip2CompressorInputStream bzin = null;
 400  
             try {
 401  2
                 if (ZIPPABLES.contains(archiveExt)) {
 402  2
                     in = new BufferedInputStream(fis);
 403  2
                     ensureReadableJar(archiveExt, in);
 404  2
                     zin = new ZipArchiveInputStream(in);
 405  2
                     extractArchive(zin, destination, engine);
 406  0
                 } else if ("tar".equals(archiveExt)) {
 407  0
                     in = new BufferedInputStream(fis);
 408  0
                     tin = new TarArchiveInputStream(in);
 409  0
                     extractArchive(tin, destination, engine);
 410  0
                 } else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) {
 411  0
                     final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName());
 412  0
                     final File f = new File(destination, uncompressedName);
 413  0
                     if (engine.accept(f)) {
 414  0
                         in = new BufferedInputStream(fis);
 415  0
                         gin = new GzipCompressorInputStream(in);
 416  0
                         decompressFile(gin, f);
 417  
                     }
 418  0
                 } else if ("bz2".equals(archiveExt) || "tbz2".equals(archiveExt)) {
 419  0
                     final String uncompressedName = BZip2Utils.getUncompressedFilename(archive.getName());
 420  0
                     final File f = new File(destination, uncompressedName);
 421  0
                     if (engine.accept(f)) {
 422  0
                         in = new BufferedInputStream(fis);
 423  0
                         bzin = new BZip2CompressorInputStream(in);
 424  0
                         decompressFile(bzin, f);
 425  
                     }
 426  
                 }
 427  0
             } catch (ArchiveExtractionException ex) {
 428  0
                 LOGGER.warn("Exception extracting archive '{}'.", archive.getName());
 429  0
                 LOGGER.debug("", ex);
 430  0
             } catch (IOException ex) {
 431  0
                 LOGGER.warn("Exception reading archive '{}'.", archive.getName());
 432  0
                 LOGGER.debug("", ex);
 433  
             } finally {
 434  
                 //overly verbose and not needed... but keeping it anyway due to
 435  
                 //having issue with file handles being left open
 436  2
                 FileUtils.close(fis);
 437  2
                 FileUtils.close(in);
 438  2
                 FileUtils.close(zin);
 439  2
                 FileUtils.close(tin);
 440  2
                 FileUtils.close(gin);
 441  2
                 FileUtils.close(bzin);
 442  2
             }
 443  
         }
 444  2
     }
 445  
 
 446  
     /**
 447  
      * Checks if the file being scanned is a JAR that begins with '#!/bin' which
 448  
      * indicates it is a fully executable jar. If a fully executable JAR is
 449  
      * identified the input stream will be advanced to the start of the actual
 450  
      * JAR file ( skipping the script).
 451  
      *
 452  
      * @see
 453  
      * <a href="http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/reference/htmlsingle/#deployment-install">Installing
 454  
      * Spring Boot Applications</a>
 455  
      * @param archiveExt the file extension
 456  
      * @param in the input stream
 457  
      * @throws IOException thrown if there is an error reading the stream
 458  
      */
 459  
     private void ensureReadableJar(final String archiveExt, BufferedInputStream in) throws IOException {
 460  2
         if ("jar".equals(archiveExt) && in.markSupported()) {
 461  2
             in.mark(7);
 462  2
             final byte[] b = new byte[7];
 463  2
             final int read = in.read(b);
 464  2
             if (read == 7
 465  
                     && b[0] == '#'
 466  
                     && b[1] == '!'
 467  
                     && b[2] == '/'
 468  
                     && b[3] == 'b'
 469  
                     && b[4] == 'i'
 470  
                     && b[5] == 'n'
 471  
                     && b[6] == '/') {
 472  0
                 boolean stillLooking = true;
 473  
                 int chr, nxtChr;
 474  0
                 while (stillLooking && (chr = in.read()) != -1) {
 475  0
                     if (chr == '\n' || chr == '\r') {
 476  0
                         in.mark(4);
 477  0
                         if ((chr = in.read()) != -1) {
 478  0
                             if (chr == 'P' && (chr = in.read()) != -1) {
 479  0
                                 if (chr == 'K' && (chr = in.read()) != -1) {
 480  0
                                     if ((chr == 3 || chr == 5 || chr == 7) && (nxtChr = in.read()) != -1) {
 481  0
                                         if (nxtChr == chr + 1) {
 482  0
                                             stillLooking = false;
 483  0
                                             in.reset();
 484  
                                         }
 485  
                                     }
 486  
                                 }
 487  
                             }
 488  
                         }
 489  
                     }
 490  
                 }
 491  0
             } else {
 492  2
                 in.reset();
 493  
             }
 494  
         }
 495  2
     }
 496  
 
 497  
     /**
 498  
      * Extracts files from an archive.
 499  
      *
 500  
      * @param input the archive to extract files from
 501  
      * @param destination the location to write the files too
 502  
      * @param engine the dependency-check engine
 503  
      * @throws ArchiveExtractionException thrown if there is an exception
 504  
      * extracting files from the archive
 505  
      */
 506  
     private void extractArchive(ArchiveInputStream input, File destination, Engine engine) throws ArchiveExtractionException {
 507  
         ArchiveEntry entry;
 508  
         try {
 509  887
             while ((entry = input.getNextEntry()) != null) {
 510  885
                 final File file = new File(destination, entry.getName());
 511  885
                 if (entry.isDirectory()) {
 512  36
                     if (!file.exists() && !file.mkdirs()) {
 513  0
                         final String msg = String.format("Unable to create directory '%s'.", file.getAbsolutePath());
 514  0
                         throw new AnalysisException(msg);
 515  
                     }
 516  849
                 } else if (engine.accept(file)) {
 517  0
                     extractAcceptedFile(input, file);
 518  
                 }
 519  885
             }
 520  0
         } catch (Throwable ex) {
 521  0
             throw new ArchiveExtractionException(ex);
 522  
         } finally {
 523  2
             FileUtils.close(input);
 524  2
         }
 525  2
     }
 526  
 
 527  
     /**
 528  
      * Extracts a file from an archive.
 529  
      *
 530  
      * @param input the archives input stream
 531  
      * @param file the file to extract
 532  
      * @throws AnalysisException thrown if there is an error
 533  
      */
 534  
     private static void extractAcceptedFile(ArchiveInputStream input, File file) throws AnalysisException {
 535  0
         LOGGER.debug("Extracting '{}'", file.getPath());
 536  0
         FileOutputStream fos = null;
 537  
         try {
 538  0
             final File parent = file.getParentFile();
 539  0
             if (!parent.isDirectory() && !parent.mkdirs()) {
 540  0
                 final String msg = String.format("Unable to build directory '%s'.", parent.getAbsolutePath());
 541  0
                 throw new AnalysisException(msg);
 542  
             }
 543  0
             fos = new FileOutputStream(file);
 544  0
             IOUtils.copy(input, fos);
 545  0
         } catch (FileNotFoundException ex) {
 546  0
             LOGGER.debug("", ex);
 547  0
             final String msg = String.format("Unable to find file '%s'.", file.getName());
 548  0
             throw new AnalysisException(msg, ex);
 549  0
         } catch (IOException ex) {
 550  0
             LOGGER.debug("", ex);
 551  0
             final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
 552  0
             throw new AnalysisException(msg, ex);
 553  
         } finally {
 554  0
             FileUtils.close(fos);
 555  0
         }
 556  0
     }
 557  
 
 558  
     /**
 559  
      * Decompresses a file.
 560  
      *
 561  
      * @param inputStream the compressed file
 562  
      * @param outputFile the location to write the decompressed file
 563  
      * @throws ArchiveExtractionException thrown if there is an exception
 564  
      * decompressing the file
 565  
      */
 566  
     private void decompressFile(CompressorInputStream inputStream, File outputFile) throws ArchiveExtractionException {
 567  0
         LOGGER.debug("Decompressing '{}'", outputFile.getPath());
 568  0
         FileOutputStream out = null;
 569  
         try {
 570  0
             out = new FileOutputStream(outputFile);
 571  0
             IOUtils.copy(inputStream, out);
 572  0
         } catch (FileNotFoundException ex) {
 573  0
             LOGGER.debug("", ex);
 574  0
             throw new ArchiveExtractionException(ex);
 575  0
         } catch (IOException ex) {
 576  0
             LOGGER.debug("", ex);
 577  0
             throw new ArchiveExtractionException(ex);
 578  
         } finally {
 579  0
             FileUtils.close(out);
 580  0
         }
 581  0
     }
 582  
 
 583  
     /**
 584  
      * Attempts to determine if a zip file is actually a JAR file.
 585  
      *
 586  
      * @param dependency the dependency to check
 587  
      * @return true if the dependency appears to be a JAR file; otherwise false
 588  
      */
 589  
     private boolean isZipFileActuallyJarFile(Dependency dependency) {
 590  0
         boolean isJar = false;
 591  0
         ZipFile zip = null;
 592  
         try {
 593  0
             zip = new ZipFile(dependency.getActualFilePath());
 594  0
             if (zip.getEntry("META-INF/MANIFEST.MF") != null
 595  0
                     || zip.getEntry("META-INF/maven") != null) {
 596  0
                 final Enumeration<ZipArchiveEntry> entries = zip.getEntries();
 597  0
                 while (entries.hasMoreElements()) {
 598  0
                     final ZipArchiveEntry entry = entries.nextElement();
 599  0
                     if (!entry.isDirectory()) {
 600  0
                         final String name = entry.getName().toLowerCase();
 601  0
                         if (name.endsWith(".class")) {
 602  0
                             isJar = true;
 603  0
                             break;
 604  
                         }
 605  
                     }
 606  0
                 }
 607  
             }
 608  0
         } catch (IOException ex) {
 609  0
             LOGGER.debug("Unable to unzip zip file '{}'", dependency.getFilePath(), ex);
 610  
         } finally {
 611  0
             ZipFile.closeQuietly(zip);
 612  0
         }
 613  
 
 614  0
         return isJar;
 615  
     }
 616  
 }