java zip压缩与解压-支持空目录,保留文件修改时间。

依赖于commons-io,commons-compress

[Java]代码

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import org.apache.commons.io.IOUtils;

public class ZipFileUtil {

/**

* 把一个目录打包到一个指定的zip文件中

*

* @param dirpath

* 目录路径

* @param zipPath

* zip文件路径

*/

public static void compressFoldToZip(String dirpath, String zipPath) {

compressFoldToZip(dirpath,zipPath,"");

}

/**

* 把一个目录打包到一个指定的zip文件中

* @param dirpath

* @param zipPath

* @param entryPath 压缩内文件逻辑路径。如static/

*/

public static void compressFoldToZip(String dirpath, String zipPath,String entryPath){

if(!entryPath.endsWith(File.separator)&&StringUtil.checkNotEmpty(entryPath)){

entryPath+=File.separator;

}

ZipArchiveOutputStream out = null;

try {

out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(new File(zipPath))));

out.setEncoding("UTF-8");

compressFoldToZip(out, dirpath, entryPath);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

IOUtils.closeQuietly(out);

}

}

/**

* 把一个目录打包到一个指定的zip文件中

*

* @param out

* @param dirpath

* 目录路径

* @param entryPath

* zip中文件的逻辑路径

*/

private static void compressFoldToZip(ZipArchiveOutputStream out, String dirpath, String entryPath) {

InputStream ins = null;

File dir = new File(dirpath);

File[] files = dir.listFiles();

if (files == null || files.length < 1) {

return;

}

try {

for (int i = 0; i < files.length; i++) {

// 判断此文件是否是一个文件夹

if (files[i].isDirectory()) {

if(files[i].listFiles().length>0){

compressFoldToZip(out, files[i].getAbsolutePath(), entryPath + files[i].getName() + File.separator);

}else{

addFileToZip(files[i], out, entryPath);

}

} else {

addFileToZip(files[i], out, entryPath);

}

}

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

IOUtils.closeQuietly(ins);

}

}

private static void addFileToZip(File file, ZipArchiveOutputStream out, String entryPath) {

InputStream ins = null;

try {

String path=entryPath + file.getName();

if(file.isDirectory()){

path=formatDirPath(path); //为了在压缩文件中包含空文件夹

}

ZipArchiveEntry entry = new ZipArchiveEntry(path);

entry.setTime(file.lastModified());

// entry.setSize(files[i].length());

out.putArchiveEntry(entry);

if(!file.isDirectory()){

ins = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));

IOUtils.copy(ins, out);

}

out.closeArchiveEntry();

} catch (IOException e) {

e.printStackTrace();

} finally {

IOUtils.closeQuietly(ins);

}

}

/**

* 解压zip文件到指定目录

*

* @param zipPath

* @param destDir

*/

public static void unZipToFold(String zipPath, String destDir) {

ZipArchiveInputStream ins = null;

OutputStream os = null;

File zip = new File(zipPath);

if (!zip.exists()) {

return;

}

File dest = new File(destDir);

if (!dest.exists()) {

dest.mkdirs();

}

destDir=formatDirPath(destDir);

try {

ins = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipPath)), "UTF-8");

ZipArchiveEntry entry = null;

while ((entry = ins.getNextZipEntry()) != null) {

if (entry.isDirectory()) {

File directory = new File(destDir, entry.getName());

directory.mkdirs();

directory.setLastModified(entry.getTime());

} else {

String absPath=formatPath(destDir+entry.getName());

mkdirsForFile(absPath);

File tmpFile=new File(absPath);

os=new BufferedOutputStream(new FileOutputStream(tmpFile));

IOUtils.copy(ins, os);

IOUtils.closeQuietly(os);

tmpFile.setLastModified(entry.getTime());

}

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

IOUtils.closeQuietly(ins);

}

}

private static String formatPath(String path){

path=path.replace('\\', File.separatorChar);

path=path.replace('/', File.separatorChar);

return path;

}

private static String formatDirPath(String dir){

if(!dir.endsWith(File.separator)){

dir+=File.separator;

}

return dir;

}

private static void mkdirsForFile(String filePath){

String absPath=filePath;

String tmpPath=absPath.substring(0,absPath.lastIndexOf(File.separator));

File tmp=new File(tmpPath);

if(!tmp.exists()){

tmp.mkdirs();

}

}

public static void main(String[] args) {

compressFoldToZip("D:\\new2\\feiq", "D:\\zip\\my.zip");

//unZipToFold("D:\\zip\\my.zip", "D:\\zip\\unzip");

}

}

java 压缩 空目录_java zip压缩与解压-支持空目录,保留文件修改时间相关推荐

  1. 将多个文件压缩成gzip,将gzip解压成多个文件

          第一步:文件压缩和解压缩方法 //解压gzip文件public static boolean extractZip(File file, File parent) {ZipFile zf ...

  2. 微信小程序下载zip压缩包后解压,并且打开文件查看的内容

    在开发pc端后台管理系统的时候,经常会遇到这样的需求:下载zip到本地,然后用户双击压缩包,并借助电脑端的压缩软件打开压缩包,就可以查看里面的word.excel.pdf文件里面的内容.(这种需求,毫 ...

  3. python图片压缩pako_前端pako.js的 解压, json 转excel文件 下载

    背景: 后台 返回:gzip压缩后进行了base64编码的字符串. 解决办法 >引入pako.js ,xlsx >定义解压和压缩的方法 import XLSX from 'xlsx' co ...

  4. ubuntu18.04 Linux包文件解压和安装,文件夹压缩打包

    目录 一.bz2 二.zip和unzip 三.tar .tar.gz tar.xz .tgz 四.deb 五.7z [无法输入中文]Ubuntu18.04中使用中文输入法_Linux教程_云网牛站 - ...

  5. java解压_Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...

  6. [Java]Swing窗体演示ZIP压缩流的压缩与解压

    最近开始学习java,然后学完了Swing和各种流后,试着写了个ZIP的压缩解压软件出来,具体代码如下: 压缩准备部分: String path = ja.getText();//获取文本域内容Str ...

  7. zip包怎么解压oracle,使用jar与zip压缩解压文件的区别

    使用jar命令压缩和解压文件不会继承原来的权限,切记! 而使用zip/unzip压缩解压文件则会保留文件原来的权限等信息,因此使用压缩解压的时候尽量使用专业的工具 下面是测试内容和结果: 1.首先确认 ...

  8. 压缩多张图片,解压后发现图片文件损坏的问题解决

    最近项目里需要做一个功能,就是吧多张图片压缩一个压缩文件,我使用的是java的ZipOutputStream 来实现的.最后开发完了,测试发现解压后的图片文件只有第一张能打开,后面的几张图片都打不开. ...

  9. 史上最“贱”黑客!把你的文件压缩,再卖给你解压密码

    我辛辛苦苦黑掉了你的电脑,还辛辛苦苦地把你的文件都压缩了.我加班加点,这么拼命,你不给钱就想要回密码,有这样的道理吗?你的内心难道不感到羞愧吗?你的良知难道不煎熬吗?不服来辩! 这大概就是" ...

最新文章

  1. POM.xml 标签详解
  2. ACM入门练习与递推小结
  3. 字符串拼接数字 java_使用JAVA代码实现字符串的简单拼接
  4. 【数据结构与算法】之深入解析“排列硬币”的求解思路与算法示例
  5. HDU4741(异面直线间的距离--空间解析几何)
  6. jsp实现数据禁用和只读
  7. swoole服务器主动推消息,实现websocket-主动消息推送laravelswoole
  8. 2.3、getRunListeners().starting()
  9. java+web提交sumbit,jsp怎么让submit不提交
  10. java虚拟机缩写为_(01-03)Java虚拟机缩写为。
  11. Fragment 和 FragmentActivity的使用(二)
  12. Windows系统服务器IIS7.5 Asp.net支持10万请求的设置方法
  13. 测试环境搭建 openwebmail+花生壳(linux客户端)
  14. 小教程:设置苹果Mac电脑的开机密码
  15. Git - 设置签名(Autograph)
  16. SQLException:no opration allowed after statement closed问题排查
  17. 好好说话之hijack retaddr
  18. 解决IOS播放器KxMovie播放音频卡顿的问题
  19. 程序计数器的作用--简单易懂
  20. 阿里云国际版ECS,虚拟主机和VPS托管之间的区别

热门文章

  1. Hive环境搭建启动报错
  2. TensorFlow 资源大全中文版
  3. Spring 创建代理类流程跟踪
  4. Aplication的意义和生命周期,与Context的关系,以及关于Aplication和Context相关问题的记录和解决办法...
  5. iOS中转义后的html标签如何还原
  6. 记录用友ERP二次开发全过程(转载)
  7. redis安全设置及主从配置
  8. eyoucms如何调用指定栏目下的推荐文章
  9. 基于kotlin的coroutines的生命周期管理
  10. Redis数据结构04-SortedSet