当前位置: 代码迷 >> J2EE >> JAVA 解压ZIP
  详细解决方案

JAVA 解压ZIP

热度:632   发布时间:2013-11-10 00:41:19.0
/* 
 提供zip文件的解压缩接口: 
 AdapterZipFile: 
   输入:zipFileName(zip文件的绝对路径),outputDirectory(zip文件解压缩后的存放路径) 
   输出: 
   说明:初始化函数 
 unZipFile: 
   输入:无 
   输出: 
   说明:解压缩zip文件,解压缩后存放到outputDirectory路径下 
 GetZipFileList: 
   输入:无 
   输出:list 
   说明:解压缩后的文件列表绝对路径存放在ArrayList动态数组中 
    http://www.daimami.com 
 */
  
  
package com.mop.pushmail.adapterziprar; 
  
import java.io.*; 
import java.util.*; 
import org.apache.tools.zip.*; 
  
public class AdapterZipFile { 
    String zipFileName = null;//zip文件的绝对路径 
    String outputDirectory = null;//zip文件解压缩后的存放路径 
    ArrayList list = new ArrayList();//文件解压缩后的文件列表绝对路径 
  
//    public AdapterZipFile(String zipFileName) { 
//        if(zipFileName == null){ 
//            return; 
//        } 
//        this.zipFileName = zipFileName.replace("/","\\"); 
//        unZipFile(); 
//    } 
  
    //初始化 
    public AdapterZipFile(String zipFileName, String outputDirectory){ 
        if(zipFileName == null){ 
            return; 
        } 
        this.zipFileName = zipFileName; 
        if(outputDirectory != null){ 
             this.outputDirectory = outputDirectory; 
        }else{ 
            this.outputDirectory = zipFileName.substring(0, 
                    zipFileName.replace('\\', '/').lastIndexOf("/")); 
        } 
       // unZipFile(); 
    } 
  
   //创建目录 
    private void createDirectory(String directory, String subDirectory) { 
        String dir[]; 
        File fl = new File(directory); 
        try { 
            if (subDirectory == "" && fl.exists() != true) 
                fl.mkdirs(); 
            else if (subDirectory != "") { 
                dir = subDirectory.replace("\\","/").split("/"); 
                for (int i = 0; i < dir.length; i++) { 
                    File subFile = new File(directory + File.separator + dir[i]); 
                    if (!subFile.exists()) 
                        subFile.mkdir(); 
                    directory += File.separator + dir[i]; 
                } 
            } 
        }catch (Exception ex) { 
            System.out.println(ex.getMessage()); 
        } 
    } 
  
    //解压缩文件 
    //如果outputDierctory为null,解压到zipFileName文件的当前目录下 
    public void unZipFile() { 
        File file = new File(zipFileName); 
        if (!file.exists()) { 
            return; 
        } 
        createDirectory(outputDirectory, ""); 
  
        try { 
            ZipFile zipFile = new ZipFile(zipFileName); 
            Enumeration e = zipFile.getEntries(); 
            ZipEntry zipEntry = null; 
            while (e.hasMoreElements()) { 
                zipEntry = (ZipEntry) e.nextElement(); 
                if (zipEntry.isDirectory()) { 
                    String name = zipEntry.getName(); 
                    name = name.substring(0, name.length() - 1); 
                    File f = new File(outputDirectory + File.separator + name); 
                    f.mkdir(); 
                } else { 
                    String fileName = zipEntry.getName(); 
                    fileName = fileName.replace('\\', '/'); 
                    //文件在多层目录下 这时需要创建目录如:d1/d2/1.txt 
                    if (fileName.indexOf("/") != -1) { 
                        createDirectory(outputDirectory, fileName.substring(0, 
                                fileName.lastIndexOf("/"))); 
                        fileName = fileName.substring( 
                                fileName.lastIndexOf("/") + 1, fileName 
                                .length()); 
                    } 
  
                    list.add(outputDirectory + File.separator 
                                      + zipEntry.getName()); 
                    File f = new File(outputDirectory + File.separator 
                                      + zipEntry.getName()); 
  
                    f.createNewFile(); 
                    InputStream in = zipFile.getInputStream(zipEntry); 
                    FileOutputStream out = new FileOutputStream(f); 
  
                    byte[] by = new byte[1024]; 
                    int c; 
                    while ((c = in.read(by)) != -1) { 
                        out.write(by, 0, c); 
                    } 
                    out.close(); 
                    in.close(); 
                } 
            } 
        } catch (Exception ex) { 
            System.out.println(ex.getMessage()); 
        } 
    } 
  
   //返回zip文件解压撒后文件列表的绝对路径 
    public ArrayList GetZipFileList(){ 
        return list; 
    } 
}

  相关解决方案