Read the jar file from java. It's very usefull if u wanna get some file content and this file is packed inti the jar. See this example.
public static void main(String args[]) { String relativeFilePath = "style/someCSSFile.css"; String zipFilePath = "/someDirectory/someWarFile.war"; String contents = readZipFile(zipFilePath, relativeFilePath); System.out.println(contents); } public static String readZipFile(String zipFilePath, String relativeFilePath) { try { ZipFile zipFile = new ZipFile(zipFilePath); Enumeration<? extends ZipEntry> e = zipFile.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); // if the entry is not directory and matches relative file then // extract it if (!entry.isDirectory() && entry.getName().equals(relativeFilePath)) { BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(entry)); // Read the file // With Apache Commons I/O String fileContentsStr = IOUtils.toString(bis, "UTF-8"); // With Guava // String fileContentsStr = new // String(ByteStreams.toByteArray(bis),Charsets.UTF_8); // close the input stream. bis.close(); return fileContentsStr; } else { continue; } } } catch (IOException e) { logger.error("IOError :" + e); e.printStackTrace(); } return null; }
No comments:
Post a Comment