---
title: " java zip递归压缩解压代码"
author: "yefei"
date: "2009-04-06T21:47:19Z"
tags: []
slug: "java-zip"
draft: false
lastmod: "2009-04-06T21:47:19Z"
id: 362447
url: "https://yefei.iteye.com/blog/362447"
---

ZIP压缩类

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.BufferedInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.ArrayList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.List;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.zip.ZipEntry;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.zip.ZipOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ZipCompress ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param args  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String\[] args) throws IOException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        compress("D:/tomcat-5.5.20","d:/test/testZip.zip");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 递归压缩文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param source 源路径,可以是文件,也可以目录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param destinct  目标路径,压缩文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    private static void compress(String source,String destinct) throws IOException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List fileList=loadFilename(new File(source));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(destinct)));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte\[] buffere=new byte\[8192];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int length;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BufferedInputStream bis;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        for(int i=0;i&lt;fileList.size();i++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File file=(File) fileList.get(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            zos.putNextEntry(new ZipEntry(getEntryName(source,file)));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            bis=new BufferedInputStream(new FileInputStream(file));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            while(true) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                length=bis.read(buffere);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if(length==-1) break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                zos.write(buffere,0,length);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            bis.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            zos.closeEntry();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        zos.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 递归获得该文件下所有文件名(不包括目录名)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param file  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    private static List loadFilename(File file) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        List filenameList=new ArrayList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if(file.isFile()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            filenameList.add(file);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if(file.isDirectory()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            for(File f:file.listFiles()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                filenameList.addAll(loadFilename(f));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return filenameList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 获得zip entry 字符串  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param base  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param file  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    private static String getEntryName(String base,File file) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File baseFile=new File(base);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String filename=file.getPath();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //int index=filename.lastIndexOf(baseFile.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if(baseFile.getParentFile().getParentFile()==null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return filename.substring(baseFile.getParent().length());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return filename.substring(baseFile.getParent().length()+1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 ZIP解压类

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.BufferedOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Enumeration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.zip.ZipEntry;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.zip.ZipFile;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ZipDecompression ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param args  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws IOException   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String\[] args) throws IOException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        decompression("d:/test/testZip.zip","d:/test/un");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 解压ZIP文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param zipFile  要解压的ZIP文件路径  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param destination  解压到哪里  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void decompression(String zipFile,String destination) throws IOException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ZipFile zip=new ZipFile(zipFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Enumeration en=zip.entries();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ZipEntry entry=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte\[] buffer=new byte\[8192];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int length=-1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        InputStream input=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BufferedOutputStream bos=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File file=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        while(en.hasMoreElements()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            entry=(ZipEntry)en.nextElement();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if(entry.isDirectory()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                System.out.println("directory");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                continue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            input=zip.getInputStream(entry);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            file=new File(destination,entry.getName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if(!file.getParentFile().exists()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                file.getParentFile().mkdirs();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            bos=new BufferedOutputStream(new FileOutputStream(file));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            while(true) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                length=input.read(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if(length==-1) break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                bos.write(buffer,0,length);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            bos.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            input.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        zip.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}