List of usage examples for org.springframework.core.io ClassPathResource createRelative
@Override
public Resource createRelative(String relativePath)
From source file:org.shept.util.JarUtils.java
/** * Copy resources from a classPath, typically within a jar file * to a specified destination, typically a resource directory in * the projects webApp directory (images, sounds, e.t.c. ) * /*from w w w. ja v a2 s . c o m*/ * Copies resources only if the destination file does not exist and * the specified resource is available. * * @param resources Array of String resources (filenames) to be copied * @param cpr ClassPathResource specifying the source location on the classPath (maybe within a jar) * @param webAppDestPath Full path String to the fileSystem destination directory * @throws IOException when copying on copy error */ public static void copyResources(String[] resources, ClassPathResource cpr, String webAppDestPath) throws IOException { String dstPath = webAppDestPath; // + "/" + jarPathInternal(cpr.getURL()); File dir = new File(dstPath); dir.mkdirs(); for (int i = 0; i < resources.length; i++) { File dstFile = new File(dstPath, resources[i]); // (StringUtils.applyRelativePath(dstPath, images[i])); Resource fileRes = cpr.createRelative(resources[i]); if (!dstFile.exists() && fileRes.exists()) { FileOutputStream fos = new FileOutputStream(dstFile); FileCopyUtils.copy(fileRes.getInputStream(), fos); logger.info("Successfully copied file " + fileRes.getFilename() + " from " + cpr.getPath() + " to " + dstFile.getPath()); } } }
From source file:org.shept.util.JarUtils.java
/** * Copy resources from a classPath, typically within a jar file * to a specified destination, typically a resource directory in * the projects webApp directory (images, sounds, e.t.c. ) * /*w w w . j a v a 2 s . c o m*/ * Copies resources only if the destination file does not exist and * the specified resource is available. * * The ClassPathResource will be scanned for all resources in the path specified by the resource. * For example a path like: * new ClassPathResource("resource/images/pager/", SheptBaseController.class); * takes all the resources in the path 'resource/images/pager' (but not in sub-path) * from the specified clazz 'SheptBaseController' * * @param cpr ClassPathResource specifying the source location on the classPath (maybe within a jar) * @param webAppDestPath Full path String to the fileSystem destination directory * @throws IOException when copying on copy error * @throws URISyntaxException */ public static void copyResources(ClassPathResource cpr, String webAppDestPath) throws IOException, URISyntaxException { String dstPath = webAppDestPath; // + "/" + jarPathInternal(cpr.getURL()); File dir = new File(dstPath); dir.mkdirs(); URL url = cpr.getURL(); // jarUrl is the URL of the containing lib, e.g. shept.org in this case URL jarUrl = ResourceUtils.extractJarFileURL(url); String urlFile = url.getFile(); String resPath = ""; int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR); if (separatorIndex != -1) { // just copy the the location path inside the jar without leading separators !/ resPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length()); } else { return; // no resource within jar to copy } File f = new File(ResourceUtils.toURI(jarUrl)); JarFile jf = new JarFile(f); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String path = entry.getName(); if (path.startsWith(resPath) && entry.getSize() > 0) { String fileName = path.substring(path.lastIndexOf("/")); File dstFile = new File(dstPath, fileName); // (StringUtils.applyRelativePath(dstPath, fileName)); Resource fileRes = cpr.createRelative(fileName); if (!dstFile.exists() && fileRes.exists()) { FileOutputStream fos = new FileOutputStream(dstFile); FileCopyUtils.copy(fileRes.getInputStream(), fos); logger.info("Successfully copied file " + fileName + " from " + cpr.getPath() + " to " + dstFile.getPath()); } } } if (jf != null) { jf.close(); } }