| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.utils; |
| 19 | |
|
| 20 | |
import java.io.BufferedInputStream; |
| 21 | |
import java.io.File; |
| 22 | |
import java.io.FileInputStream; |
| 23 | |
import java.io.FileNotFoundException; |
| 24 | |
import java.io.FileOutputStream; |
| 25 | |
import java.io.FilenameFilter; |
| 26 | |
import java.io.IOException; |
| 27 | |
import java.util.zip.GZIPInputStream; |
| 28 | |
import java.util.zip.ZipEntry; |
| 29 | |
import java.util.zip.ZipInputStream; |
| 30 | |
|
| 31 | |
import org.apache.commons.compress.archivers.ArchiveEntry; |
| 32 | |
import org.apache.commons.compress.archivers.ArchiveInputStream; |
| 33 | |
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; |
| 34 | |
import org.apache.commons.compress.utils.IOUtils; |
| 35 | |
import org.owasp.dependencycheck.Engine; |
| 36 | |
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; |
| 37 | |
import org.owasp.dependencycheck.analyzer.exception.ArchiveExtractionException; |
| 38 | |
import org.slf4j.Logger; |
| 39 | |
import org.slf4j.LoggerFactory; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public final class ExtractionUtil { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(ExtractionUtil.class); |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | private ExtractionUtil() { |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public static void extractFiles(File archive, File extractTo) throws ExtractionException { |
| 68 | 0 | extractFiles(archive, extractTo, null); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException { |
| 84 | 0 | if (archive == null || extractTo == null) { |
| 85 | 0 | return; |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | FileInputStream fis = null; |
| 89 | 0 | ZipInputStream zis = null; |
| 90 | |
|
| 91 | |
try { |
| 92 | 0 | fis = new FileInputStream(archive); |
| 93 | 0 | } catch (FileNotFoundException ex) { |
| 94 | 0 | LOGGER.debug("", ex); |
| 95 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 96 | 0 | } |
| 97 | 0 | zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 98 | |
ZipEntry entry; |
| 99 | |
try { |
| 100 | 0 | while ((entry = zis.getNextEntry()) != null) { |
| 101 | 0 | if (entry.isDirectory()) { |
| 102 | 0 | final File d = new File(extractTo, entry.getName()); |
| 103 | 0 | if (!d.exists() && !d.mkdirs()) { |
| 104 | 0 | final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath()); |
| 105 | 0 | throw new ExtractionException(msg); |
| 106 | |
} |
| 107 | 0 | } else { |
| 108 | 0 | final File file = new File(extractTo, entry.getName()); |
| 109 | 0 | if (engine == null || engine.accept(file)) { |
| 110 | 0 | FileOutputStream fos = null; |
| 111 | |
try { |
| 112 | 0 | fos = new FileOutputStream(file); |
| 113 | 0 | IOUtils.copy(zis, fos); |
| 114 | 0 | } catch (FileNotFoundException ex) { |
| 115 | 0 | LOGGER.debug("", ex); |
| 116 | 0 | final String msg = String.format("Unable to find file '%s'.", file.getName()); |
| 117 | 0 | throw new ExtractionException(msg, ex); |
| 118 | 0 | } catch (IOException ex) { |
| 119 | 0 | LOGGER.debug("", ex); |
| 120 | 0 | final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); |
| 121 | 0 | throw new ExtractionException(msg, ex); |
| 122 | |
} finally { |
| 123 | 0 | FileUtils.close(fos); |
| 124 | 0 | } |
| 125 | |
} |
| 126 | 0 | } |
| 127 | |
} |
| 128 | 0 | } catch (IOException ex) { |
| 129 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 130 | 0 | LOGGER.debug("", ex); |
| 131 | 0 | throw new ExtractionException(msg, ex); |
| 132 | |
} finally { |
| 133 | 0 | FileUtils.close(zis); |
| 134 | 0 | } |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public static void extractFilesUsingFilter(File archive, File destination, |
| 146 | |
FilenameFilter filter) throws ExtractionException { |
| 147 | 3 | if (archive == null || destination == null) { |
| 148 | 0 | return; |
| 149 | |
} |
| 150 | |
|
| 151 | 3 | FileInputStream fis = null; |
| 152 | |
try { |
| 153 | 3 | fis = new FileInputStream(archive); |
| 154 | 0 | } catch (FileNotFoundException ex) { |
| 155 | 0 | LOGGER.debug("", ex); |
| 156 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 157 | 3 | } |
| 158 | |
try { |
| 159 | 3 | extractArchive(new ZipArchiveInputStream(new BufferedInputStream( |
| 160 | |
fis)), destination, filter); |
| 161 | 0 | } catch (ArchiveExtractionException ex) { |
| 162 | 0 | LOGGER.warn("Exception extracting archive '{}'.", archive.getName()); |
| 163 | 0 | LOGGER.debug("", ex); |
| 164 | |
} finally { |
| 165 | 0 | try { |
| 166 | 3 | fis.close(); |
| 167 | 0 | } catch (IOException ex) { |
| 168 | 0 | LOGGER.debug("", ex); |
| 169 | 3 | } |
| 170 | 0 | } |
| 171 | 3 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
private static void extractArchive(ArchiveInputStream input, |
| 183 | |
File destination, FilenameFilter filter) |
| 184 | |
throws ArchiveExtractionException { |
| 185 | |
ArchiveEntry entry; |
| 186 | |
try { |
| 187 | 36 | while ((entry = input.getNextEntry()) != null) { |
| 188 | 33 | if (entry.isDirectory()) { |
| 189 | 0 | final File dir = new File(destination, entry.getName()); |
| 190 | 0 | if (!dir.exists() && !dir.mkdirs()) { |
| 191 | 0 | final String msg = String.format( |
| 192 | |
"Unable to create directory '%s'.", |
| 193 | 0 | dir.getAbsolutePath()); |
| 194 | 0 | throw new AnalysisException(msg); |
| 195 | |
} |
| 196 | 0 | } else { |
| 197 | 33 | extractFile(input, destination, filter, entry); |
| 198 | |
} |
| 199 | |
} |
| 200 | 0 | } catch (IOException ex) { |
| 201 | 0 | throw new ArchiveExtractionException(ex); |
| 202 | 0 | } catch (Throwable ex) { |
| 203 | 0 | throw new ArchiveExtractionException(ex); |
| 204 | |
} finally { |
| 205 | 3 | FileUtils.close(input); |
| 206 | 3 | } |
| 207 | 3 | } |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private static void extractFile(ArchiveInputStream input, File destination, |
| 221 | |
FilenameFilter filter, ArchiveEntry entry) throws ExtractionException { |
| 222 | 33 | final File file = new File(destination, entry.getName()); |
| 223 | 33 | if (filter.accept(file.getParentFile(), file.getName())) { |
| 224 | 6 | LOGGER.debug("Extracting '{}'", |
| 225 | 3 | file.getPath()); |
| 226 | 3 | FileOutputStream fos = null; |
| 227 | |
try { |
| 228 | 3 | createParentFile(file); |
| 229 | 3 | fos = new FileOutputStream(file); |
| 230 | 3 | IOUtils.copy(input, fos); |
| 231 | 0 | } catch (FileNotFoundException ex) { |
| 232 | 0 | LOGGER.debug("", ex); |
| 233 | 0 | final String msg = String.format("Unable to find file '%s'.", |
| 234 | 0 | file.getName()); |
| 235 | 0 | throw new ExtractionException(msg, ex); |
| 236 | 0 | } catch (IOException ex) { |
| 237 | 0 | LOGGER.debug("", ex); |
| 238 | 0 | final String msg = String |
| 239 | 0 | .format("IO Exception while parsing file '%s'.", |
| 240 | 0 | file.getName()); |
| 241 | 0 | throw new ExtractionException(msg, ex); |
| 242 | |
} finally { |
| 243 | 3 | FileUtils.close(fos); |
| 244 | 3 | } |
| 245 | |
} |
| 246 | 33 | } |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
private static void createParentFile(final File file) |
| 257 | |
throws ExtractionException { |
| 258 | 3 | final File parent = file.getParentFile(); |
| 259 | 3 | if (!parent.isDirectory() && !parent.mkdirs()) { |
| 260 | 0 | final String msg = String.format( |
| 261 | |
"Unable to build directory '%s'.", |
| 262 | 0 | parent.getAbsolutePath()); |
| 263 | 0 | throw new ExtractionException(msg); |
| 264 | |
} |
| 265 | 3 | } |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public static void extractGzip(File file) throws FileNotFoundException, IOException { |
| 276 | 2 | final String originalPath = file.getPath(); |
| 277 | 2 | final File gzip = new File(originalPath + ".gz"); |
| 278 | 2 | if (gzip.isFile() && !gzip.delete()) { |
| 279 | 0 | LOGGER.debug("Failed to delete initial temporary file when extracting 'gz' {}", gzip.toString()); |
| 280 | 0 | gzip.deleteOnExit(); |
| 281 | |
} |
| 282 | 2 | if (!file.renameTo(gzip)) { |
| 283 | 0 | throw new IOException("Unable to rename '" + file.getPath() + "'"); |
| 284 | |
} |
| 285 | 2 | final File newfile = new File(originalPath); |
| 286 | |
|
| 287 | 2 | final byte[] buffer = new byte[4096]; |
| 288 | |
|
| 289 | 2 | GZIPInputStream cin = null; |
| 290 | 2 | FileOutputStream out = null; |
| 291 | |
try { |
| 292 | 2 | cin = new GZIPInputStream(new FileInputStream(gzip)); |
| 293 | 2 | out = new FileOutputStream(newfile); |
| 294 | |
|
| 295 | |
int len; |
| 296 | 1136 | while ((len = cin.read(buffer)) > 0) { |
| 297 | 1134 | out.write(buffer, 0, len); |
| 298 | |
} |
| 299 | |
} finally { |
| 300 | 2 | if (cin != null) { |
| 301 | |
try { |
| 302 | 2 | cin.close(); |
| 303 | 0 | } catch (IOException ex) { |
| 304 | 0 | LOGGER.trace("ignore", ex); |
| 305 | 2 | } |
| 306 | |
} |
| 307 | 2 | if (out != null) { |
| 308 | |
try { |
| 309 | 2 | out.close(); |
| 310 | 0 | } catch (IOException ex) { |
| 311 | 0 | LOGGER.trace("ignore", ex); |
| 312 | 2 | } |
| 313 | |
} |
| 314 | 2 | if (gzip.isFile() && !org.apache.commons.io.FileUtils.deleteQuietly(gzip)) { |
| 315 | 0 | LOGGER.debug("Failed to delete temporary file when extracting 'gz' {}", gzip.toString()); |
| 316 | 0 | gzip.deleteOnExit(); |
| 317 | |
} |
| 318 | |
} |
| 319 | 2 | } |
| 320 | |
} |