java 压缩包
package com.gome.budget.common.utils; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.io.*;
import java.util.ArrayList;
import java.util.List; public class CompressUtil { /**
* zip压缩文件
* @param dir
* @param zippath
*/
public static void zip(String dir ,String zippath){
List<String> paths = getFiles(dir);
compressFilesZip(paths.toArray(new String[paths.size()]),zippath,dir );
} /**
* 递归取到当前目录所有文件
* @param dir
* @return
*/
public static List<String> getFiles(String dir){
List<String> lstFiles = null;
if(lstFiles == null){
lstFiles = new ArrayList<String>();
}
File file = new File(dir);
File [] files = file.listFiles();
for(File f : files){
if(f.isDirectory()){
lstFiles.add(f.getAbsolutePath());
lstFiles.addAll(getFiles(f.getAbsolutePath()));
}else{
String str =f.getAbsolutePath();
lstFiles.add(str);
}
}
return lstFiles;
} /**
* 文件名处理
* @param dir
* @param path
* @return
*/
public static String getFilePathName(String dir,String path){
String p = path.replace(dir + File.separator, "");
p = p.replace("\\", "/");
return p;
}
/**
* 把文件压缩成zip格式
* @param files 需要压缩的文件
* @param zipFilePath 压缩后的zip文件路径 ,如"D:/test/aa.zip";
*/
public static void compressFilesZip(String[] files,String zipFilePath,String dir) {
if(files == null || files.length <= 0) {
return ;
}
ZipArchiveOutputStream zaos = null;
try {
File zipFile = new File(zipFilePath);
zaos = new ZipArchiveOutputStream(zipFile);
zaos.setUseZip64(Zip64Mode.AsNeeded);
//将每个文件用ZipArchiveEntry封装
//再用ZipArchiveOutputStream写到压缩文件中
for(String strfile : files) {
File file = new File(strfile);
if(file != null) {
String name = getFilePathName(dir,strfile);
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,name);
zaos.putArchiveEntry(zipArchiveEntry);
if(file.isDirectory()){
continue;
}
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024 ];
int len = -1;
while((len = is.read(buffer)) != -1) {
//把缓冲区的字节写入到ZipArchiveEntry
zaos.write(buffer, 0, len);
}
zaos.closeArchiveEntry();
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
if(is != null)
is.close();
} }
}
zaos.finish();
}catch(Exception e){
throw new RuntimeException(e);
}finally {
try {
if(zaos != null) {
zaos.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} } /**
* 把zip文件解压到指定的文件夹
* @param zipFilePath zip文件路径, 如 "D:/test/aa.zip"
* @param saveFileDir 解压后的文件存放路径, 如"D:/test/" ()
*/
public static void unzip(String zipFilePath, String saveFileDir) {
if(!saveFileDir.endsWith("\\") && !saveFileDir.endsWith("/") ){
saveFileDir += File.separator;
}
File dir = new File(saveFileDir);
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(zipFilePath);
if (!file.exists()) {
return;
}
InputStream is = null;
// ZipArchiveInputStream zais = null;
ArchiveInputStream input = null;
try {
is = new FileInputStream(file);
String archiverName = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()); input = new ArchiveStreamFactory()
.createArchiveInputStream(archiverName, is); // zais = new ZipArchiveInputStream(is, "");
ArchiveEntry archiveEntry = null;
while ((archiveEntry = input.getNextEntry()) != null) {
// 获取文件名
String entryFileName = archiveEntry.getName();
// 构造解压出来的文件存放路径
String entryFilePath = saveFileDir + entryFileName;
OutputStream os = null;
try {
// 把解压出来的文件写到指定路径
File entryFile = new File(entryFilePath);
if(entryFileName.endsWith("/")){
entryFile.mkdirs();
}else{
os = new BufferedOutputStream(new FileOutputStream(
entryFile));
byte[] buffer = new byte[1024 ];
int len = -1;
while((len = input.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
}
} catch (IOException e) {
throw new IOException(e);
} finally {
if (os != null) {
os.flush();
os.close();
}
} }
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (input != null) {
input.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} public static void main(String[] args) {
// zip压缩
String dir = "D:\\share\\java";
String zippath = "D:\\share\\test.zip";
CompressUtil.zip(dir, zippath);
} }
java 压缩包的更多相关文章
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载 实现功能:zip文件上传,后台自动解压,Jstree树目录(遍历文件),editor.md预览 采用Spring+Sp ...
- Java压缩包解压到指定文件
在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...
- Java压缩包(zip)【学习笔记】
前言 Java实现Zip压缩解压可以使用JDK的原生类java.util.zip,但是JDK 7 之前存在中文文件名乱码问题. 使用 ant.jar 的org.apache.tools.zip包,可以 ...
- Java Hour 11
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为11 Hour,请各位不吝赐教. Hour 11 ...
- [第二届构建之法论坛] 预培训文档(Java版)
本博客是第二届构建之法论坛暨软件工程培训活动预培训文档中[适用于结对编程部分的Java版本],需要实验者有一部分Java基础. 目录 Part0.背景 Part1.配置环境 配置JDK Linux 平 ...
- 3、Linux下配置Java环境
转载:http://blog.sina.com.cn/s/blog_c5a35e780102wtxl.html 生物信息很多软件都是用java写的,所以需要在linux上配置java运行环境.平台上的 ...
- linux系统安装java
1.下载Java压缩包 *.gz 2.解压 3.修改Linux配置文件,配置Java环境变量 4.使用命令source /etc/profile让修改生效 转载 https://www.cnblogs ...
- install hadoop on xubuntu
0. install xubuntu we recommend to set username as "hadoop" after installation, set user & ...
- Linux(centeros)下安装jdk
首先需要说明的是有的Linux系统自带jdk,这个jdk是openjdk,可以通过java-version查看 所以安装的步骤是,首先删除系统自带的(如果有)openjdk 1. rpm -qa | ...
随机推荐
- USACO 06JAN 牛的舞会 洛谷2863
题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...
- NX二次开发-UFUN圆弧矩阵标记、起始角和结束角(弧度测量)、圆弧中心坐标和圆弧半径UF_CURVE_ask_arc_data(边可以用)
1 NX11+VS2013 2 3 #include <uf.h> 4 #include <uf_ui.h> 5 #include <uf_modl.h> 6 #i ...
- 3.4_springboot2.x整合spring Data Elasticsearch
Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...
- Spring Cloud Feign设计原理
什么是Feign? Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直 ...
- tushare使用教程:初始化调用PRO版数据示例
下面介绍两种常用的数据调取方式: 通过tushare python包 使用http协议直接获取 注:pro版数据接口采用语言无关的http协议实现,但也提供了多种语言的SDK数据获取. 前提条件 1. ...
- <读书笔记>如何入门爬虫?
大部分爬虫框架都是 发送请求 获得页面 解析页面 下载内容 存储内容 定个宏伟目标 淘宝1000页 知乎 豆瓣 ... python基础 list.dict:序列化爬取的内容 切片:分割爬取内容,获取 ...
- 【python】遇到的错误
呃.这学期在学python啦.之前虽然自学过,但都是跟着教程也没使用什么编译环境.没遇到奇奇怪怪的错误. 现在就当作一个记录贴吧. 用的编译工具是pycharm.电脑是MacBook Air 1.我在 ...
- Selenium(一)---Selenium的安装和使用
一.前言 最近在帮一个老师爬取网页内容,发现网页是动态加载的,为了拿到全部的网页数据,这里使用到了Selenium.Selenium 是一个用于Web应用程序测试的工具,它可以模拟真实浏览器,支持多种 ...
- zepto(mark)
Zepto的设计目的是提供 jQuery 的类似的API,但并不是100%覆盖 jQuery .Zepto设计的目的是有一个5-10k的通用库.下载并快速执行.有一个熟悉通用的API,所以你能把你主要 ...
- 浏览器自带记忆功能,使input颜色和字体丢失
方法一 : 会有视觉上颜色的变化input:-internal-autofill-selected { /*内置阴影填充 背景颜色*/ box-shadow: inset 0 0 0 1000px # ...