ant中的zip包解压本地zip文件

本文介绍了一个使用Java实现的ZIP文件解压方法,能够处理嵌套ZIP文件,并创建相应目录结构。通过示例代码展示了如何读取ZIP文件中的条目,判断文件类型并进行解压。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

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

public class ZipFetcher implements IFetcher {
    
byte[] by = new byte[1024];

    
public void fetch(String srcFilepath, String destFilepath) throws Exception {
    }


    
/**
     * 解压zip,需要ant.jar包
     * 
     * 
@param srcFilepath
     *            源文件地址
     * 
@param destFilepath
     *            目标目录
     * 
@throws Exception
     
*/

    
private void unzip(String srcFilepath, String destFilepath)
            
throws Exception {
        
// zip数据流
        ZipFile zipFile = new ZipFile(srcFilepath);
        
// zip中的项
        ZipEntry zipEntry = null;

        Enumeration e 
= zipFile.getEntries();

        
while (e.hasMoreElements()) {
            zipEntry 
= (ZipEntry) e.nextElement();
            System.out.println(
"解压文件 : " + zipEntry.getName());
            
// zipentry是目录
            if (zipEntry.isDirectory()) {
                
// 处理目录
                dealDir(zipEntry, destFilepath);
            }

            
// zipentry是文件
            else {
                
// 处理文件
                dealFile(zipFile, zipEntry, destFilepath);
            }

        }

        zipFile.close();
    }


    
/**
     * 解决zip中嵌套zip
     * 
     * 
@param zipEntry
     *            源zip项
     * 
@param destFilepath
     *            文件保存路径
     * 
@throws Exception
     
*/

    
private void dealZipInZip(ZipEntry zipEntry, String destFilepath)
            
throws Exception {
        
// 嵌套
        String fileName = zipEntry.getName();
        
// 储存路径,降后面的.zip去掉,建立文件夹
        String toPath = destFilepath + File.separator
                
+ fileName.substring(0, fileName.length() - 4);
        
// 源文件
        String srcfile = destFilepath + File.separator + fileName;

        unzip(srcfile, toPath);
// 解析子zip包
        delete(srcfile);
    }


    
/**
     * 如果zip项是目录,创建目录
     * 
     * 
@param zipEntry
     * 
@param destFilepath
     
*/

    
private void dealDir(ZipEntry zipEntry, String destFilepath) {
        String name 
= zipEntry.getName();// 取得文件名,因为是目录,所以最后一位是/
        name = name.substring(0, name.length() - 1);// 去掉最后一位

        
// 建立目录
        File dir = new File(destFilepath + File.separator + name);
        
if (!dir.exists())
            dir.mkdirs();

        System.out.println(
"创建目录:" + destFilepath + File.separator + name);
    }


    
/**
     * zip项是文件,处理文件
     * 
     * 
@param zipFile
     *            全部zip数据流
     * 
@param zipEntry
     *            zip项
     * 
@param destFilepath
     *            目标路径
     * 
@throws Exception
     
*/

    
private void dealFile(ZipFile zipFile, ZipEntry zipEntry,
            String destFilepath) 
throws Exception {
        
// 取得文件的保存路径和文件名
        String fname = destFilepath + File.separator + zipEntry.getName();
        mkpath(fname);
// 建立文件的目录

        write(zipFile.getInputStream(zipEntry), 
new FileOutputStream(fname));

        
if (isZip(zipEntry.getName()))
            dealZipInZip(zipEntry, destFilepath);
    }


    
/**
     * 写入
     * 
     * 
@param in
     * 
@param out
     * 
@throws IOException
     
*/

    
private void write(InputStream in, OutputStream out) throws IOException {
        
int c;
        
try {
            
while ((c = in.read(by)) != -1)
                out.write(by, 
0, c);
        }
 catch (IOException ex) {
            
throw ex;
        }
 finally {
            close(in, out);
        }

    }


    
/**
     * 创建文件的路径
     * 
     * 
@param fname
     *            文件名
     
*/

    
private void mkpath(String fname) {
        File f 
= new File(fname);
        File pf 
= f.getParentFile();
        
if (!pf.exists())
            pf.mkdirs();
    }


    
/**
     * 判断zip项是否还是zip文件
     * 
     * 
@param fname
     * 
@return
     
*/

    
private boolean isZip(String fname) {
        
if (fname.length() < 5)
            
return false;
        
int idx = fname.lastIndexOf(".");
        
if (idx == -1)
            
return false;
        
if (!fname.substring(idx + 1, fname.length()).equalsIgnoreCase("zip"))
            
return false;
        
return true;
    }


    
/**
     * 关闭
     * 
     * 
@param in
     * 
@param out
     
*/

    
private void close(InputStream in, OutputStream out) {
        
if (null != in) {
            
try {
                in.close();
            }
 catch (Exception ex) {
                
// ignore
                ex.printStackTrace();
            }
 finally {
                
if (null != in)
                    in 
= null;
            }

        }

        
if (null != out) {
            
try {
                out.close();
            }
 catch (Exception ex) {
                
// ignore
            }
 finally {
                
if (null != out)
                    out 
= null;
            }

        }

    }


    
/**
     * 删除文件,如果源zip包中包含zip文件,解析后删除所包含的zip文件
     * 
     * 
@param filename
     
*/

    
private void delete(String filename) {
        File f 
= new File(filename);
        
if (f.exists() && f.delete())
            System.out.println(
"delete " + filename + " OK!");
        
else
            System.out.println(
"delete " + filename + " Failed!");
    }


    
public static void main(String[] args) throws Exception {
        ZipFetcher test 
= new ZipFetcher();
        test.unzip(
"f:/111222/121.zip""f:/新建文件夹");

        
// File test = new File("f:/新建文件夹/b1.zip");
        
// test.delete();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值