java关于压缩包的处理,我这里贴出三中格式的(zip,rar,tar)解压方法(目前只用到解压,所以压缩过多研究)。
1、首先我们先来说下为什么会有这三中格式:
(1)rar格式:rar格式是最先出现的压缩方式,它主要是用于商业机构一些文件的压缩,它可以根据不同公司的要求,去设定制定不同的压缩算法,这种算法是不对外公开的,安全性比较高,但他是收费的。
(2)zip格式:因为rar格式收费,必然会诞生一些免费的压缩格式,那么zip就是在这样的情况下诞生的。同样压缩算法也是公开的,不收取任何费用,效果当然也比rar格式稍差。
(3)tar格式:tar是国产推出的一种免费的压缩格式。
2、java编程实现rar,zip,tar格式的解压缩。
(1)rar解压缩:这里需要用到junrar-0.7.jar包,这里是下载地址:http://pan.baidu.com/s/1mg80F1I

/**
*
* 对rar文件进行解压
* <p>描述</p>
* @date 2014-7-16 下午1:59:28
* @version
* @param sourceRar
* @param destDir
* @throws Exception
*/
public static void unRar(String sourceRar, String destDir)
throws Exception {
Archive a = null;
FileOutputStream fos = null;
try {
a = new Archive(new File(sourceRar));
FileHeader fh = a.nextFileHeader(); while (fh != null) {
if (!fh.isDirectory()) {
// 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
String compressFileName = fh.getFileNameW().trim();
String destFileName = "";
String destDirName = "";
// 非windows系统
if (File.separator.equals("/")) {
destFileName = destDir +"//"+ compressFileName.replaceAll("\\\\", "/");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
// windows系统
} else {
destFileName = destDir+"//"+ compressFileName.replaceAll("/", "\\\\");
destDirName = destFileName.substring(0,destFileName.lastIndexOf("\\"));
}
// 2创建文件夹
File dir = new File(destDirName);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
// 3解压缩文件
fos = new FileOutputStream(new File(destFileName));
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
} catch (Exception e) {
log.error("the methord unRar is fiail ", e);
throw new Exception("Rar格式解压失败!");
} finally {
try {
if (fos != null) {
fos.close();
fos = null;
}
if (a != null) {
a.close();
a = null;
}
} catch (Exception e) {
log.error("the methord unRar close is fiail ", e);
} }
}

(2)zip解压缩:这个直接用apache自带的。

/**
* 解压缩ZIP文件,将ZIP文件里的内容解压到descFileName目录下
* @param zipFileName 需要解压的ZIP文件
* @param descFileName 目标文件
*/
public static boolean unZipFiles(String zipFileName, String descFileName) {
String descFileNames = descFileName;
if (!descFileNames.endsWith(File.separator)) {
descFileNames = descFileNames + File.separator;
}
try { // 根据ZIP文件创建ZipFile对象
ZipFile zipFile = new ZipFile(new File(zipFileName),"GB2312"); // 2014/6/27 解压乱码 杨云霞修改
ZipEntry entry = null;
String entryName = null;
String descFileDir = null;
byte[] buf = new byte[4096];
int readByte = 0;
// 获取ZIP文件里所有的entry
@SuppressWarnings("rawtypes")
Enumeration enums = zipFile.getEntries();
// 遍历所有entry
while (enums.hasMoreElements()) {
entry = (ZipEntry) enums.nextElement();
// 获得entry的名字
entryName = entry.getName();
descFileDir = descFileNames + entryName;
if (entry.isDirectory()) {
// 如果entry是一个目录,则创建目录
new File(descFileDir).mkdirs();
continue;
} else {
// 如果entry是一个文件,则创建父目录
new File(descFileDir).getParentFile().mkdirs();
}
File file = new File(descFileDir);
// 打开文件输出流
OutputStream os = new FileOutputStream(file);
// 从ZipFile对象中打开entry的输入流
InputStream is = zipFile.getInputStream(entry);
while ((readByte = is.read(buf)) != -1) {
os.write(buf, 0, readByte);
}
os.close();
is.close();
}
zipFile.close();
// log.debug("文件解压成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

(2)tar解压缩:这里需要用到java-gnu-tar-0.0.1.jar包,这里是下载地址:http://pan.baidu.com/s/1sjCuquT

/**
* 对tar文件进行解压缩
* 描述内容
* <p>描述</p>
* @date 2014-7-16 下午2:00:30
* @version
* @param path
*/
public static void unTar(String path,String destDir) throws Exception {
InputStream inputstream = null;
OutputStream outputstream = null;
TarInputStream zis = null;
try {
inputstream = new FileInputStream(new File(path));
zis = new TarInputStream(inputstream); //压缩包TarEntry,有多个文件(包含文件夹)就有多少个tarEntry
TarEntry tarEntry = null;
String destFileName = "";
String destDirName ="";
while ((tarEntry = zis.getNextEntry()) != null) {
destFileName = destDir+"\\" + tarEntry.getName().replaceAll("/", "\\\\");//获取文件地址
destDirName = destFileName.substring(0,destFileName.lastIndexOf("\\"));//获取文件目录
File dir = new File(destDirName);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
//判断是否为目录
if(!destFileName.substring(0, destFileName.length()-1).equals(destDirName)){
outputstream = new FileOutputStream(new File(destFileName));
//定一个缓存池 可以根据实际情况调整大小(事实证明很有用)
byte[] buffer = new byte[1024 * 100];
while (true) {
int readsize = zis.read(buffer);
outputstream.write(buffer);
if (readsize < 1024 * 50) {
break;
}
}
}
}
}catch (IOException e) {
log.error("the methord unTar is fiail ", e);
throw new Exception("Tar格式解压失败!");
} finally {
try {
if(outputstream!=null){
outputstream.flush();
outputstream.close();
}
if(inputstream!=null)
inputstream.close();
if(zis!=null)
zis.close();
} catch (Exception e) {
log.error("the methord unTar close is fiail ", e);
} } }

tar解压缩有中文乱码,一直没找到合适的处理办法,非常抱歉若有好办请提醒我。
转载自扑球小猫 (博客园)
java关于压缩包的处理,我这里贴出三中格式的(zip,rar,tar)解压方法(目前只用到解压,所以压缩过多研究)。的更多相关文章
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- java上传图片到数据库,涉及压缩文件zip/rar上传等
项目中有这个需求: 1)上传文件通过公司平台的校验,校验成功后,通过接口,返回文件流: 2)我们根据这个文件流进行操作.这里,先将文件流复制文件到项目临时目录WEB-INF/temp;文件使用完毕,删 ...
- atitit.提取zip rar文件列表 java php c# 的原理与设计
atitit.java提取zip rar文件列表 1. 取zip rar文件的场景问题 1 1.1. 多重压缩的问题 1 1.2. 文件名编码的问题 1 1.3. 目录的判定 1 2. rar的解析 ...
- Java中,异常的处理及抛出
首先我们需要知道什么是异常? 常通常指,你的代码可能在编译时没有错误,可是运行时会出现异常.比如常见的空指针异常.也可能是程序可能出现无法预料的异常,比如你要从一个文件读信息,可这个文件不存在,程序无 ...
- java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)
问题描述:怎样快速获取一个20MB图片的分辨率? 程序代码: package test; import java.awt.Dimension; import java.awt.image.Buffer ...
- [大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world
[大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world 原文链接:http://www.cnblogs.com/blog5277/ ...
- java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
总结一些日期常用的代码,方便以后直接拿 <code> /** * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) * @param year * @param m ...
- 在java的多态调用中,new的是哪一个类就是调用的哪个类的方法。
在java的多态调用中,new的是哪一个类就是调用的哪个类的方法.(x) 原因: ava多态有两种情况:重载和覆写 在覆写中,运用的是动态单分配,是根据new的类型确定对象,从而确定调用的方法: 在重 ...
- java 打包压缩包下载文件
1. 下载压缩包zip方法 @Override public void downloadZip(HttpServletResponse servletResponse) { String nowTim ...
随机推荐
- HAProxy介绍
简单说明 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需 ...
- spring HttpInvoker相关学习资料
官方文档 spring支持的几种RPC 用Http Invoker实现RCP客户端与后台的交互 Java HttpInvoker小试 Spring注解发布RMI/HTTPInvoker/Hessian ...
- 《Note --- Unreal 4 --- behavior tree》
Web: https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/index.html Test project: D:\En ...
- WORD中字数和字符
在WORD中,一个汉字算1个字符,也算是1个字,一个标点符号也算1个字符,也算是1个字,WORD中字符数的统计分为(不计空格)和(计空格)的两种. 如果一篇文章仅由汉字和标点符号组成,那么字数=字符数 ...
- 前端之float的几种清除浮动方式
前端之float的几种清除浮动方式 本节内容 1.float清除方式1 2.float清除方式2 3.float清除方式3 4.float清除方式4 1.float清除方式1 <!DOCTYPE ...
- 使用Guid做主键和int做主键性能比较
使用Guid做主键和int做主键性能比较 在数据库的设计中我们常常用Guid或int来做主键,根据所学的知识一直感觉int做主键效率要高,但没有做仔细的测试无法 说明道理.碰巧今天在数据库的优化过程中 ...
- 商城项目:装nginx时碰到的各种问题
因为项目需要,我们要在linux上nginx.碰到了各种问题.在这里一一记录下来. 首先我要开启我的两个虚拟机,开起来之后.用主机的SeureCRT去连接.都是好的. 但是我在虚拟机机上去ping I ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Spring Aspectj切入点语法定义
在使用spring框架配置AOP的时候,pointcut"切入点" 例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..)) ...
- Eclipse中一个Maven工程的目录结构
在之前的javaSE开发中,没有很关注Eclipse工程目录下的环境,总是看见一个src就点进去新建一个包再写一个class.以后的日子中也没有机会注意到一个工程到底是怎么组织的这种问题,跟不要说自己 ...