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. maven validator数据校验

    1.maven文件中添加依赖包 <!-- validator校验--> <dependency> <groupId>org.hibernate</groupI ...

  2. echarts使用中的那些事儿(一)

    近来由于有几个小项目要用到echarts里的一些图,不得不使用,可是要完全按照自己的意愿来,要对它有些了解,要翻阅资料,遂有以下小结: 1.最开始第一步是把数据调出来就行,能在图上显示就成,以下是最开 ...

  3. FusionCharts使用JavaScript渲染图表(不用Flash)

    FusionCharts可以让用户只使用JavaScript建立图表(而不是使用Flash),只需要添加另一行代码,如下所示: FusionCharts.setCurrentRenderer('jav ...

  4. [转]latex符号

    常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LATEX2e 介绍”) 1.指数和下标可以用^和_后加相应字符来实现.比如: 2.平方根(square root)的输入命令 ...

  5. JavaScript:理解worker事件api

    如果你不是很了解Event事件,建议先看我上一篇随文javascript:理解DOM事件.或者直接看下文worker api. hack 首先,我们需要实例一个Worker的对象,浏览器会根据新创建的 ...

  6. 使用AirDroid控制百度影棒

    十一假期的时候看到有促销活动买了一个百度影棒2S+.话说这东西当做普通家庭客厅的电视盒子还差点火候,不论是操作还是内容用起来都有点折腾.(当然,有些是形势所迫,大家都懂.) 不过,如果就把它看作是普通 ...

  7. hdu-1162 Eddy's picture---浮点数的MST

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 #include<bi ...

  8. SpringMVC+Hibernate框架快速搭建

    1.  新建Maven项目springmvc 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  9. 梁勇(Danniel Liang) java教材例题:java程序购买额按税率求营业税 java中数值保留2位小数的方法

    package com.swift; import java.util.Scanner; public class PurchaseTaxDecimalsTwo { public static voi ...

  10. STL MAP使用注意事项

    Hat’s Words A hat’s word is a word in the dictionary that is the concatenation of exactly two other ...