| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.utils; |
| 20 | |
|
| 21 | |
import java.io.BufferedInputStream; |
| 22 | |
import java.io.BufferedOutputStream; |
| 23 | |
import java.io.File; |
| 24 | |
import java.io.FileInputStream; |
| 25 | |
import java.io.FileNotFoundException; |
| 26 | |
import java.io.FileOutputStream; |
| 27 | |
import java.io.IOException; |
| 28 | |
import java.io.UnsupportedEncodingException; |
| 29 | |
import java.net.URLDecoder; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
import java.util.zip.ZipEntry; |
| 33 | |
import java.util.zip.ZipInputStream; |
| 34 | |
import org.owasp.dependencycheck.Engine; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public final class FileUtils { |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
private static final int BUFFER_SIZE = 4096; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | private FileUtils() { |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static String getFileExtension(String fileName) { |
| 61 | 199 | String ret = null; |
| 62 | 199 | final int pos = fileName.lastIndexOf("."); |
| 63 | 199 | if (pos >= 0) { |
| 64 | 189 | ret = fileName.substring(pos + 1, fileName.length()).toLowerCase(); |
| 65 | |
} |
| 66 | 199 | return ret; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static void delete(File file) throws IOException { |
| 77 | 35 | if (file.isDirectory()) { |
| 78 | 32 | for (File c : file.listFiles()) { |
| 79 | 28 | delete(c); |
| 80 | |
} |
| 81 | |
} |
| 82 | 35 | if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) { |
| 83 | |
|
| 84 | 0 | throw new FileNotFoundException("Failed to delete file: " + file); |
| 85 | |
} else { |
| 86 | 35 | file.deleteOnExit(); |
| 87 | |
} |
| 88 | 35 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public static void delete(File file, boolean deleteOnExit) throws IOException { |
| 101 | 0 | if (file.isDirectory()) { |
| 102 | 0 | for (File c : file.listFiles()) { |
| 103 | 0 | delete(c); |
| 104 | |
} |
| 105 | |
} |
| 106 | 0 | if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) { |
| 107 | |
|
| 108 | 0 | if (deleteOnExit) { |
| 109 | 0 | file.deleteOnExit(); |
| 110 | |
} else { |
| 111 | 0 | throw new FileNotFoundException("Failed to delete file: " + file); |
| 112 | |
} |
| 113 | |
} |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
@java.lang.Deprecated |
| 132 | |
public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException { |
| 133 | 0 | final File file = new File(configuredFilePath); |
| 134 | 0 | if (file.isDirectory() && file.canWrite()) { |
| 135 | 0 | return new File(file.getCanonicalPath()); |
| 136 | |
} else { |
| 137 | 0 | final File exePath = getPathToJar(clazz); |
| 138 | 0 | return new File(exePath, configuredFilePath); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
@java.lang.Deprecated |
| 154 | |
public static File getPathToJar(Class clazz) throws UnsupportedEncodingException { |
| 155 | 0 | final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); |
| 156 | 0 | final String decodedPath = URLDecoder.decode(filePath, "UTF-8"); |
| 157 | 0 | final File jarPath = new File(decodedPath); |
| 158 | 0 | return jarPath.getParentFile(); |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public static void extractFiles(File archive, File extractTo) throws ExtractionException { |
| 170 | 2 | extractFiles(archive, extractTo, null); |
| 171 | 2 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException { |
| 186 | 2 | if (archive == null || extractTo == null) { |
| 187 | 0 | return; |
| 188 | |
} |
| 189 | |
|
| 190 | 2 | FileInputStream fis = null; |
| 191 | 2 | ZipInputStream zis = null; |
| 192 | |
|
| 193 | |
try { |
| 194 | 2 | fis = new FileInputStream(archive); |
| 195 | 0 | } catch (FileNotFoundException ex) { |
| 196 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.INFO, null, ex); |
| 197 | 0 | throw new ExtractionException("Archive file was not found.", ex); |
| 198 | 2 | } |
| 199 | 2 | zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 200 | |
ZipEntry entry; |
| 201 | |
try { |
| 202 | 36 | while ((entry = zis.getNextEntry()) != null) { |
| 203 | 34 | if (entry.isDirectory()) { |
| 204 | 4 | final File d = new File(extractTo, entry.getName()); |
| 205 | 4 | if (!d.exists() && !d.mkdirs()) { |
| 206 | 0 | final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath()); |
| 207 | 0 | throw new ExtractionException(msg); |
| 208 | |
} |
| 209 | 4 | } else { |
| 210 | 30 | final File file = new File(extractTo, entry.getName()); |
| 211 | 30 | final String ext = getFileExtension(file.getName()); |
| 212 | 30 | if (engine == null || engine.supportsExtension(ext)) { |
| 213 | 30 | BufferedOutputStream bos = null; |
| 214 | |
FileOutputStream fos; |
| 215 | |
try { |
| 216 | 30 | fos = new FileOutputStream(file); |
| 217 | 30 | bos = new BufferedOutputStream(fos, BUFFER_SIZE); |
| 218 | |
int count; |
| 219 | 30 | final byte data[] = new byte[BUFFER_SIZE]; |
| 220 | 191432 | while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { |
| 221 | 191402 | bos.write(data, 0, count); |
| 222 | |
} |
| 223 | 30 | bos.flush(); |
| 224 | 0 | } catch (FileNotFoundException ex) { |
| 225 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, null, ex); |
| 226 | 0 | final String msg = String.format("Unable to find file '%s'.", file.getName()); |
| 227 | 0 | throw new ExtractionException(msg, ex); |
| 228 | 0 | } catch (IOException ex) { |
| 229 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, null, ex); |
| 230 | 0 | final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); |
| 231 | 0 | throw new ExtractionException(msg, ex); |
| 232 | |
} finally { |
| 233 | 30 | if (bos != null) { |
| 234 | |
try { |
| 235 | 30 | bos.close(); |
| 236 | 0 | } catch (IOException ex) { |
| 237 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.FINEST, null, ex); |
| 238 | 30 | } |
| 239 | |
} |
| 240 | |
} |
| 241 | |
} |
| 242 | 30 | } |
| 243 | |
} |
| 244 | 0 | } catch (IOException ex) { |
| 245 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 246 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, msg, ex); |
| 247 | 0 | throw new ExtractionException(msg, ex); |
| 248 | |
} finally { |
| 249 | 0 | try { |
| 250 | 2 | zis.close(); |
| 251 | 0 | } catch (IOException ex) { |
| 252 | 0 | Logger.getLogger(FileUtils.class.getName()).log(Level.FINEST, null, ex); |
| 253 | 2 | } |
| 254 | 0 | } |
| 255 | 2 | } |
| 256 | |
} |