java 解压文件

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnZip {
    
    /**
     * @param args
     */
    static final int BUFFER = 2048;
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        try{
            String filename = "D:/1.zip";
            int index1=filename.lastIndexOf(".");
            String zipName=filename.substring(0,index1);
            ZipFile zipFile = new ZipFile(filename);
            Enumeration enu = zipFile.entries();
            new File(zipName).mkdirs();
            while(enu.hasMoreElements()){
                ZipEntry zipEntry = (ZipEntry)enu.nextElement();
                if(zipEntry.isDirectory()){
                    new File(zipName+"/"+zipEntry.getName()).mkdirs();
                    continue;
                }
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                File file = new File(zipName+"/"+zipEntry.getName());
                File parent = file.getParentFile();
                if(parent != null && !parent.exists()){
                    parent.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);
                byte[] array = new byte[BUFFER];
                while((bis.read(array, 0, BUFFER))!=-1){
                    bos.write(array, 0, BUFFER);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java)