package edu.abc.jythondemo.util;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void main(String[] args) {
//toZip("D:\\pythonfile\\add","D:\\pythonfile\\dest.zip");
unZipFiles("D:\\pythonfile\\dest.zip","D:\\pythonfile\\");
}
public static void toZip(String srcDir,String destFile){
FileOutputStream out=null;
ZipOutputStream zos=null;
try{
File file = new File(destFile);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}else{
if (file.exists()) {
file.delete();
}
}
out=new FileOutputStream(file);
zos=new ZipOutputStream(out);
File sourceFile=new File(srcDir);
if(!sourceFile.exists()){
throw new RuntimeException(srcDir+"is not exits!");
}
compress(sourceFile,zos,sourceFile.getName());
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(zos!=null) {
zos.close();
}
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
递归压缩
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name){
byte[] buf=new byte[1024*1024];
System.out.println("进行压缩文件名:"+sourceFile.getName());
if(sourceFile.isFile()){
System.out.println("file>>>"+sourceFile.getName());
try {
//向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name));
//copy文件到zip输出流
int len;
FileInputStream in=new FileInputStream(sourceFile);
while ((len=in.read(buf))!=-1){
zos.write(buf,0, len);
}
zos.closeEntry();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}else{
File[] listFiles=sourceFile.listFiles();
if(listFiles==null||listFiles.length==0){
System.out.println("空文件夹:"+name);
System.out.println(sourceFile.getName());
try {
zos.putNextEntry(new ZipEntry(name+"/"));
} catch (IOException e) {
e.printStackTrace();
}
}else{
for (File file:listFiles){
compress(file,zos,name+"/"+file.getName());
}
}
}
}
/*
解压
*/
public static void unZipFiles(String srcFilePath,String destDir){
long start = System.currentTimeMillis();
// 判断源文件是否存在
File srcFile=new File(srcFilePath);
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 = destDir + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDir + "/" + 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[1024*1024];
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");
srcFile.deleteOnExit();
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
}finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}