1. 程式人生 > >ZipUtil壓縮解壓工具類——java

ZipUtil壓縮解壓工具類——java



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;


import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


/**
 * 
 * @create 2009-7-25 下午06:17:33
 * @since
 */
public class ZipUtil {


public static final String FILE_SEPARATOR = System
.getProperty("file.separator");


/**
* 將指定的檔案解壓縮到指定的資料夾,解壓後的資料夾目錄和給定的壓縮檔名相同.

* @param zipFilePath
*            全路徑
* @param unZipDirectory
*            全路徑
* @return 解壓縮檔案是否成功.
* @throws IOException
*/
@SuppressWarnings("unused")
public static boolean unZipFile(String zipFilePath, String unZipDirectory)
throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<?> entries = zipFile.getEntries();
if (zipFile == null) {
return false;
}
while (entries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
File f = new File(unZipDirectory + FILE_SEPARATOR
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!f.exists() && !f.mkdirs())
throw new IOException("Couldn't create directory: " + f);
} else {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(
zipFile.getInputStream(zipEntry));
File destDir = f.getParentFile();
if (!destDir.exists() && !destDir.mkdirs()) {
throw new IOException("Couldn't create dir " + destDir);
}
os = new BufferedOutputStream(new FileOutputStream(f));
int b = -1;
while ((b = is.read()) != -1) {
os.write(b);
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
}
}
}
zipFile.close();
return true;
}


/**
* 壓縮一個檔案

* @param filePath被壓縮的檔案路徑
* @param zipPath壓縮後的存放路徑
* @param zipfileName壓縮後的檔名稱
* @return
*/
public static boolean zipFile(String filePath, String zipPath, String zipfileName) {
BufferedReader in = null;
ZipOutputStream out = null;
try {
File file = new File(filePath);
in = new BufferedReader(new InputStreamReader(new FileInputStream(
filePath), "ISO-8859-1"));
FileOutputStream f = new FileOutputStream(zipPath+"/"+zipfileName+".zip");
CheckedOutputStream ch = new CheckedOutputStream(f, new CRC32());
out = new ZipOutputStream(new BufferedOutputStream(ch));
//out.setEncoding("GBK");
int c;
out.putNextEntry(new ZipEntry(file.getName()));
while ((c = in.read()) != -1)
out.write(c);
}


catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}


/**
* 壓縮一個目錄
* @param dir 被壓縮的資料夾
* @param zipPath壓縮後的存放路徑
* @param zipfileName壓縮後的檔名稱
* @return
*/
public static boolean zipDirectory(String dir, String zipPath, String zipfileName) {
ZipOutputStream out = null;
try {
File dirFile = new File(dir);
if (!dirFile.isDirectory())
return false;
FileOutputStream fo = new FileOutputStream(zipPath+"/"+zipfileName+".zip");
CheckedOutputStream ch = new CheckedOutputStream(fo, new CRC32());
out = new ZipOutputStream(new BufferedOutputStream(ch));
//out.setEncoding("GBK");
zip(out, dirFile, "");


} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}


public static void zip(ZipOutputStream out, File f, String base)
throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream is = new FileInputStream(f);
BufferedInputStream in = new BufferedInputStream(is);// 修改BUG!二進位制輸出採用buffered
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}


}

 /**
     * 對臨時生成的資料夾和資料夾下的檔案進行刪除
     */
    public static void deletefile(String delpath) {
        try {
            File file = new File(delpath);
            if (!file.isDirectory()) {
                file.delete();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + File.separator + filelist[i]);
                    if (!delfile.isDirectory()) {
                        delfile.delete();
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + File.separator + filelist[i]);
                    }
                }
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


public static void main(String[] args) {

//解壓檔案字尾必須是zip,否則報錯。 
//String zipFilePath = "D:\\axis.zip"; 
//String unZipDirectory = "D:\\"; 
//try { 
//boolean flag = ZipUtil1.unZipFile(zipFilePath, unZipDirectory); 
//if (flag) {
//System.out.println("解壓成功");
//}
//} catch (IOException e) { // TODO Auto-generated catch block 
//e.printStackTrace(); 
//}
 
//壓縮檔案 中文名稱測試
//ZipUtil1.zipFile("D:\\副本.log", "D:\\","中");
//壓縮目錄
ZipUtil.zipDirectory("D:\\新建資料夾", "D:\\", "新建");
//刪除臨時檔案
//ZipUtil1.deletefile("D:\\新建資料夾\\副本.log");
}


}