java 压缩jar_Java基础之实现解压和压缩jar、zip、rar等源码分享
有的时候在修改服务器项目中jar包配置时比较费事,相信开发过一定时间的码农们都遇到过类似的问题吧,需要重新打包再上传替换jar包,这样相对比较费事还有可能导致多人修改jar包导致不同步从而系统异常。下面为大家讲述一下如何利用java实现解压并修改解压后的目录中的文件,以及如何重新压缩jar、zip、rar等。
简单封装压缩Compressor.java工具类代码,具体如下:package com.yoodb.blog;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 压缩工具
* @author 路人宅
*/
public class Compressor {
private static Log log = LogFactory.getLog(Compressor.class);
private static final int BUFFER = 8192;
private File fileName;
private String originalUrl;
public Compressor(String pathName) {
fileName = new File(pathName);
}
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i = 0; i
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
this.compressDirectory(file, out, basedir);
} else {
this.compressFile(file, out, basedir);
}
}
/**
* 压缩目录
* @param dir
* @param out
* @param basedir
*/
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/**
* 压缩文件
* @param file
* @param out
* @param basedir
*/
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
String filePath = (basedir + file.getName())
.replaceAll(getOriginalUrl() + "/", "");
log.info("压缩文件:" + filePath);
ZipEntry entry = new ZipEntry(filePath);
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Compressor zc = new Compressor("E:\\org.wso2.carbon.service.mgt.ui_4.7.1.jar");
zc.compress("E:\\org.wso2.carbon.service.mgt.ui_4.7.0");
}
public String getOriginalUrl() {
return originalUrl;
}
public void setOriginalUrl(String originalUrl) {
this.originalUrl = originalUrl;
}
}
分析:
新建对象并传入新压缩包路径参数,调用compress方法传入需要压缩的目录路径,创建File对象传入参数,判断是否存在,不存在抛出异常,之后进行一系列创建或判断操作后,压缩目录创建压缩包。
简单封装解压Decompression.java工具类代码,具体如下:package com.yoodb.blog;
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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 解压工具
* @author 路人宅
*/
public class Decompression {
protected static Log log = LogFactory.getLog(Decompression.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 = 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("E:\\org.wso2.carbon.service.mgt.ui_4.7.0.jar");
File targetDir = new File("E:\\org.wso2.carbon.service.mgt.ui_4.7.0");
try {
Decompression.uncompress(jarFile, targetDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
分析:
新建两个File对象分别是压缩包路径和需要解压到的指定目录路径,之后调用uncompress方法进行一些创建、修改以及判断操作,解压成功。
利用上述两个Decompression.java 和Compressor.java工具类,进行解压重新打包操作,有什么疑难问题欢迎大家留言咨询,每日持续更新技术文章,感兴趣欢迎收藏。
java 压缩jar_Java基础之实现解压和压缩jar、zip、rar等源码分享相关推荐
- c# rar解压大小_C#解压缩Zip,Rar等压缩文件(详细说明)
其实这个东西网上已经有很多了 给出了一大把 当然我也是在网上找到得 只不过 说明不够详细 经过测试 给出详细的备注: 解压的给的很详细 压缩的基本也一样 只不过参数信息不一样罢了: 利用winra ...
- java压缩文件太慢_java 解压6万个ZIP文件,如何提升速度?
目前我使用的是org.apache.tools.zipjar包解压5015个zip文件,解压后14344个文件,耗时:669493毫秒代码:****************************** ...
- C语言零基础项目:吃豆人小游戏!详细思路+源码分享
每天一个C语言小项目,提升你的编程能力! <吃豆游戏>是一款休闲小游戏,和贪吃蛇,球球大作战吃食物都是有差不多的游戏逻辑. 效果展示: 这个游戏本身很简单,一共3关,吃掉画面上全部小豆子就 ...
- 解压deb_Linux填坑记:很全面的解压和压缩命令集合
Linux下常见的压缩包格式有5种:zip tar.gz tar.bz2 tar.xz tar.Z 基中tar是打包格式,gz和bz2等后缀才是指代压缩方式:gzip 和 bzip2 打包和解包方法: ...
- 利用R语言解压与压缩 .tar.gz .zip .gz .bz2 等文件
最近尝试用 R 对一些文件进行批量的解压与压缩,这里记录一些常用的解压与压缩的方法. 由于解压与压缩是对称的两种方法,这里我们着重以对文件的解压为例,分不同的格式进行讲解. .zip 压缩:zip() ...
- java zip加密压缩_Java解压和压缩带密码的zip文件过程详解
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
- java zip 解压 密码_Java解压和压缩带密码的zip文件过程详解
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
- java 压缩解压密码zip_Java解压和压缩带密码的zip文件过程详解|chu
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
- java压缩文件详解_Java解压和压缩带密码的zip文件过程详解
前言 JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名. 为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzi ...
最新文章
- R语言使用ggplot2包geom_jitter()函数绘制分组(strip plot,一维散点图)带状图(添加均值、中位数)实战
- c语言习题与实验doc,[教材]C语言程序设计习题与上机实验(全部答案).doc
- mac安装brew和zsh
- mysql php 新手卡生成_6个强大的PHP/Mysql代码生成器介绍
- 【POJ - 2663】Tri Tiling (简单dp)
- 熟悉 ASP.NET MVC 类
- 习题1083字符转换
- mysql text 查询速度_数据库学习之让索引加快查询速度(四)
- python字符串函数_python字符串函数
- rk3399_android7.1 USB Type-A的配置
- BizTalk 2002:Registering Custom Components
- js声明数组的四种方式
- Android Hybrid 方案之 离线文件加载
- python名词_使用Python词性标记提取名词(循环)
- EasyCVR通过GB28181级联到紫光华智综合安防应用平台无法注册成功问题排查
- 三,标识符(identifier)讲解
- 海湾主机汉字注释表打字出_电脑打字打不出来_电脑上打字只显示字母打不出汉字是怎么回事?...
- 用户行为分析,就该这么做!
- 百度云同步盘网络异常【1】解决办法(续)
- Atcoder CADDi 2018 Solution