Skip to content

Instantly share code, notes, and snippets.

@liujbo
Last active November 24, 2018 15:41
Show Gist options
  • Save liujbo/2dadccc91b2e7c948debc29bc96956e6 to your computer and use it in GitHub Desktop.
Save liujbo/2dadccc91b2e7c948debc29bc96956e6 to your computer and use it in GitHub Desktop.
解压jar包到指定目录
package com.xilai.cuteBoy.common.util;
import java.io.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/*************************************************************
* Description: Jar工具类
* Author: Liu Junbo
* CreateTime: 2017/5/2
************************************************************/
public class JarUtil {
/**
* 解压jar文件到指定目录
*
* @param fileName
* @param outputPath
*/
public static synchronized void decompress(String fileName, String outputPath) {
// 保证输出路径为目录
if (!outputPath.endsWith(File.separator)) {
outputPath += File.separator;
}
// 如果不存在输出目录,则创建
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 解压到输出目录
JarFile jf = null;
try {
jf = new JarFile(fileName);
for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
JarEntry je = e.nextElement();
String outFileName = outputPath + je.getName();
File f = new File(outFileName);
if (je.isDirectory()) {
if (!f.exists()) {
f.mkdirs();
}
} else {
File pf = f.getParentFile();
if (!pf.exists()) {
pf.mkdirs();
}
InputStream in = jf.getInputStream(je);
OutputStream out = new BufferedOutputStream(
new FileOutputStream(f));
byte[] buffer = new byte[2048];
int nBytes;
while ((nBytes = in.read(buffer)) > 0) {
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
in.close();
}
}
} catch (Exception e) {
System.out.println("解压" + fileName + "出错!" + e.getMessage());
} finally {
if (jf != null) {
try {
jf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
decompress("D:\\maven\\repository\\org\\apache\\activemq\\activemq-pool\\5.13.4\\activemq-pool-5.13.4.jar","D:\\maven");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment