import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZIPTest {
/**
* @param args
*/
public static void main(String[] args) {
List<String> fileList = new ArrayList<String>();
fileList.add("D:\\zh.txt");
fileList.add("D:\\a.java");
fileList.add("D:\\a.class");
fileList.add("D:\\aa.txt");
fileList.add("D:\\图片.png");
String fileDest = "D:\\中文.zip";
try {
// zip(fileList, fileDest);
} catch (Exception e) {
e.printStackTrace();
}
try {
//unzip(fileDest, "E:/aa/bb", false);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ZIPTest.class.getResource("/"));
}
public static void zip(List<String> fileList, String fileDest)
throws Exception {
/**
* 三种情况 1.zip文件存在就不创建 2.如果zip文件不存在就创建 3.创建过程中有错;就删除
*
*/
if (fileList == null || fileList.size() <= 0) {
throw new Exception("file list is empty!");
} else {
File destFile = null;
OutputStream output = null;
ZipOutputStream zipOut = null;
try {
destFile = new File(fileDest);
if (destFile.exists()) {
throw new Exception("zip file exists!");
} else {
output = new FileOutputStream(destFile);
// path
byte[] buf = new byte[1024];
zipOut = new ZipOutputStream(output);
for (String filePath : fileList) {
File file = new File(filePath);
if (file.exists()) {
// is file
if (file.isFile()) {
FileInputStream in = new FileInputStream(
filePath);
int index = filePath.lastIndexOf("/");
if (index <= 0) {
index = filePath.lastIndexOf("\\");
}
if (index <= 0) {
destFile.delete();
throw new Exception("file format Error!");
}
String filename = filePath.substring(index + 1);
ZipEntry entry = new ZipEntry(filename);
zipOut.putNextEntry(entry);
int len = 0;
while ((len = in.read(buf)) > 0) {
zipOut.write(buf, 0, len);
}
in.close();
} else {
if (zipOut != null) {
zipOut.closeEntry();
try {
zipOut.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
zipOut = null;
}
if (output != null) {
try {
output.flush();
output.close();
} catch (Exception e) {
destFile.delete();
}
output = null;
}
destFile.delete();
throw new Exception("is not file!");
}
} else {
if (zipOut != null) {
zipOut.closeEntry();
try {
zipOut.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
zipOut = null;
}
if (output != null) {
try {
output.flush();
output.close();
} catch (Exception e) {
destFile.delete();
}
output = null;
}
destFile.delete();
throw new Exception("file path not exists!");
}
}
}
} catch (Exception e1) {
if (zipOut != null) {
zipOut.closeEntry();
try {
zipOut.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
destFile.delete();
zipOut = null;
}
if (output != null) {
try {
output.flush();
output.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
output = null;
destFile.delete();
}
e1.printStackTrace();
}
if (zipOut != null) {
zipOut.closeEntry();
try {
zipOut.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
zipOut = null;
}
if (output != null) {
try {
output.flush();
output.close();
} catch (Exception e) {
destFile.delete();
e.printStackTrace();
}
output = null;
}
}
}
public static void unzip(String zipPath, String fileDest, boolean originDect)
throws Exception {
File srcFile = new File(zipPath);
if (srcFile.exists()) {
if (srcFile.isFile()) {
File destFile = new File(fileDest);
if (destFile.exists()) {
if (!destFile.isFile()) {
InputStream input = new FileInputStream(srcFile);
ZipInputStream zipInput = new ZipInputStream(input);
ZipEntry zipEntry = null;
while ((zipEntry = zipInput.getNextEntry()) != null) {
String name = zipEntry.getName();
String absFile = fileDest + "/" + name; // 不以原文件名为目标文件目录
if (originDect) {
int index = 0;
index = srcFile.getName().lastIndexOf(".");
if (index <= 0) {
throw new Exception("target path Error!");
}
absFile = fileDest + "/"
+ srcFile.getName().substring(0, index);
File file = new File(absFile);
if (!file.exists()) {
file.mkdirs();
absFile = fileDest
+ "/"
+ srcFile.getName().substring(0,
index) + "/" + name;
} else {
absFile = fileDest
+ "/"
+ srcFile.getName().substring(0,
index) + "/" + name;
}
}
OutputStream output = new FileOutputStream(absFile);
byte[] buf = new byte[1024];
int len = 0;
while ((len = zipInput.read(buf)) > 0) {
output.write(buf, 0, len);
}
output.flush();
output.close();
}
} else {
throw new Exception("target path must is directory!");
}
} else {
throw new Exception("target file directory must exists!");
}
} else {
throw new Exception("file is not file!");
}
} else {
throw new Exception("file is not exists!");
}
}
}