ZipUtils工具类
package com.util;
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.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.jdom.output.Format;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class ZipUtils {
public static void zip(File inputFile, File outputFile) {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(outputFile));
zos.setComment("From Log");
zipFile(zos, inputFile, null);
System.err.println("压缩完成!");
} catch (IOException e) {
e.printStackTrace();
System.err.println("压缩失败!");
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void zipFile(ZipOutputStream zos, File inputFile, String path) {
if (inputFile.isDirectory()) {
String p = null;
File[] fileList = inputFile.listFiles();
for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if (path == null || path.isEmpty()) {
p = file.getName();
} else {
p = path + File.separator + file.getName();
}
System.out.println(p);
zipFile(zos, file, p);
}
} else {
zipSingleFile(zos, inputFile, path);
}
}
public static void zipSingleFile(ZipOutputStream zos, File inputFile, String path) {
try {
InputStream in = new FileInputStream(inputFile);
zos.putNextEntry(new ZipEntry(path));
write(in, zos);
} catch (IOException e) {
e.printStackTrace();
}
}
public static HashSet<String> unZip(File srcFile, String destDirPath) throws RuntimeException {
HashSet<String> fileList = new HashSet<String>();
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String fName1 = entry.getName();
fName1 = fName1.replace("\\", "/");
String fName = fName1.substring(fName1.lastIndexOf("/")+1);
if(fName != null && fName.trim().length() > 0){
System.out.println("文件:" + fName);
fileList.add(fName);
}
if (entry.isDirectory()) {
String dirPath = destDirPath + File.separator + fName1;
File dir = new File(dirPath);
dir.mkdirs();
} else {
File targetFile = new File(destDirPath + File.separator + fName1);
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];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
is.close();
}
}
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileList;
}
private static void write(InputStream input, OutputStream output) {
int len = -1;
byte[] buff = new byte[1024];
try {
while ((len = input.read(buff)) != -1) {
output.write(buff, 0, len);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}