/** * 解压缩JAR包 * @param fileName 文件名 * @param outputPath 解压输出路径 * @throws IOException IO异常 */ private void decompress(String fileName, String outputPath) throws IOException{ if (!outputPath.endsWith(File.separator)) { outputPath += File.separator; } JarFile jf = new JarFile(fileName); for (Enumeration e = jf.entries(); e.hasMoreElements();) { JarEntry je = (JarEntry) e.nextElement(); String outFileName = outputPath + je.getName(); File f = new File(outFileName); System.out.println(f.getAbsolutePath()); //创建该路径的目录和所有父目录 makeSupDir(outFileName); //如果是目录,则直接进入下一个循环 if(f.isDirectory()) { continue; } InputStream in = null; OutputStream out = null; try { in = jf.getInputStream(je); out = new BufferedOutputStream(new FileOutputStream(f)); byte[] buffer = new byte[2048]; int nBytes = 0; while ((nBytes = in.read(buffer)) > 0) { out.write(buffer, 0, nBytes); } }catch (IOException ioe) { throw ioe; } finally { try { if (null != out) { out.flush(); out.close(); } } catch (IOException ioe) { throw ioe; } finally { if (null != in) { in.close(); } } } } } /** * 循环创建父目录 * @param outFileName */ private void makeSupDir(String outFileName) { //匹配分隔符 Pattern p = Pattern.compile("[/\\" + File.separator + "]"); Matcher m = p.matcher(outFileName); //每找到一个匹配的分隔符,则创建一个该分隔符以前的目录 while (m.find()) { int index = m.start(); String subDir = outFileName.substring(0, index); File subDirFile = new File(subDir); if (!subDirFile.exists()) subDirFile.mkdir(); } }
java解压jar文件
猜你喜欢
转载自zhao103804.iteye.com/blog/1821916
今日推荐
周排行