此文介绍apache的压缩包,好处是可以自己设置ZipFile文件名的编码.

而jdk自带的ZipFIle只支持utf-8,导致压缩后中文出现乱码.

*下面给出个zip压缩解压的示例

---zip压缩

Zip Compressprivate void doZip() throws ArchiveException, IOException{
ZipArchiveOutputStream zos =(ZipArchiveOutputStream) new ArchiveStreamFactory()
.createArchiveOutputStream("zip", new FileOutputStream(path)); //or new ZipArchiveOutputStream(new FileOutputStream(path))
zos.setEncoding(encoding);
String rootPath =FileUtil.getRootPath(files);  //获取要压缩文件根路径
ZipArchiveEntry ze;
for(File f:files){
if(!f.exists())
continue;
ze =new ZipArchiveEntry(getEntryName(f,rootPath)); //获取每个文件相对路径,作为在ZIP中路径
zos.putArchiveEntry(ze);
//folder
if(ze.isDirectory()){
zos.closeArchiveEntry();
continue;
}
//file
FileInputStream fis =new FileInputStream(f);
IOUtils.copy(fis, zos, BUF);
fis.close();
zos.closeArchiveEntry();
}
zos.close();
}
private String getEntryName(File f,String rootPath){
String entryName;
String fPath =f.getAbsolutePath();
if(fPath.indexOf(rootPath)!=-1)
entryName =fPath.substring(rootPath.length()+1);
else
entryName =f.getName();
if(f.isDirectory())
entryName +="/";   //"/"后缀表示entry是文件夹
return entryName;
}

---zip解压

Zip Decompresspublic void doZip() throws IOException, ArchiveException{
ZipFile file =new ZipFile(sPath,encoding);
Enumeration<ZipArchiveEntry> en =file.getEntries();
ZipArchiveEntry ze;
while(en.hasMoreElements()){
ze =en.nextElement();
File f =new File(tPath,ze.getName());
//创建完整路径
if(ze.isDirectory()){
f.mkdirs();
continue;
}else
f.getParentFile().mkdirs();
InputStream is =file.getInputStream(ze);
OutputStream os =new FileOutputStream(f);
IOUtils.copy(is, os, BUF);
is.close();
os.close();
}
file.close();
}
//这是更一般的解压处理
public void doTar() throws IOException{
TarArchiveInputStream tis =new TarArchiveInputStream(new FileInputStream(sPath));
TarArchiveEntry te =null;
while((te=tis.getNextTarEntry())!=null){
File target =new File(tPath,te.getName());
if(te.isDirectory()){
target.mkdirs();
continue;
}else
target.getParentFile().mkdirs();
FileOutputStream fos =new FileOutputStream(target); //将当前entry写入文件
byte[] buf =new byte[BUF];
int len;
while((len=tis.read(buf))!=-1){
fos.write(buf, 0, len);
}
fos.close();
}
tis.close();
}

*tar包

---默认tar中entry的长度限制是TarConstants.NAMELEN = 100 [0x64]

原因有待研究?

这个长度应当=操作系统支持的最小长度.

---长文件名处理setLongFileMode(int longFileMode)

参数

/** Fail if a long file name is required in the archive. */
public static final int LONGFILE_ERROR = 0;

/** Long paths will be truncated in the archive. */
public static final int LONGFILE_TRUNCATE = 1;

/** GNU tar extensions are used to store long file names in the archive. */
public static final int LONGFILE_GNU = 2;

在putArchiveEntry()中根据上面的设置处理文件名.

使用长文件名的设置缺点是在其他非gnu操作系统上可能无法解压

"If you choose the GNU tar option, the archive can not be extracted using many other tar implementations like the ones of OpenBSD, Solaris or MacOS X"

---注意

"The tar package does not support the full POSIX tar standard nor more modern GNU extension of said standard. It cannot deal with entries larger than 2 GByte either. "

*Jar包

“Note that ArchiveStreamFactory doesn't distinguish ZIP archives from JAR archives, so if you use the one-argument createArchiveInputStream  method on a JAR archive, it will still return the more generic ZipArchiveInputStream.

The JarArchiveEntry class contains fields for certificates and attributes that are planned to be supported in the future but are not supported as of Compress 1.0.”

从源码可知JarArchive*Stream和JarArchiveEntry继承自对应的Zip,从说明可知,目前两者功能是一致的

package com.me;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;

public class UnZip {
 
 private static final String path = "D://WorkSpace//111.zip";
 
 private static final String tpath = "D://WorkSpace//";
 
 public static void main(String[] args) throws IOException
 {
  //File file = new File(path);
  ZipFile zipFile = new ZipFile(path);
  Enumeration<ZipArchiveEntry> en = zipFile.getEntries();
  ZipArchiveEntry ze;
  while(en.hasMoreElements())
  {
   ze = en.nextElement();
   File file = new File(tpath,ze.getName());
   //创建完整路径
   if(ze.isDirectory())
   {
    file.mkdirs();
    continue;
   }else{
    file.getParentFile().mkdirs();
   }
   InputStream is =zipFile.getInputStream(ze);
   OutputStream os =new FileOutputStream(file);
   IOUtils.copy(is, os);
   is.close();
   os.close();
  }
  zipFile.close();  
 }
}

commons compress使用+ziji相关推荐

  1. java 7zip解压_Apache Commons Compress介绍-JAVA压缩解压7z文件

    7zip(下面简称7z)是由Igor Pavlov所开发的一种压缩格式,主要使用的压缩算法是LZMA/LZMA2.7z是一种压缩比非常高的格式,这与其压缩算法LZMA有直接关系,所以很多大文件都是用7 ...

  2. java 分卷压缩_Apache Commons Compress介绍-Zip压缩解压

    Zip格式应该是最出名的压缩格式之一了,zlib.gzip这些辈分很老的库大家应该都用过,甚至大部分其他格式的压缩库,都可以处理zip格式.Commons Compress当然也少补了对zip格式的支 ...

  3. Apache Commons Compress

    Apache Commons是Apache软件基金会的项目,曾隶属于Jakarta项目.Commons的目的是提供可重用的.开源的Java代码.Apache Commons包含了很多开源的工具,用于解 ...

  4. java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile

    poi4.0.0读取excel文件时报java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile ...

  5. java Apache Commons jar包简介

    一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI 说明 ...

  6. 常用Apache Commons工具类备忘

    常用Apache Commons工具类 ----------------------------------------------------------------- 例如:commons.lan ...

  7. beanutils工具类_Apache Commons 工具类介绍及简单使用

    来源:http://h5ip.cn/9xu3 Apache Commons 工具类大家都有用过,但是可能缺乏系统学习,只用到了一小部分功能,无法发挥极限的价值,肥朝用大白话说就是,一颗好白菜都让猪给拱 ...

  8. Java压缩技术(七) TAR——Commons实现

    转载自   Java压缩技术(七) TAR--Commons实现 顺便复习一遍linux命令:  tar cf <file.tar> <file>将由文件<file> ...

  9. Apache Commons介绍(转载)

    一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI 说明 ...

最新文章

  1. 精心分享8个特别实用,但又鲜为人知的软件
  2. 第十、十一周项目一-点-圆-圆柱类族的设计(3)
  3. WWW 2021有哪些值得读的图机器学习相关论文?
  4. webgl获取鼠标形状_三模无线搭配对称手型设计,游戏致胜利器,ROG烈刃2无线鼠标...
  5. 整型数据类型java_Java 六种基本整型数据类型变量的取值范围
  6. vue 防止按钮重复点击
  7. 第 2 章 Java 基础
  8. LeetCode问题7
  9. 在Eclipse上用JAVA连接数据库
  10. [Matlab] 二进制蝙蝠算法用于解决背包问题
  11. 我的CSDN账号被偷了
  12. 读《淘宝技术这十年》有感
  13. 技术年货:美团技术沙龙合辑大放送
  14. 图解:卷帘快门(Rolling_shutter)与全局快门(global_shutter)的区别
  15. 斯坦福AI百年报告2017:人工智能与机器学习全景式概览
  16. 斗地主洗牌+发牌+排序
  17. 优酷弱网平台落地实践
  18. STM32 自定义HID USB设备的实现
  19. IMSI 和 IMEI
  20. 麒麟V10系统U盘引导盘制作手册

热门文章

  1. 敏捷基本概念——三大角色五大会议
  2. SSL证书过期怎么办?别慌!SSL应急解决方案及注意事项来了
  3. 深克隆和浅克隆的区别
  4. Freedom and discipline
  5. 尽量用pass-by-reference-to-const(const引用)替换pass-by-value(传值)
  6. 带你玩转kubernetes-k8s(第14篇:k8s-深入掌握Pod-在容器内获取Pod信息)
  7. CQHMI品牌NR系列HMI人机界面在纺织机械设备行业中的应用
  8. 黑马程序员--第一阶段5.JavaAPI--第13天
  9. python精彩编程200例 pdf-Python创意编程200例turtle篇
  10. 爬取QQ音乐周杰伦歌曲的歌词