java 解jar包工具,工具类-解压jar - SunnyWu的个人页面 - OSCHINA - 中文开源技术交流社区...

package com.wuxiongwei.java.jar2;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.nio.ByteBuffer;

import java.nio.channels.Channels;

import java.nio.channels.FileChannel;

import java.nio.channels.ReadableByteChannel;

import java.util.Enumeration;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

/**

* 非常好的工具类

* 解压jar.

* @author

* @version 1.0.0

*/

public class JarDecompression {

protected static Log log = LogFactory.getLog(JarDecompression.class);

@SuppressWarnings("resource")

public static void uncompress(File jarFile, File tarDir) throws IOException {

JarFile jfInst = new JarFile(jarFile);

Enumeration enumEntry = jfInst.entries();

while (enumEntry.hasMoreElements()) {

JarEntry jarEntry = (JarEntry) enumEntry.nextElement();

File tarFile = new File(tarDir, jarEntry.getName());

if(jarEntry.getName().contains("META-INF")){

File miFile = new File(tarDir, "META-INF");

if(!miFile.exists()){

miFile.mkdirs();

}

}

makeFile(jarEntry, tarFile);

if (jarEntry.isDirectory()) {

continue;

}

FileChannel fileChannel = new FileOutputStream(tarFile).getChannel();

InputStream ins = jfInst.getInputStream(jarEntry);

transferStream(ins, fileChannel);

}

}

/**

* 流交换操作

* @param ins 输入流

* @param channel 输出流

*/

private static void transferStream(InputStream ins, FileChannel channel) {

ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10);

ReadableByteChannel rbcInst = Channels.newChannel(ins);

try {

while (-1 != (rbcInst.read(byteBuffer))) {

byteBuffer.flip();

channel.write(byteBuffer);

byteBuffer.clear();

}

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

if (null != rbcInst) {

try {

rbcInst.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (null != channel) {

try {

channel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 打印jar文件内容信息

* @param file jar文件

*/

public static void printJarEntry(File file) {

JarFile jfInst = null;;

try {

jfInst = new JarFile(file);

} catch (IOException e) {

e.printStackTrace();

}

Enumeration enumEntry = jfInst.entries();

while (enumEntry.hasMoreElements()) {

log.info((enumEntry.nextElement()));

}

}

/**

* 创建文件

* @param jarEntry jar实体

* @param fileInst 文件实体

* @throws IOException 抛出异常

*/

public static void makeFile(JarEntry jarEntry, File fileInst) {

if (!fileInst.exists()) {

if (jarEntry.isDirectory()) {

fileInst.mkdirs();

} else {

try {

fileInst.createNewFile();

} catch (IOException e) {

log.error("创建文件失败>>>".concat(fileInst.getPath()));

}

}

}

}

public static void main(String[] args) {

File jarFile = new File("/Users/mac/Documents/other/bw2/bopsdk-openapi-1.0.2-Release.jar");

File targetDir = new File("/Users/mac/Documents/other/bw2/test/");

try {

JarDecompression.uncompress(jarFile, targetDir);

} catch (IOException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值