| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 8 | public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(ArchiveAnalyzer.class); |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 1 | private static int dirCount = 0; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 8 | private File tempFileLocation = null; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | 1 | private static final int MAX_SCAN_DEPTH = Settings.getInt("archive.scan.depth", 3); |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 8 | private int scanDepth = 0; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
private static final String ANALYZER_NAME = "Archive Analyzer"; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INITIAL; |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 1 | private static final Set<String> ZIPPABLES = newHashSet("zip", "ear", "war", "jar", "sar", "apk", "nupkg"); |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | 1 | private static final Set<String> EXTENSIONS = newHashSet("tar", "gz", "tgz", "bz2", "tbz2"); |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 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 | |
|
| 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 | |
|
| 137 | |
|
| 138 | 1 | private static final FileFilter ZIP_FILTER = FileFilterBuilder.newInstance().addExtensions("zip").build(); |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
@Override |
| 146 | |
public String getName() { |
| 147 | 22 | return ANALYZER_NAME; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
@Override |
| 156 | |
public AnalysisPhase getAnalysisPhase() { |
| 157 | 6 | return ANALYSIS_PHASE; |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
@Override |
| 168 | |
protected String getAnalyzerEnabledSettingKey() { |
| 169 | 2 | return Settings.KEYS.ANALYZER_ARCHIVE_ENABLED; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 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 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 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 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
@Override |
| 228 | |
public boolean supportsParallelProcessing() { |
| 229 | 1 | return false; |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 265 | |
|
| 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 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 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 | |
|
| 310 | |
|
| 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 | |
|
| 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 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 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 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
private File getNextTempDirectory() throws AnalysisException { |
| 359 | 2 | dirCount += 1; |
| 360 | 2 | final File directory = new File(tempFileLocation, String.valueOf(dirCount)); |
| 361 | |
|
| 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 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 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 | |
|
| 435 | |
|
| 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 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 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 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 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 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 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 | |
|
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 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 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
|
| 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 | |
} |