import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Zip {
private ZipInputStream zipIn; // 解压Zip
private ZipOutputStream zipOut; // 压缩Zip
private ZipEntry zipEntry;
private int bufSize; // size of bytes
private byte[] buf;
private int readedBytes;
public Zip() {
this(512);
}
public Zip(int bufSize) {
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
// 压缩文件夹内的文件
public void doZip(String zipDirectory) throws IOException {// zipDirectoryPath:需要压缩的文件夹名
File zipDir = null;
zipDir = new File(zipDirectory);
String filePath = zipDir.getAbsolutePath();
String zipFileName = (filePath.indexOf(".") == -1 ? filePath : filePath
.substring(0, filePath.lastIndexOf("."))) + ".zip";// 压缩后生成的zip文件名
File zipFile = new File(zipFileName);
if (zipFile.exists()) {
zipFile.delete();
} else {
zipFile.createNewFile();
}
this.zipOut = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFile)));
if (zipDir.isDirectory())
handleDir(zipDir, this.zipOut);
else
handleFile(zipDir, this.zipOut);
this.zipOut.close();
}
private void handleFile(File file, ZipOutputStream zipOut)
throws IOException {
FileInputStream fileIn = new FileInputStream(file);
this.zipOut.putNextEntry(new ZipEntry(file.getName()));
while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
this.zipOut.write(this.buf, 0, this.readedBytes);
}
this.zipOut.closeEntry();
}
// 由doZip调用,递归完成目录文件读取
private void handleDir(File dir, ZipOutputStream zipOut) throws IOException {
File[] files;
files = dir.listFiles();
for (File fileName : files) {
// System.out.println(fileName);
if (fileName.isDirectory()) {
handleDir(fileName, this.zipOut);
} else {
handleFile(fileName, this.zipOut);
}
}
}
public void unZip(String unZipfileName) {
try {
unZip(unZipfileName,
unZipfileName.substring(0, unZipfileName.indexOf(".zip")));
} catch (IOException e) {
e.printStackTrace();
}
}
// 解压指定zip文件
public void unZip(String unZipfileName, String descFile) throws IOException {// unZipfileName需要解压的zip文件名
this.zipIn = new ZipInputStream(new FileInputStream(unZipfileName));
this.zipEntry = this.zipIn.getNextEntry();
while (this.zipEntry != null) {
File f = new File(descFile);
f.mkdir();
if (this.zipEntry.isDirectory()) {
String name = this.zipEntry.getName();
name = name.substring(0, name.length() - 1);
f = new File(descFile + File.separator + name);
f.mkdir();
} else {
f = new File(descFile + File.separator
+ this.zipEntry.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = this.zipIn.read()) != -1) {
out.write(b);
}
out.close();
}
// 读取下一个ZipEntry
this.zipEntry = this.zipIn.getNextEntry();
}
this.zipIn.close();
}
// 设置缓冲区大小
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
}
// 测试Zip类
// public static void main(String[] args) throws Exception {
// Zip zip = new Zip();
// zip.unZip("D:\\temp\\upLoadDir.zip");
//
// }
}