mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-20 08:14:44 +01:00
checkstyle corrections
Former-commit-id: 982cd5df66cc4b41e73deddd7e1ae749a2cb955e
This commit is contained in:
@@ -42,6 +42,7 @@ import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
|
|||||||
import org.owasp.dependencycheck.analyzer.exception.ArchiveExtractionException;
|
import org.owasp.dependencycheck.analyzer.exception.ArchiveExtractionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Set of utilities to extract files from archives.
|
||||||
*
|
*
|
||||||
* @author Jeremy Long
|
* @author Jeremy Long
|
||||||
*/
|
*/
|
||||||
@@ -74,9 +75,8 @@ public final class ExtractionUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the contents of an archive into the specified directory. The files are only extracted if they are
|
* Extracts the contents of an archive into the specified directory. The files are only extracted if they are supported by the
|
||||||
* supported by the analyzers loaded into the specified engine. If the engine is specified as null then all files
|
* analyzers loaded into the specified engine. If the engine is specified as null then all files are extracted.
|
||||||
* are extracted.
|
|
||||||
*
|
*
|
||||||
* @param archive an archive file such as a WAR or EAR
|
* @param archive an archive file such as a WAR or EAR
|
||||||
* @param extractTo a directory to extract the contents to
|
* @param extractTo a directory to extract the contents to
|
||||||
@@ -140,153 +140,171 @@ public final class ExtractionUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the contents of an archive into the specified directory.
|
* Extracts the contents of an archive into the specified directory.
|
||||||
*
|
*
|
||||||
* @param archive
|
* @param archive an archive file such as a WAR or EAR
|
||||||
* an archive file such as a WAR or EAR
|
* @param destination a directory to extract the contents to
|
||||||
* @param destination
|
* @param filter determines which files get extracted
|
||||||
* a directory to extract the contents to
|
* @throws ExtractionException thrown if the archive is not found
|
||||||
* @param filter
|
*/
|
||||||
* determines which files get extracted
|
public static void extractFilesUsingFilter(File archive, File destination,
|
||||||
* @throws ExtractionException
|
FilenameFilter filter) throws ExtractionException {
|
||||||
* thrown if the archive is not found
|
if (archive == null || destination == null) {
|
||||||
*/
|
return;
|
||||||
public static void extractFilesUsingFilter(File archive, File destination,
|
}
|
||||||
FilenameFilter filter) throws ExtractionException {
|
|
||||||
if (archive == null || destination == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileInputStream fis = null;
|
FileInputStream fis = null;
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(archive);
|
fis = new FileInputStream(archive);
|
||||||
} catch (FileNotFoundException ex) {
|
} catch (FileNotFoundException ex) {
|
||||||
LOGGER.log(Level.FINE, null, ex);
|
LOGGER.log(Level.FINE, null, ex);
|
||||||
throw new ExtractionException("Archive file was not found.", ex);
|
throw new ExtractionException("Archive file was not found.", ex);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
extractArchive(new ZipArchiveInputStream(new BufferedInputStream(
|
extractArchive(new ZipArchiveInputStream(new BufferedInputStream(
|
||||||
fis)), destination, filter);
|
fis)), destination, filter);
|
||||||
} catch (ArchiveExtractionException ex) {
|
} catch (ArchiveExtractionException ex) {
|
||||||
final String msg = String.format(
|
final String msg = String.format(
|
||||||
"Exception extracting archive '%s'.", archive.getName());
|
"Exception extracting archive '%s'.", archive.getName());
|
||||||
LOGGER.log(Level.WARNING, msg);
|
LOGGER.log(Level.WARNING, msg);
|
||||||
LOGGER.log(Level.FINE, null, ex);
|
LOGGER.log(Level.FINE, null, ex);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
fis.close();
|
fis.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LOGGER.log(Level.FINE, null, ex);
|
LOGGER.log(Level.FINE, null, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts files from an archive.
|
* Extracts files from an archive.
|
||||||
*
|
*
|
||||||
* @param input
|
* @param input the archive to extract files from
|
||||||
* the archive to extract files from
|
* @param destination the location to write the files too
|
||||||
* @param destination
|
* @param filter determines which files get extracted
|
||||||
* the location to write the files too
|
* @throws ArchiveExtractionException thrown if there is an exception extracting files from the archive
|
||||||
* @param filter
|
*/
|
||||||
* determines which files get extracted
|
private static void extractArchive(ArchiveInputStream input,
|
||||||
* @throws ArchiveExtractionException
|
File destination, FilenameFilter filter)
|
||||||
* thrown if there is an exception extracting files from the
|
throws ArchiveExtractionException {
|
||||||
* archive
|
ArchiveEntry entry;
|
||||||
*/
|
try {
|
||||||
private static void extractArchive(ArchiveInputStream input,
|
while ((entry = input.getNextEntry()) != null) {
|
||||||
File destination, FilenameFilter filter)
|
if (entry.isDirectory()) {
|
||||||
throws ArchiveExtractionException {
|
final File dir = new File(destination, entry.getName());
|
||||||
ArchiveEntry entry;
|
if (!dir.exists()) {
|
||||||
try {
|
if (!dir.mkdirs()) {
|
||||||
while ((entry = input.getNextEntry()) != null) {
|
final String msg = String.format(
|
||||||
if (entry.isDirectory()) {
|
"Unable to create directory '%s'.",
|
||||||
final File dir = new File(destination, entry.getName());
|
dir.getAbsolutePath());
|
||||||
if (!dir.exists()) {
|
throw new AnalysisException(msg);
|
||||||
if (!dir.mkdirs()) {
|
}
|
||||||
final String msg = String.format(
|
}
|
||||||
"Unable to create directory '%s'.",
|
} else {
|
||||||
dir.getAbsolutePath());
|
extractFile(input, destination, filter, entry);
|
||||||
throw new AnalysisException(msg);
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException ex) {
|
||||||
} else {
|
throw new ArchiveExtractionException(ex);
|
||||||
extractFile(input, destination, filter, entry);
|
} catch (Throwable ex) {
|
||||||
}
|
throw new ArchiveExtractionException(ex);
|
||||||
}
|
} finally {
|
||||||
} catch (IOException ex) {
|
closeStream(input);
|
||||||
throw new ArchiveExtractionException(ex);
|
}
|
||||||
} catch (Throwable ex) {
|
}
|
||||||
throw new ArchiveExtractionException(ex);
|
|
||||||
} finally {
|
|
||||||
closeStream(input);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void extractFile(ArchiveInputStream input, File destination,
|
/**
|
||||||
FilenameFilter filter, ArchiveEntry entry) throws ExtractionException {
|
* Extracts a file from an archive (input stream) and correctly builds the directory structure.
|
||||||
final File file = new File(destination, entry.getName());
|
*
|
||||||
if (filter.accept(file.getParentFile(), file.getName())) {
|
* @param input the archive input stream
|
||||||
final String extracting = String.format("Extracting '%s'",
|
* @param destination where to write the file
|
||||||
file.getPath());
|
* @param filter the file filter to apply to the files being extracted
|
||||||
LOGGER.fine(extracting);
|
* @param entry the entry from the archive to extract
|
||||||
BufferedOutputStream bos = null;
|
* @throws ExtractionException thrown if there is an error reading from the archive stream
|
||||||
FileOutputStream fos = null;
|
*/
|
||||||
try {
|
private static void extractFile(ArchiveInputStream input, File destination,
|
||||||
createParentFile(file);
|
FilenameFilter filter, ArchiveEntry entry) throws ExtractionException {
|
||||||
fos = new FileOutputStream(file);
|
final File file = new File(destination, entry.getName());
|
||||||
bos = new BufferedOutputStream(fos, BUFFER_SIZE);
|
if (filter.accept(file.getParentFile(), file.getName())) {
|
||||||
transferUsingBuffer(input, bos);
|
final String extracting = String.format("Extracting '%s'",
|
||||||
} catch (FileNotFoundException ex) {
|
file.getPath());
|
||||||
LOGGER.log(Level.FINE, null, ex);
|
LOGGER.fine(extracting);
|
||||||
final String msg = String.format("Unable to find file '%s'.",
|
BufferedOutputStream bos = null;
|
||||||
file.getName());
|
FileOutputStream fos = null;
|
||||||
throw new ExtractionException(msg, ex);
|
try {
|
||||||
} catch (IOException ex) {
|
createParentFile(file);
|
||||||
LOGGER.log(Level.FINE, null, ex);
|
fos = new FileOutputStream(file);
|
||||||
final String msg = String
|
bos = new BufferedOutputStream(fos, BUFFER_SIZE);
|
||||||
.format("IO Exception while parsing file '%s'.",
|
transferUsingBuffer(input, bos);
|
||||||
file.getName());
|
} catch (FileNotFoundException ex) {
|
||||||
throw new ExtractionException(msg, ex);
|
LOGGER.log(Level.FINE, null, ex);
|
||||||
} finally {
|
final String msg = String.format("Unable to find file '%s'.",
|
||||||
closeStream(bos);
|
file.getName());
|
||||||
closeStream(fos);
|
throw new ExtractionException(msg, ex);
|
||||||
}
|
} catch (IOException ex) {
|
||||||
}
|
LOGGER.log(Level.FINE, null, ex);
|
||||||
}
|
final String msg = String
|
||||||
|
.format("IO Exception while parsing file '%s'.",
|
||||||
|
file.getName());
|
||||||
|
throw new ExtractionException(msg, ex);
|
||||||
|
} finally {
|
||||||
|
closeStream(bos);
|
||||||
|
closeStream(fos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void transferUsingBuffer(InputStream input,
|
/**
|
||||||
BufferedOutputStream bos) throws IOException {
|
* Transfers data from one stream to another using a buffer.
|
||||||
int count;
|
*
|
||||||
final byte[] data = new byte[BUFFER_SIZE];
|
* @param input the input stream
|
||||||
while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
|
* @param bos the output stream
|
||||||
bos.write(data, 0, count);
|
* @throws IOException thrown if there is an error reading/writing to the streams
|
||||||
}
|
*/
|
||||||
bos.flush();
|
private static void transferUsingBuffer(InputStream input,
|
||||||
}
|
BufferedOutputStream bos) throws IOException {
|
||||||
|
int count;
|
||||||
|
final byte[] data = new byte[BUFFER_SIZE];
|
||||||
|
while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
|
||||||
|
bos.write(data, 0, count);
|
||||||
|
}
|
||||||
|
bos.flush();
|
||||||
|
}
|
||||||
|
|
||||||
private static void closeStream(Closeable stream) {
|
/**
|
||||||
if (stream != null) {
|
* Closes the stream.
|
||||||
try {
|
*
|
||||||
stream.close();
|
* @param stream the stream to close
|
||||||
} catch (IOException ex) {
|
*/
|
||||||
LOGGER.log(Level.FINEST, null, ex);
|
private static void closeStream(Closeable stream) {
|
||||||
}
|
if (stream != null) {
|
||||||
}
|
try {
|
||||||
}
|
stream.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LOGGER.log(Level.FINEST, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void createParentFile(final File file)
|
/**
|
||||||
throws ExtractionException {
|
* Ensures the parent path is correctly created on disk so that the file can be extracted to the correct location.
|
||||||
final File parent = file.getParentFile();
|
*
|
||||||
if (!parent.isDirectory()) {
|
* @param file the file path
|
||||||
if (!parent.mkdirs()) {
|
* @throws ExtractionException thrown if the parent paths could not be created
|
||||||
final String msg = String.format(
|
*/
|
||||||
"Unable to build directory '%s'.",
|
private static void createParentFile(final File file)
|
||||||
parent.getAbsolutePath());
|
throws ExtractionException {
|
||||||
throw new ExtractionException(msg);
|
final File parent = file.getParentFile();
|
||||||
}
|
if (!parent.isDirectory()) {
|
||||||
}
|
if (!parent.mkdirs()) {
|
||||||
}
|
final String msg = String.format(
|
||||||
|
"Unable to build directory '%s'.",
|
||||||
|
parent.getAbsolutePath());
|
||||||
|
throw new ExtractionException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user