| 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.BufferedOutputStream; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileInputStream; |
| 24 | |
import java.io.FileNotFoundException; |
| 25 | |
import java.io.FileOutputStream; |
| 26 | |
import java.io.IOException; |
| 27 | |
import java.util.logging.Level; |
| 28 | |
import java.util.logging.Logger; |
| 29 | |
import java.util.zip.ZipEntry; |
| 30 | |
import java.util.zip.ZipInputStream; |
| 31 | |
import org.owasp.dependencycheck.Engine; |
| 32 | |
import static org.owasp.dependencycheck.utils.FileUtils.getFileExtension; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class ExtractionUtil { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 0 | private static final Logger LOGGER = Logger.getLogger(ExtractionUtil.class.getName()); |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
private static final int BUFFER_SIZE = 4096; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private ExtractionUtil() { |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static void extractFiles(File archive, File extractTo) throws ExtractionException { |
| 63 | 0 | extractFiles(archive, extractTo, null); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException { |
| 77 | 0 | if (archive == null || extractTo == null) { |
| 78 | 0 | return; |
| 79 | |
} |
| 80 | |
|
| 81 | 0 | FileInputStream fis = null; |
| 82 | 0 | ZipInputStream zis = null; |
| 83 | |
|
| 84 | |
try { |
| 85 | 0 | fis = new FileInputStream(archive); |
| 86 | 0 | } catch (FileNotFoundException ex) { |
| 87 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 88 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 89 | 0 | } |
| 90 | 0 | zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 91 | |
ZipEntry entry; |
| 92 | |
try { |
| 93 | 0 | while ((entry = zis.getNextEntry()) != null) { |
| 94 | 0 | if (entry.isDirectory()) { |
| 95 | 0 | final File d = new File(extractTo, entry.getName()); |
| 96 | 0 | if (!d.exists() && !d.mkdirs()) { |
| 97 | 0 | final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath()); |
| 98 | 0 | throw new ExtractionException(msg); |
| 99 | |
} |
| 100 | 0 | } else { |
| 101 | 0 | final File file = new File(extractTo, entry.getName()); |
| 102 | 0 | final String ext = getFileExtension(file.getName()); |
| 103 | 0 | if (engine == null || engine.supportsExtension(ext)) { |
| 104 | 0 | BufferedOutputStream bos = null; |
| 105 | |
FileOutputStream fos; |
| 106 | |
try { |
| 107 | 0 | fos = new FileOutputStream(file); |
| 108 | 0 | bos = new BufferedOutputStream(fos, BUFFER_SIZE); |
| 109 | |
int count; |
| 110 | 0 | final byte data[] = new byte[BUFFER_SIZE]; |
| 111 | 0 | while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { |
| 112 | 0 | bos.write(data, 0, count); |
| 113 | |
} |
| 114 | 0 | bos.flush(); |
| 115 | 0 | } catch (FileNotFoundException ex) { |
| 116 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 117 | 0 | final String msg = String.format("Unable to find file '%s'.", file.getName()); |
| 118 | 0 | throw new ExtractionException(msg, ex); |
| 119 | 0 | } catch (IOException ex) { |
| 120 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 121 | 0 | final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); |
| 122 | 0 | throw new ExtractionException(msg, ex); |
| 123 | |
} finally { |
| 124 | 0 | if (bos != null) { |
| 125 | |
try { |
| 126 | 0 | bos.close(); |
| 127 | 0 | } catch (IOException ex) { |
| 128 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 129 | 0 | } |
| 130 | |
} |
| 131 | |
} |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
} |
| 135 | 0 | } catch (IOException ex) { |
| 136 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 137 | 0 | LOGGER.log(Level.FINE, msg, ex); |
| 138 | 0 | throw new ExtractionException(msg, ex); |
| 139 | |
} finally { |
| 140 | 0 | try { |
| 141 | 0 | zis.close(); |
| 142 | 0 | } catch (IOException ex) { |
| 143 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 144 | 0 | } |
| 145 | 0 | } |
| 146 | 0 | } |
| 147 | |
} |