JAVA 解压ZIP

/*
提供zip文件的解压缩接口:
AdapterZipFile:
   输入:zipFileName(zip文件的绝对路径),outputDirectory(zip文件解压缩后的存放路径)
   输出:
   说明:初始化函数
unZipFile:
   输入:无
   输出:
   说明:解压缩zip文件,解压缩后存放到outputDirectory路径下
GetZipFileList:
   输入:无
   输出:list
   说明:解压缩后的文件列表绝对路径存放在ArrayList动态数组中
*/


package com.mop.pushmail.adapter.htmlziprar;

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值