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