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. pscp no such file or directory

    背景:在WINDOWS10 上传一个文件 到 Centos 7中 工具:pscp 用法: pscp.exe -C e:\tinyfox\site\wwwroot\cdms\projecttemplat ...

  2. 微信小程序:从本地相册选择图片或使用相机拍照。

    wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...

  3. WebUploader实现采集图片的功能

    项目最开始用百度团队的文件上传组件做了个物料照片采集的功能,后来做员工头像采集时竟然不知道怎么使用了. 参照官方Demo: http://fex.baidu.com/webuploader/getti ...

  4. npm常用指令小记

    查看本地指定包在npm远程服务器的版本信息 方式一: npm view <packageName> versions 方式二: npm info <packageName> 查 ...

  5. CSS样式表优化

    前几天公司要模仿一家客户的网站模板来为另一客户新建一个模板,说白了就是换个数据源,然后样式表再小修小改一下就行了.但通过浏览器控制台下载素材时,发现这个网站开发的挺专业的,单就样式表而言,代码工整,注 ...

  6. ffmeg过滤器介绍[转]

    在ffmpeg中,进行反交错需要用到avfilter,即图像过滤器,ffmpeg中有很多过滤器,很强大,反交错的过滤器是yadif. 基本的过滤器使用流程是: 解码后的画面--->buffer过 ...

  7. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  8. MVC文件下载和webform也能使用的下载方法

    public ActionResult Index() { DownloadMethod("text/plain", "C:/Users/sunny/Pictures/S ...

  9. Aizu 2304 Reverse Roads(无向流)

    把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...

  10. 【BZOJ1064】[NOI2008] 假面舞会(图上DFS)

    点此看题面 大致题意:有\(k\)种面具(\(k\)是一个未知数且\(k≥3\),每种面具可能有多个),已知戴第\(i\)种面具的人能看到第\(i+1\)种面具上的编号,特殊的,戴第\(k\)种面具的 ...