Java對Zip文件的支持不是很強大斯辰,有一些需要自己實現(xiàn)的代碼,我在網(wǎng)上找了很多代碼坡疼,都不能用于生產(chǎn)彬呻,要不就是流沒有被關(guān)閉,要不就是Exception處理很隨意回梧,下面是我修改過并測試過的代碼能用于生產(chǎn)的壓縮和解壓縮的代碼废岂,做一個代碼備份。
使用遞歸算法將一個文件夾壓縮成Zip文件:
static final int BUFFER = 8192;
public static void compress(String srcPath , String dstPath) throws IOException{
File srcFile = new File(srcPath);
File dstFile = new File(dstPath);
if (!srcFile.exists()) {
throw new FileNotFoundException(srcPath + "不存在狱意!");
}
FileOutputStream out = null;
ZipOutputStream zipOut = null;
try {
out = new FileOutputStream(dstFile);
CheckedOutputStream cos = new CheckedOutputStream(out,new CRC32());
zipOut = new ZipOutputStream(cos);
String baseDir = "";
compress(srcFile, zipOut, baseDir);
}
finally {
if(null != zipOut){
zipOut.close();
out = null;
}
if(null != out){
out.close();
}
}
}
private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (file.isDirectory()) {
compressDirectory(file, zipOut, baseDir);
} else {
compressFile(file, zipOut, baseDir);
}
}
/** 壓縮一個目錄 */
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], zipOut, baseDir + dir.getName() + "/");
}
}
/** 壓縮一個文件 */
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (!file.exists()){
return;
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zipOut.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zipOut.write(data, 0, count);
}
}finally {
if(null != bis){
bis.close();
}
}
}
將Zip文件加壓縮出來湖苞,包含所有文件和文件夾到目標目錄:
public static void decompress(String zipFile , String dstPath)throws IOException{
File pathFile = new File(dstPath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = null;
OutputStream out = null;
try{
in = zip.getInputStream(entry);
String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");;
//判斷路徑是否存在,不存在則創(chuàng)建文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓
if(new File(outPath).isDirectory()){
continue;
}
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
}
finally {
if(null != in){
in.close();
}
if(null != out){
out.close();
}
}
}
zip.close();
}
調(diào)用示例:
public static void main(String[] args)throws Exception{
String targetFolderPath = "/Users/fred/zipFile/zipFolder";
String rawZipFilePath = "/Users/fred/zipFile/raw.zip";
String newZipFilePath = "/Users/fred/zipFile/new.zip";
//將Zip文件解壓縮到目標目錄
CompressUtil.decompress(rawZipFilePath , targetFolderPath);
//將目標目錄的文件壓縮成Zip文件
CompressUtil.compress(targetFolderPath , newZipFilePath);
}