java解压jar文件

/** 
* 解压缩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(); 
} 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值