Apache Compress 是什么?

  Apache  提供的文件压缩工具。

运行环境

  jdk 1.7

  commons-compress 1.15

测试代码

  

 package com.m.basic;

 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.junit.Test; import java.io.*;
import java.util.zip.ZipOutputStream; public class CompressTest { @Test
public void compressTest() {
File targetFile = new File("D:/malin/malin.zip");
File sourceFile = new File("D:/malin/xxx"); compressFile(targetFile, sourceFile);
} public void compressFile(File targetFile, File sourceFile) {
ZipOutputStream zipOutput = null;
try {
zipOutput = new ZipOutputStream(new FileOutputStream(targetFile));
compress(zipOutput, sourceFile, sourceFile.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zipOutput != null) {
try {
zipOutput.closeEntry();
zipOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} private void compress(ZipOutputStream zipOutput, File sourceFile, String base) throws IOException {
if (sourceFile.isDirectory()) {
File[] files = sourceFile.listFiles();
if (files.length == 0) {
System.out.println(base + "/");
zipOutput.putNextEntry(new ZipArchiveEntry(base + "/"));
} else {
for (File file : files) {
compress(zipOutput, file, base + "/" + file.getName());
}
}
} else {
zipOutput.putNextEntry(new ZipArchiveEntry(base));
FileInputStream fis = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fis); int tag;
System.out.println(base);
while ((tag = bis.read()) != -1) {
zipOutput.write(tag);
}
fis.close();
bis.close();
}
}
}

参考

http://commons.apache.org/proper/commons-compress/examples.html

Apache Compress-使用的更多相关文章

  1. Apache 项目列表功能分类便于技术选型

    big-data (49):  Apache Accumulo  Apache Airavata  Apache Ambari  Apache Apex  Apache Avro  Apache Be ...

  2. ASF (0) - ASF Java 项目总览

    Apache .NET Ant Library This is a library of Ant tasks that help developing .NET software. It includ ...

  3. 一文教您如何通过 Java 压缩文件,打包一个 tar.gz Filebeat 采集器包

    欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人网站: https://www.exception.site/essay/create-tar-gz-by-j ...

  4. zip格式文件编码检测

    解压后文件名乱码 由于zip格式文件无编码存储的结构,因此解压时无法知道原先的编码. 当解压zip格式文件时使用的编码和原编码不一致时,就可能会出现解压后文件名乱码问题. 猜测编码 基于上述问题,需要 ...

  5. apache.commons.compress 压缩,解压

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

  6. How to append files to a .tar archive using Apache Commons Compress?(转)

    I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar ar ...

  7. 关于Apache/Tomcat/JBOSS/Neginx/lighttpd/Jetty等一些常见服务器的区别比较和理解

    先说Apache和Tomcat的区别: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. ...

  8. [转]三大WEB服务器对比分析(apache ,lighttpd,nginx)

    原博文地址:http://www.blogjava.net/daniel-tu/archive/2008/12/29/248883.html 一.软件介绍(apache  lighttpd  ngin ...

  9. 三大WEB服务器对比分析(apache ,lighttpd,nginx)

    一.软件介绍(apache  lighttpd  nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...

随机推荐

  1. agc015F - Kenus the Ancient Greek(结论题)

    题意 题目链接 $Q$组询问,每次给出$[x, y]$,定义$f(x, y)$为计算$(x, y)$的最大公约数需要的步数,设$i \leqslant x, j \leqslant y$,求$max( ...

  2. @Valid的坑

    @Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前

  3. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  4. hihocoder 1093 SPFA算法

    题目链接:http://hihocoder.com/problemset/problem/1093 , 最短路的SPFA算法. 由于点的限制(10w),只能用邻接表.今天也学了一种邻接表的写法,感觉挺 ...

  5. 如何处理CloudFoundry应用部署时遇到的254错误

    使用SAP云平台的CloudFoundry部署应用: 在cockpit遇到错误信息:instance: a0abe2b5-7623-4cf1-4c65-0c79, index: 0, exit_des ...

  6. a low memory warning should only destroy the layer’s bitmap

    https://stablekernel.com/view-controller-in-ios-6/ Some of you may have noticed that your view contr ...

  7. netbackup :nbu备份 Hyper-V 遇到快照错误(状态码 156)

    遇到快照错误(状态码 156) 下表介绍与 NetBackup 状态码 156 有关的 Hyper-V 问题. 表:状态码 156 的可能原因 状态码 156 的原因 说明及推荐操作 NetBacku ...

  8. handlebars用法

    为什么需要模板引擎 关于前端的模板引擎,我用一个公式来解释 模板引擎 模板 + 数据 ========> html页面 模板引擎就像是html的解析生成器,将对应的模板填充完数据之后生成静态的h ...

  9. 初学Python遇到的坑

    问题一 脚本内容 MacBookPro:Desktop mac$ cat wike.py #!/usr/bin/python from urllib.request import urlopen fr ...

  10. 启动tomcat的Cannot find ./catalina.sh 的问题

    从终端进入tomcat的bin目录,然后执行startup.sh Cannot find bin/catalina.sh The file is absent or does not have exe ...