Apache Compress-使用
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-使用的更多相关文章
- Apache 项目列表功能分类便于技术选型
big-data (49): Apache Accumulo Apache Airavata Apache Ambari Apache Apex Apache Avro Apache Be ...
- ASF (0) - ASF Java 项目总览
Apache .NET Ant Library This is a library of Ant tasks that help developing .NET software. It includ ...
- 一文教您如何通过 Java 压缩文件,打包一个 tar.gz Filebeat 采集器包
欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人网站: https://www.exception.site/essay/create-tar-gz-by-j ...
- zip格式文件编码检测
解压后文件名乱码 由于zip格式文件无编码存储的结构,因此解压时无法知道原先的编码. 当解压zip格式文件时使用的编码和原编码不一致时,就可能会出现解压后文件名乱码问题. 猜测编码 基于上述问题,需要 ...
- apache.commons.compress 压缩,解压
最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...
- 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 ...
- 关于Apache/Tomcat/JBOSS/Neginx/lighttpd/Jetty等一些常见服务器的区别比较和理解
先说Apache和Tomcat的区别: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. ...
- [转]三大WEB服务器对比分析(apache ,lighttpd,nginx)
原博文地址:http://www.blogjava.net/daniel-tu/archive/2008/12/29/248883.html 一.软件介绍(apache lighttpd ngin ...
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)
一.软件介绍(apache lighttpd nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...
随机推荐
- 异步加载js的3种方式
默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很大的javascript,如果放在页 ...
- Android 浮动按钮的伸缩效果
在做项目时想增加点动感,于是就有如下效果: 实现起来也很简单,通过属性动画和recyclerview 滑动结合就很好实现了. 通过给recycleview添加一个滑动监听:通过滚动的差值来处理动画 m ...
- SpringBoot的快速构建
1.http://start.spring.io2.Spring Tool Suite3.IntelliJ IDEA4.Spring Boot CLI5.Maven手工构建
- VS打包方法(安装和部署简介)——内含大量图片,密症慎入!
友情提示:内含大量文字.图片,密集恐惧症者慎入! 主要记述一下利用微软集成开发环境VS打包的方法和详细步骤. 1.新建打包工程 打开VS,文件->添加项目->新建项目(如图1),在添加新项 ...
- python_76_json与pickle反序列化2
import pickle def say(name):#序列化时用完会释放,要想反序列化,要重新写上该函数,否则会出错 print('我的高中:', name)#可以和之前的序列化函数不同 f=op ...
- python_65_生成器1
# map()函数 # map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. # 例 ...
- Deep Learning 优化方法总结
Stochastic Gradient Descent (SGD) SGD的参数 在使用随机梯度下降(SGD)的学习方法时,一般来说有以下几个可供调节的参数: Learning Rate 学习率 We ...
- detection in video and image
video中的detection,背景更加复杂,目标更加不聚焦,同时由于图片分辨率低于图像,因此更加难做. image中的Detection,背景相对简单些,目标更加聚焦,同时图片分辨率高,因此更加容 ...
- js日期类型date
javascript语言核心包括Date()构造函数,用来创建表示日期和时间的函数 //返回当前的日期和时间 var today = new Date(); //2011年1月1日 ...
- jfinal excel表导出
在自己的WEB项目中要用到导出Excel,所以结合网络上的资源写了一个自己的export 工具类. 说明: JFinal 环境 WEB项目 JAVA后台生成非JS插件 好了,直接撸代码 1.设置文件保 ...