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是众多 ...
随机推荐
- Catch the moments of your life. Catch them while you're young and quick.
Catch the moments of your life. Catch them while you're young and quick.趁你还年轻利落,把握住生活中的美好瞬间吧!
- pm2部署node应用
背景: 很早就知道了pm2的强大功能,部署,多进程部署,负载均衡等等,但是一直没有取尝试使用,每次写完代码就没关心部署的事了.最近有空就想着把pm2的部署流程走一遍,顺便整理出来. 环境: 1.本地: ...
- Android 使用GreenDao 添加字段,删除表,新增表操作
GreenDao 给我个人感觉 比一般的ORM框架要好很多,虽然说上手和其他的比起来,较复杂,但是如果使用熟练以后,你会爱上这个框架的 用这些ORM 框架给我的感觉都是,当升级时,都需要进行数据库所有 ...
- C#利用WebClient 两种方式下载文件(一)
WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...
- 【extjs6学习笔记】1.16 初始: 关于主题
打开app.json,里面有主题设置 主题说明 theme-base 这个包是所有其他主题的基础主题,是唯一没有父主题的主题. 它包含Ext JS组件和布局正常工作绝对必需的最低限度的一组CSS规则. ...
- python3基础09(装饰器的使用)
#!/usr/bin/env python# -*- coding:utf-8 -*- import time #方式1class People: def __init__(self, name, a ...
- LeetCode Nim Game (简单nim)
题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数, ...
- 深入理解计算机系统_3e 第十一章家庭作业 CS:APP3e chapter 11 homework
注:tiny.c csapp.c csapp.h等示例代码均可在Code Examples获取 11.6 A. 书上写的示例代码已经完成了大部分工作:doit函数中的printf("%s&q ...
- python_40_通过脚本转换参数实现替换
import sys f=open('yesterday','r',encoding='utf-8') f_new=open('yesterday_update','w',encoding='utf- ...
- java Socket 客户端服务端对接正确写法(BIO)
之前在工作中写过一些Socket客户端与服务端的代码,但是当时没有时间仔细研究,只能不报错先过的态度,对其细节了解不深,写的代码有各种问题也浑然不知,只是业务量级以及对接方对接代码没有出现出格的情况所 ...