| 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.Closeable; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileInputStream; |
| 24 | |
import java.io.FileNotFoundException; |
| 25 | |
import java.io.FileOutputStream; |
| 26 | |
import java.io.FilenameFilter; |
| 27 | |
import java.io.IOException; |
| 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 | |
public static void extractFiles(File archive, File extractTo) throws ExtractionException { |
| 67 | 0 | extractFiles(archive, extractTo, null); |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException { |
| 80 | 0 | if (archive == null || extractTo == null) { |
| 81 | 0 | return; |
| 82 | |
} |
| 83 | |
|
| 84 | 0 | FileInputStream fis = null; |
| 85 | 0 | ZipInputStream zis = null; |
| 86 | |
|
| 87 | |
try { |
| 88 | 0 | fis = new FileInputStream(archive); |
| 89 | 0 | } catch (FileNotFoundException ex) { |
| 90 | 0 | LOGGER.debug("", ex); |
| 91 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 92 | 0 | } |
| 93 | 0 | zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 94 | |
ZipEntry entry; |
| 95 | |
try { |
| 96 | 0 | while ((entry = zis.getNextEntry()) != null) { |
| 97 | 0 | if (entry.isDirectory()) { |
| 98 | 0 | final File d = new File(extractTo, entry.getName()); |
| 99 | 0 | if (!d.exists() && !d.mkdirs()) { |
| 100 | 0 | final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath()); |
| 101 | 0 | throw new ExtractionException(msg); |
| 102 | |
} |
| 103 | 0 | } else { |
| 104 | 0 | final File file = new File(extractTo, entry.getName()); |
| 105 | 0 | if (engine == null || engine.accept(file)) { |
| 106 | 0 | FileOutputStream fos = null; |
| 107 | |
try { |
| 108 | 0 | fos = new FileOutputStream(file); |
| 109 | 0 | IOUtils.copy(zis, fos); |
| 110 | 0 | } catch (FileNotFoundException ex) { |
| 111 | 0 | LOGGER.debug("", ex); |
| 112 | 0 | final String msg = String.format("Unable to find file '%s'.", file.getName()); |
| 113 | 0 | throw new ExtractionException(msg, ex); |
| 114 | 0 | } catch (IOException ex) { |
| 115 | 0 | LOGGER.debug("", ex); |
| 116 | 0 | final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); |
| 117 | 0 | throw new ExtractionException(msg, ex); |
| 118 | |
} finally { |
| 119 | 0 | closeStream(fos); |
| 120 | 0 | } |
| 121 | |
} |
| 122 | 0 | } |
| 123 | |
} |
| 124 | 0 | } catch (IOException ex) { |
| 125 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 126 | 0 | LOGGER.debug("", ex); |
| 127 | 0 | throw new ExtractionException(msg, ex); |
| 128 | |
} finally { |
| 129 | 0 | closeStream(zis); |
| 130 | 0 | } |
| 131 | 0 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public static void extractFilesUsingFilter(File archive, File destination, |
| 142 | |
FilenameFilter filter) throws ExtractionException { |
| 143 | 3 | if (archive == null || destination == null) { |
| 144 | 0 | return; |
| 145 | |
} |
| 146 | |
|
| 147 | 3 | FileInputStream fis = null; |
| 148 | |
try { |
| 149 | 3 | fis = new FileInputStream(archive); |
| 150 | 0 | } catch (FileNotFoundException ex) { |
| 151 | 0 | LOGGER.debug("", ex); |
| 152 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 153 | 3 | } |
| 154 | |
try { |
| 155 | 3 | extractArchive(new ZipArchiveInputStream(new BufferedInputStream( |
| 156 | |
fis)), destination, filter); |
| 157 | 0 | } catch (ArchiveExtractionException ex) { |
| 158 | 0 | LOGGER.warn("Exception extracting archive '{}'.", archive.getName()); |
| 159 | 0 | LOGGER.debug("", ex); |
| 160 | |
} finally { |
| 161 | 0 | try { |
| 162 | 3 | fis.close(); |
| 163 | 0 | } catch (IOException ex) { |
| 164 | 0 | LOGGER.debug("", ex); |
| 165 | 3 | } |
| 166 | 0 | } |
| 167 | 3 | } |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
private static void extractArchive(ArchiveInputStream input, |
| 178 | |
File destination, FilenameFilter filter) |
| 179 | |
throws ArchiveExtractionException { |
| 180 | |
ArchiveEntry entry; |
| 181 | |
try { |
| 182 | 36 | while ((entry = input.getNextEntry()) != null) { |
| 183 | 33 | if (entry.isDirectory()) { |
| 184 | 0 | final File dir = new File(destination, entry.getName()); |
| 185 | 0 | if (!dir.exists() && !dir.mkdirs()) { |
| 186 | 0 | final String msg = String.format( |
| 187 | |
"Unable to create directory '%s'.", |
| 188 | 0 | dir.getAbsolutePath()); |
| 189 | 0 | throw new AnalysisException(msg); |
| 190 | |
} |
| 191 | 0 | } else { |
| 192 | 33 | extractFile(input, destination, filter, entry); |
| 193 | |
} |
| 194 | |
} |
| 195 | 0 | } catch (IOException ex) { |
| 196 | 0 | throw new ArchiveExtractionException(ex); |
| 197 | 0 | } catch (Throwable ex) { |
| 198 | 0 | throw new ArchiveExtractionException(ex); |
| 199 | |
} finally { |
| 200 | 3 | closeStream(input); |
| 201 | 3 | } |
| 202 | 3 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
private static void extractFile(ArchiveInputStream input, File destination, |
| 214 | |
FilenameFilter filter, ArchiveEntry entry) throws ExtractionException { |
| 215 | 33 | final File file = new File(destination, entry.getName()); |
| 216 | 33 | if (filter.accept(file.getParentFile(), file.getName())) { |
| 217 | 6 | LOGGER.debug("Extracting '{}'", |
| 218 | 3 | file.getPath()); |
| 219 | 3 | FileOutputStream fos = null; |
| 220 | |
try { |
| 221 | 3 | createParentFile(file); |
| 222 | 3 | fos = new FileOutputStream(file); |
| 223 | 3 | IOUtils.copy(input, fos); |
| 224 | 0 | } catch (FileNotFoundException ex) { |
| 225 | 0 | LOGGER.debug("", ex); |
| 226 | 0 | final String msg = String.format("Unable to find file '%s'.", |
| 227 | 0 | file.getName()); |
| 228 | 0 | throw new ExtractionException(msg, ex); |
| 229 | 0 | } catch (IOException ex) { |
| 230 | 0 | LOGGER.debug("", ex); |
| 231 | 0 | final String msg = String |
| 232 | 0 | .format("IO Exception while parsing file '%s'.", |
| 233 | 0 | file.getName()); |
| 234 | 0 | throw new ExtractionException(msg, ex); |
| 235 | |
} finally { |
| 236 | 3 | closeStream(fos); |
| 237 | 3 | } |
| 238 | |
} |
| 239 | 33 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
private static void closeStream(Closeable stream) { |
| 247 | 6 | if (stream != null) { |
| 248 | |
try { |
| 249 | 6 | stream.close(); |
| 250 | 0 | } catch (IOException ex) { |
| 251 | 0 | LOGGER.trace("", ex); |
| 252 | 6 | } |
| 253 | |
} |
| 254 | 6 | } |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private static void createParentFile(final File file) |
| 263 | |
throws ExtractionException { |
| 264 | 3 | final File parent = file.getParentFile(); |
| 265 | 3 | if (!parent.isDirectory() && !parent.mkdirs()) { |
| 266 | 0 | final String msg = String.format( |
| 267 | |
"Unable to build directory '%s'.", |
| 268 | 0 | parent.getAbsolutePath()); |
| 269 | 0 | throw new ExtractionException(msg); |
| 270 | |
} |
| 271 | 3 | } |
| 272 | |
} |