最近在一个前辈的指引下,开始研究apache.commons。都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹

package my.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; public class FileUtil { public static int BUFFER_SIZE = 2048;
public static List<String> unZip(File zipfile, String destDir)
throws Exception {
if (StringUtils.isBlank(destDir)) {
destDir = zipfile.getParent();
}
destDir = destDir.endsWith(File.separator) ? destDir : destDir
+ File.separator;
ZipArchiveInputStream is = null;
List<String> fileNames = new ArrayList<String>(); try {
is = new ZipArchiveInputStream(new BufferedInputStream(
new FileInputStream(zipfile), BUFFER_SIZE));
ZipArchiveEntry entry = null;
while ((entry = is.getNextZipEntry()) != null) {
fileNames.add(entry.getName());
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(
new File(destDir, entry.getName())),
BUFFER_SIZE);
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(os);
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
IOUtils.closeQuietly(is);
} return fileNames;
} public static List<String> unZip(String zipfile, String destDir)
throws Exception {
File zipFile = new File(zipfile);
return unZip(zipFile, destDir);
} public static void compressFiles2Zip(File[] files, String zipFilePath) {
if (files != null && files.length > 0) {
ZipArchiveOutputStream zaos = null;
File f = new File(zipFilePath);
if(f.isDirectory())
{
System.out.println("isDirectory");
return;
}
if(f.exists())
{
f.delete();
}
try {
File zipFile = new File(zipFilePath);
zaos = new ZipArchiveOutputStream(zipFile);
zaos.setUseZip64(Zip64Mode.AsNeeded);
int index = 0;
for (File file : files) {
if (file != null) {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(
file, new File(file.getParent()).getName()+File.separator+file.getName());
zaos.putArchiveEntry(zipArchiveEntry);
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(
file));
byte[] buffer = new byte[1024 * 5];
int len = -1;
while ((len = is.read(buffer)) != -1) {
// 把缓冲区的字节写入到ZipArchiveEntry
zaos.write(buffer, 0, len);
}
// Writes all necessary data for this entry.
zaos.closeArchiveEntry();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (is != null)
is.close();
} } } zaos.finish(); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (zaos != null) { zaos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } public static void main(String[] args) throws Exception { String f = "C:/20161202/notepad_cache/test/notepad_cache - 副本";
File file = new File(f); File[] files = new File[100];
int index = 0;
for(String ff : file.list())
{
files[index++] = new File(f+File.separator+ff); }
compressFiles2Zip(files, f+".zip");
/*
String f = "C:/20161202/notepad_cache/test/notepad_cache - 副本";
unZip(f+"1.zip", "C:/1202/notepad_cache/test/");*/
} }

apache.commons.compress 压缩,解压的更多相关文章

  1. linux驱动系列之文件压缩解压小节(转)

    转至网页:http://www.jb51.net/LINUXjishu/43356.html Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通 ...

  2. Linux的压缩解压命令快速上手——解压篇

    在Linux系统中,压缩文件通常是先将若干文件(包括目录)打包成一个tar文件,然后再调用压缩程序将tar文件压缩成相应的压缩包,这也就是为什么Linux系的压缩包的后缀通常都是像tar.gz,tar ...

  3. 使用SevenZipSharp压缩/解压7z格式

    7z格式采用的LZMA算法,号称具有现今最高压缩率.笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压.不得不说,SevenZipSharp的API设计得非常 ...

  4. linux压缩解压命令

    -c: 建立压缩档案  -x:解压  -t:查看内容  -r:向压缩归档文件末尾追加文件  -u:更新原压缩包中的文件  这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一 ...

  5. linux中tar及压缩解压命令用法

    把常用的tar解压命令总结下,当作备忘: tar 命令可以为Linux的文件和目录创建档案.利用 tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向 档案中加入新的文件.t ...

  6. linux下打压缩解压

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  7. WebAPI性能优化之压缩解压

    有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小 ...

  8. linux命令tar压缩解压

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  9. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

随机推荐

  1. 每天一个设计模式-2 外观模式(Facade)

    每天一个设计模式-2  外观模式(Facade) 1.生活中的示例 客户想要购买一台电脑,一般有两种方法: 1.自己DIY,客户需要知道组成电脑的所有电子器件,并且需要熟悉那些配件,对客户要求较高. ...

  2. 转 threejs中3D视野的缩放实现

    Threejs基础部分学习知道透视相机new THREE.PerspectiveCamera(fov, aspect , near,far)中. fov视野角(拍摄距离)越大,场景中的物体越小.fov ...

  3. CSS3鼠标悬停图片上浮显示描述代码

    效果:http://hovertree.com/texiao/css3/20/ 效果图: 代码如下: <!doctype html> <html lang="zh" ...

  4. ArcGIS Engine开发之地图基本操作(2)

    地图数据的加载 1.加载地图文档 ArcGIS Engine支持加载多种类型的数据,有矢量数据的Coverage.Shapefile.dwg/dxf文件,栅格数据的BMP.GRID.控件数据库等.很多 ...

  5. 服务器开启https协议

    开启Tomcat https服务 发布企业级应用的时候遇到一个问题,就是IOS7.1之后app的下载地址URL必须是https开头的协议,所以服务器必须支持https协议. 实验环境:Mac OSX ...

  6. Android Studio快速开发之道

    概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. P ...

  7. Struts2初始(一)

    1.过滤器:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter位置:在struts2的核心包struts2-co ...

  8. java对xml节点属性的增删改查

    学习本文之前请先看我的另一篇文章JAVA对XML节点的操作可以对XML操作有更好的了解. package vastsum; import java.io.File; import java.io.Fi ...

  9. J2EE或MyEclipse简单配置以及第一个web页面

    首先打开你下载安装好的MyEclipse,配置你开发需要的环境. 大致分为3步:①配置编码:Window-->preferences-->General-->Workspace--& ...

  10. JavaScript (If...Else和Switch和循环遍历) 语句以及常用消息框

    If...Else 语句 JavaScript中if...else语句和Java中的语法和使用方法是一样的. 只是在JavaScript中要使用小写字母.使用大写的 IF 会出错! 至于if...el ...