1. 程式人生 > >zip壓縮下載工具類,特別是注意壓縮後,winrar上檔名亂碼的解決方法

zip壓縮下載工具類,特別是注意壓縮後,winrar上檔名亂碼的解決方法

package org.hnjk.core.comm.util;




import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.zip.ZipInputStream;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


/**
 * 提供�?��壓縮、解壓縮檔案的工具類. <p>
 * 
 */
public class ZipUtils {


protected static Log logger = LogFactory.getLog(ZipUtils.class);


/*
* 對目錄的壓縮.
* @pamar out 壓縮檔案�?
* @pamar f 目標檔案
* @pamar base 基本目錄
*/
private static void directoryZip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {// 如果傳入的是目錄
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));// 建立壓縮的子目錄
if (base.length() == 0) {
base = "";
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
directoryZip(out, fl[i], base + fl[i].getName());
}
} else {
// 把壓縮檔案加入rar�?
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = in.read(bb)) != -1) {
out.write(bb, 0, aa);
}
in.close();
}
}


/*
* 壓縮單個檔案.
* @pamar zos 壓縮檔案�?
* @pamar file 目標檔案
*/
private static void fileZip(ZipOutputStream zos, File file) throws Exception {
if (file.isFile()) {
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] bb = new byte[2048];
int aa = 0;
while ((aa = fis.read(bb)) != -1) {
zos.write(bb, 0, aa);
}
fis.close();
} else {
directoryZip(zos, file, "");
}
}

/*
* 解壓單個檔案.
* @param zis 壓縮檔案�?
* @param file 目標檔案
*/
private static void fileUnZip(ZipInputStream zis, File file) throws Exception {
java.util.zip.ZipEntry zip = zis.getNextEntry();

if (zip == null)
return;
String name = zip.getName();
File f = new File(file.getAbsolutePath() + "/" + name);
if (zip.isDirectory()) {
f.mkdirs();
fileUnZip(zis, file);
} else {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
byte b[] = new byte[2048];
int aa = 0;
while ((aa = zis.read(b)) != -1) {
fos.write(b, 0, aa);
}
fos.close();
fileUnZip(zis, file);
}
}

/**
* 解壓檔案
* @param 源文�?
* @param 目標輸出路徑 
*/
public static void fileUnZip(String unZipfileName,String outputDirectory,String encoding){//unZipfileName�?��解壓的zip檔案�?
        FileOutputStream fileOut;
        File file;
        InputStream inputStream;


        try{
       
        org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(unZipfileName,encoding);


            for(java.util.Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();){
                ZipEntry entry = (ZipEntry)entries.nextElement();
                file = new File(outputDirectory+File.separator+entry.getName());


                if(entry.isDirectory()){
                    file.mkdirs();
                }
                else{
                    //如果指定檔案的目錄不存在,則建立之.
                    File parent = file.getParentFile();
                    if(!parent.exists()){
                        parent.mkdirs();
                    }


                    inputStream = zipFile.getInputStream(entry);


                    fileOut = new FileOutputStream(file);
                    int c;
           byte[] by=new byte[1024];
           while((c=inputStream.read(by)) != -1){
            fileOut.write(by,0,c);
           }
           inputStream.close();
           fileOut.close();
                }    
            }
           
        }catch(Exception ioe){
            ioe.printStackTrace();
        }
    } 

/**
* 對目標檔案源進行壓縮.
* @param directory �?��壓縮的檔案源
* @param zipFile 新的壓縮檔案�?
*/
public static void zip(String directory, String zipFile) throws Exception{
org.apache.tools.zip.ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
fileZip(zos, new File(directory));
zos.close();

}

/**
* 對目標檔案進行解�?
* @param directory 解壓後的目標資料夾子
* @param zipFile �?��解壓的壓縮包
*/
public static void unZip(String directory, String zipFile) {
try {
//ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
org.apache.tools.zip.ZipFile zis = new org.apache.tools.zip.ZipFile(zipFile);

// File f = new File(directory);
// f.mkdirs();
// fileUnZip(zis, f);
// zis.close();
if(null != zis){
fileUnZip(zipFile, directory,zis.getEncoding());
}
//}else{
// fileUnZip(zipFile, directory,"UTF-8");
//}

} catch (Exception e) {
logger.error("Unzip file "+zipFile+" error:"+e.getMessage());
}
}

/**
* 對目標檔案打包壓縮
* @param filePath 目標檔案路徑
* @param destFileName 匯出壓縮包名
*/
public static void fileToZip(String filePath, String destFileName) throws IOException {
  File souceFile = new File(filePath);  
       FileOutputStream fileOut = null;  
        try {  
            fileOut = new FileOutputStream(destFileName);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
        ZipOutputStream out = new ZipOutputStream(fileOut);  
        out.setEncoding("UTF-8");   
        zipFile(souceFile, out, "");  
        out.close();  
       
}


/**
* 對目標檔案 寫入壓縮包
* 解決壓縮之後,用winrar開啟的亂碼問題
* 1.給ZipOutputStream 增加編碼輸出 out.setEncoding("UTF-8");
* 2.解決檔案linux輸出亂碼  entry.setUnixMode(644);//解決linux亂碼
* @author lhw
*/
public static void zipFile(File souceFile, ZipOutputStream out, String base) throws IOException {
out.setEncoding("UTF-8");   
if (souceFile.isDirectory()) {  
           File[] files = souceFile.listFiles();  
           if(base!=""){
           out.putNextEntry(new ZipEntry(base + "/"));  
           }
           base = base.length() == 0 ? "" : base + "/";  
          
           for (File file : files) {  
            zipFile(file, out, base + URLEncoder.encode(file.getName(), "UTF-8"));  
           } 
          
          
       } else {  
        base = URLDecoder.decode(base, "UTF-8");
        //base = URLDecoder.decode(base, "UTF-8");
        ZipEntry entry = null;
           if (base.length() > 0) {  
            entry = new ZipEntry(base);
            entry.setUnixMode(644);//解決linux亂碼
               out.putNextEntry(entry);  
           } else {  
            entry = new ZipEntry(souceFile.getName());
            entry.setUnixMode(644);//解決linux亂碼
               out.putNextEntry(entry );  
           }  
           FileInputStream in = new FileInputStream(souceFile);  
 
           int b;  
           byte[] by = new byte[1024]; 
           out.setEncoding("UTF-8");   
           while ((b = in.read(by)) != -1) {  
               out.write(by, 0, b);  
           }  
           in.close();  
       } 
}

public static void main(String[] args) throws Exception{
fileToZip("d:\\Test","d:\\temp33.zip");
//unZip("c:\\test", "c:\\test.zip");
}

}