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 | ...
随机推荐
- delphi 压缩
DELPHI 通过ZLib来压缩文件夹 unit Unit1; interface uses ZLib, Windows, Messages, SysUtils, Variants, Classes, ...
- 【Python】模拟登录上海西南某高校校园网 (jaccount)
好久没写东西了,最近学习了一下模拟登录,以校园网为例,作一记录. 去年的时候写过一篇模拟登录的文章,用的是登录后的cookies,这种操作比较傻瓜,也不智能,不够自动化,本质还是手动登录. 这次我尝试 ...
- Go将统治下一个10年?Go语言发展现状分析
“本文是国内Go语言大中华区首席布道师——许式伟,在QCon2015上海站上的分享.他预测Go语言10年内一定会超过C和java,并且统治这一个10年. Go语言语法及标准库变化 Go从1.0版本到现 ...
- Windows内核驱动开发入门学习资料
声明:本文所描述的所有资料和源码均搜集自互联网,版权归原始作者所有,所以在引用资料时我尽量注明原始作者和出处:本文所搜集资料也仅供同学们学习之用,由于用作其他用途引起的责任纠纷,本人不负任何责任.(本 ...
- 剑指offer——13矩阵中的路径
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- 2.1_springboot2.x消息介绍&RabbitMQ运行机制
1.概述 1.大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2.消息服务中两个重要概念: 消息代理(message broker)即消息服务器 和目的地(destination ...
- 【python】遇到的错误
呃.这学期在学python啦.之前虽然自学过,但都是跟着教程也没使用什么编译环境.没遇到奇奇怪怪的错误. 现在就当作一个记录贴吧. 用的编译工具是pycharm.电脑是MacBook Air 1.我在 ...
- 【hihocoder】Demo Day
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. Today is your co ...
- 如何使用flow进行静态类型检查
Flow 是 facebook 出品的 JavaScript 静态类型检查⼯具.Vue.js 的源码利⽤了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. 为什么⽤ Flow? ...
- 学习修复Laravel The only supported ciphers are AES-128-CBC and AES-256-CBC
The only supported ciphers are AES-128-CBC and AES-256-CBC 在项目中,删除了 .env的APP_KEY的值,再运行 php artisan k ...