java中使用ZipInputStream解压缩文件

package com.company;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

class unzip{
    byte doc[]=null;
    String filename=null;
    String unzipath=null;
    public unzip(String filename,String unzipath)
    {
        this.filename=filename;
        this.unzipath=unzipath;
    }
    public void dounzip()
    {
        try{
            //这里filename是文件名,如xxx.zip
            ZipInputStream zipis=new ZipInputStream(new FileInputStream(filename));
            ZipEntry fentry=null;
            while((fentry=zipis.getNextEntry())!=null)
            {
                //fentry逐个读取zip中的条目,第一个读取的名称为test。
                //test条目是文件夹,因此会创建一个File对象,File对象接收的参数为地址
                //然后就会用exists,判断该参数所指定的路径的文件或者目录是否存在
                //如果不存在,则构建一个文件夹;若存在,跳过
                //如果读到一个zip,也继续创建一个文件夹,然后继续读zip里面的文件,如txt
                if(fentry.isDirectory()){
                    File dir = new File(unzipath+fentry.getName());
                    if(!dir.exists()){
                        dir.mkdirs();
                    }
                }
                else {
                    //fname是文件名,fileoutputstream与该文件名关联
                    String fname=new String(unzipath+fentry.getName());
                    try{
                        //新建一个out,指向fname,fname是输出地址
                        FileOutputStream out = new FileOutputStream(fname);
                        doc=new byte[512];
                        int n;
                        //若没有读到,即读取到末尾,则返回-1
                        while((n=zipis.read(doc,0,512))!=-1)
                        {
                            //这就把读取到的n个字节全部都写入到指定路径了
                            out.write(doc,0,n);
//                            System.out.println(n);
                        }
                        out.close();
                        out=null;
                        doc=null;
                    }catch (Exception ex) {
                        System.out.println("there is a problem");
                    }
                }
            }
            zipis.close();
        }catch (IOException ioex){ System.out.println("io错误:"+ioex);}
        System.out.println("finished!");
    }
}
public class unzipTester {
    public static void main(String[]args)throws IOException,ClassNotFoundException{
        String zipFile=args[0];
        String unzipPath=args[1];
        unzip myzip=new unzip(zipFile,unzipPath);
        myzip.dounzip();
    }
}

下面是文件名和解压缩的路径,注意windows下路径的最后要加上/,然后就是对于test.zip,解压缩读取的第一个entry为test/,因此拼接在一起就是创建一个c:/users/enz/desktop/test的文件夹(因为我的桌面无test文件夹)(还有就是,entry为test/,最后得到的文件的getName是test,而dir输出是c:/users/enz/desktop/test,这里不是c:/users/enz/desktop/test/)

猜你喜欢

转载自blog.csdn.net/hgtjcxy/article/details/81504206