Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vorburger/1062678 to your computer and use it in GitHub Desktop.
Save vorburger/1062678 to your computer and use it in GitHub Desktop.
Copy resources from Classpath into a temporary directory
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
File directory = new File(System.getProperty("java.io.tmpdir"), "MifosBirtFilesExtractedFromClasspath");
directory.mkdirs();
FileUtils.cleanDirectory(directory);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + BIRT_RESOURCES_PATTERN + "**");
for (Resource resource : resources) {
if (resource.exists() & resource.isReadable() && resource.contentLength() > 0) {
URL url = resource.getURL();
String urlString = url.toExternalForm();
String targetName = urlString.substring(urlString.indexOf(directoryToScan));
File destination = new File(rootDirectory, targetName);
FileUtils.copyURLToFile(url, destination);
LOGGER.info("Copied " + url + " to " + destination.getAbsolutePath());
} else {
LOGGER.debug("Did not copy, seems to be directory: " + resource.getDescription());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment