package client;
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.ZipOutputStream;
/**
* 壓縮實作類別 <br>
* 主要實現: <br>
* <p>
* 壓縮單個檔案、
* <p>
* 壓縮檔夾下的所有檔案及子檔案夾
*
* <p>
*
* @author libo
*
*/
public class ZipUtil {
/**
* 壓縮單個檔案
*
* @param filePath
* 檔案路徑
* @param fileName
* 檔案名稱字
* @param objDir
* 壓縮檔目標檔案夾
* @param ojbZipName
* 壓縮檔名字
* @return
* @throws IOException
*/
public boolean zip(String filePath, String fileName, String objDir,
String ojbZipName) throws IOException {
boolean tag = false;
ZipOutputStream zos = null;
FileInputStream fis = null;
try {
zos = new ZipOutputStream(new FileOutputStream(objDir + ojbZipName));
fis = new FileInputStream(filePath + fileName);
byte[] b = new byte[4096];
int i = 0;
zos.putNextEntry(new ZipEntry(fileName));
while ((i = (fis.read(b))) > 0) {
zos.write(b, 0, i);
}
} finally {
try {
fis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException();
}
try {
zos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException();
}
}
return tag;
}
/**
* 壓縮一個檔案夾下的所有檔案 注意中間路徑串連用"/" 如:c:/tt/ttt
*
* @param srcPath
* 要壓縮的檔案夾路徑
* @param objZipPath
* 目標檔案夾路徑
* @param ojbZipName
* 目標壓縮檔名字
* @return
*/
public boolean zipDir(String srcPath, String objZipPath, String ojbZipName)
throws Exception {
ZipOutputStream zos = null;
try {
File objFile = new File(objZipPath, ojbZipName);
zos = new ZipOutputStream(new FileOutputStream(objFile));
if (srcPath == null) {
System.out.println("傳入的源檔案夾路徑字串不可為空!");
throw new java.lang.NullPointerException();
}
String dirName = "";
File file = new File(srcPath);
if (!file.isDirectory()) {
throw new Exception("傳入了不正確的源檔案夾路徑!");
} else {
dirName = srcPath.substring(srcPath.lastIndexOf("/") + 1);
if (dirName == null || "".equals(dirName)) {
String subStr = srcPath.substring(0, srcPath.length() - 2);
dirName = subStr.substring(subStr.lastIndexOf("/") + 1);
}
ZipEntry ze = new ZipEntry(dirName + "/");
zos.putNextEntry(ze);
if (dirName == null || "".equals(dirName)) {
throw new Exception("傳入了不正確的源檔案夾路徑!");
}
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(dirName + "/", files[i], zos);
}
return true;
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 用來壓縮檔夾下的所有子檔案 此方法為一個遞迴調法方法
*
* @param zipPath
* 要壓縮的檔案在壓縮包中的相對路徑
* @param file
* 要壓縮的檔案引用
* @param zos
* 壓縮檔輸出資料流
* @throws IOException
*/
private void zipFile(String zipPath, File file, ZipOutputStream zos)
throws IOException {
// 是檔案夾的操作
if (file.isDirectory()) {
ZipEntry ze = new ZipEntry(zipPath + file.getName() + "/");
zos.putNextEntry(ze);
// 遞迴調用
for (int i = 0; i < file.listFiles().length; i++) {
zipFile(zipPath + file.getName() + "/", file.listFiles()[i],
zos);
}
// 是檔案時的操作
} else {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ZipEntry ze = new ZipEntry(zipPath + file.getName());
zos.putNextEntry(ze);
byte[] b = new byte[4096];
int i = 0;
while ((i = (fis.read(b))) > 0) {
zos.write(b, 0, i);
}
} finally {
if (fis != null) {
fis.close();
}
}
}
}
public static void main(String[] args) {
ZipUtil zt = new ZipUtil();
try {
zt.zipDir("c:/gif", "c:/", "test.zip");
} catch (Exception e) {
e.printStackTrace();
}
}
}