最近在一个前辈的指引下,开始研究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. 9.1.3 .net framework通过业务逻辑层自动生成WebApi的做法

    首先需要说明的是这是.net framework的一个组件,而不是针对.net core的.目前工作比较忙,因此.net core的转换正在编写过程中,有了实现会第一时间贴出来. 接下来进入正题.对于 ...

  2. 怎样才能自学好Java?

    经常有初学Java的同学问:怎么样才能学好Java?自学Java难吗? 我认为自学Java并不难.相对于其他语言来说,因为Java95年才出来,所以相对比较新,旧的语言中一些不合适的东西在Java里面 ...

  3. 使用AxisHelper帮助理解View and Data API中的坐标系统

    大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的.Three.js里面提供了一个AxisHelpe ...

  4. BCS datetime 时间区间问题

    BCS 整合sql表时发现以下问题: datetime字段在列表中带了时区,比如插入12-6号的数据,在sql中显示的是12-5 date类型字段无法正确识别,插入成功但报错 LobSystem (外 ...

  5. Looper.prepare()和Looper.loop()

    什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建 ...

  6. java、easyui-combotree树形下拉选择框

    最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...

  7. JS二分查找

    二分法查找,也称折半查找,是一种在有序数组中查找特定元素的搜索算法.查找过程可以分为以下步骤:(1)首先,从有序数组的中间的元素开始搜索,如果该元素正好是目标元素(即要查找的元素),则搜索过程结束,否 ...

  8. jedisLock—redis分布式锁实现

    一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...

  9. 【转】(超详细)jsp与servlet之间页面跳转及参数传递实例

    初步学习JavaEE,对其中jsp与Servlet之间的传值没弄清楚,查看网上资料,发现一篇超详细的文章,收获大大,特此记录下来.具体链接:http://blog.csdn.net/ssy_shand ...

  10. JMeter常见问题集合

    前言 本文内容仅仅是针对Jmeter的部分功能名词的介绍和解释,以及初学者不易理解的问题的整理.部分内容来自别人做的整理,为了更好地整理自己的思路,所以可耻的整理一下发到博客上. 标题[1-6]和[参 ...