实现自己的解压缩包包工具类

自己用java简单实现了一个解压缩的工具类,可以解压*.zip、*.jar等压缩文件,代码如下:里面需要传入两个参数,一个是filePath(待解压文件路径)、另一个是dirPath(文件解压后的目录),目前代码里面取到的是项目根下的项目名称.jar文件,也就是对上一篇中的 [url]http://ryan200909.iteye.com/blog/1734743[/url](实现自己的项目打jar包工具类)打的jar包文件进行解压缩。

【Java Code】

--------------------------------------------------------------------

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
* 解包工具类
* @author laiyujun@vip.qq.com
* QQ: 381418273
*
*/
public class UnzipJarUtil {

private Logger log = Logger.getLogger(this.getClass().getName()) ;
// private String path = System.getProperty("java.class.path") ;
// private String currentPath = path + File.separator + this.getClass().getName().replaceAll("\\.", "\\\\") +".class" ;

/**
* @param args
*/
public static void main(String[] args) {
UnzipJarUtil util = new UnzipJarUtil() ;

String userPath = System.getProperty("user.dir") ; //得到当前项目的根
String filePath = userPath + ".jar" ; // 定义压缩文件名称
File file = new File(filePath) ; //得到压缩文件
util.log.info("unzip file【"+file.getAbsolutePath()+"】") ;

String dirPath = file.getAbsolutePath().replace(".", "_") ;
File unzipDir = new File(dirPath); //得到解压缩目录
util.log.info("unzip file path【"+unzipDir.getAbsolutePath()+"】") ;

long start = System.currentTimeMillis();
try {
util.unzip(file, unzipDir) ; //执行解压缩
} catch (Exception e) {
e.printStackTrace();
} finally {
long end = System.currentTimeMillis();
util.log.info("unzip use total time【"+(end-start)+"ms】") ;
}

}

private void unzip(File file, File unzipDir) throws Exception {
File outFile = null ;
OutputStream out = null ;
InputStream input = null ;
ZipEntry entry = null ;

ZipFile zipFile = new ZipFile(file) ;
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file)) ;

int i = 0 ;
while((entry = zipInput.getNextEntry()) != null){
log.info("unzip【" + unzipDir.getAbsolutePath()+ File.separator + entry.getName() + "】file "+ (++i)) ;

outFile = new File(unzipDir.getAbsolutePath()+ File.separator + entry.getName()) ;

if(!outFile.getParentFile().exists()){ // 如果输出文件夹不存在
outFile.getParentFile().mkdirs() ; // 创建文件夹
}

if (!outFile.exists()){ // 判断输出文件是否存在
outFile.createNewFile() ; // 创建文件
}

input = zipFile.getInputStream(entry) ;
out = new FileOutputStream(outFile) ;
int temp = 0 ;
while((temp = input.read()) != -1){
out.write(temp) ;
}
input.close() ;
out.close() ;
}
zipInput.close() ;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Abellor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值