java中使用ZipInputStream解压缩文件

本文介绍如何在Java中使用ZipInputStream进行文件解压缩。通过指定文件名和解压缩路径,例如在Windows环境下,注意路径末尾需加'/'。以test.zip为例,解压后会在桌面创建一个名为test的文件夹。需要注意的是,虽然entry为'test/',但实际创建的文件名为'test',目录为'c:/users/enz/desktop/test',而非'c:/users/enz/desktop/test/'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值