91名师指路-头部
91名师指路

java 解压zip压缩文件

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

代码如下:

package com.fyq.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;

public class ZipUtils {

private static final int BUFFER_SIZE = 2 * 1024;

/**
* 功能:多个文件进行合并压缩并下载
* 作者:zxb
*/
public static void zipFiles(HttpServletResponse response, List<File> fileList, File zipPath) {
// 1 文件压缩
if (!zipPath.exists()) { // 判断压缩后的文件存在不,不存在则创建
try {
zipPath.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fileOutputStream=null;
ZipOutputStream zipOutputStream=null;
FileInputStream fileInputStream=null;
try {
fileOutputStream=new FileOutputStream(zipPath); // 实例化 FileOutputStream对象
zipOutputStream=new ZipOutputStream(fileOutputStream); // 实例化 ZipOutputStream对象
ZipEntry zipEntry=null; // 创建 ZipEntry对象
for (int i=0; i<fileList.size(); i++) { // 遍历源文件数组
fileInputStream = new FileInputStream(fileList.get(i)); // 将源文件数组中的当前文件读入FileInputStream流中
zipEntry = new ZipEntry(fileList.get(i).getName()); // 实例化ZipEntry对象,源文件数组中的当前文件
zipOutputStream.putNextEntry(zipEntry);
int len; // 该变量记录每次真正读的字节个数
byte[] buffer=new byte[1024]; // 定义每次读取的字节数组
while ((len=fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();

// 2 文件下载
long currentTime=System.currentTimeMillis(); // 当时时间戳
int randomFour=(int)((Math.random()*9+1)*1000); // 4位随机数
String fileName=String.valueOf(currentTime)+String.valueOf(randomFour)+".zip"; // 新的文件名称
String path=zipPath.toString();

// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

// 循环取出流中的数据
FileInputStream inStream=new FileInputStream(path); // 读到流中
byte[] b = new byte[100];
int len;
try {
OutputStream os=response.getOutputStream();
response.setContentType("application/octet-stream");
while ((len = inStream.read(b)) > 0){
os.write(b, 0, len);
}
inStream.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try { // 3 删除压缩包
String path=zipPath.toString();
File zfile = new File(path);
zfile.delete();
} catch (Exception e){
e.printStackTrace();
}
}
}

/**
* 功能:zip解压
* @param srcFile:zip源文件
* @param destDirPath:解压后的目标文件夹
* 备注:文件夹中不能带有中文,否则会导致解压失败
*/
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
// long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "路径下文件不存在");
}

// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile, Charset.forName("GBK")); // 解决附件解压文件夹内无法有中文名称的问题
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
// System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
// long end = System.currentTimeMillis();
// System.out.println("解压完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("解压失败", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
ZipUtils zip=new ZipUtils();
String path="E://11.zip";
File f=new File(path);
zip.unZip(f, "E://1");
}


}


2019-11-23 12:44:19     阅读(900)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部