解压zip压缩文件

解压zip格式文件,可以解压直接压缩的文件,也可以解压放在一个文件夹中压缩的zip文件

1.不需要引入其他任何jar包

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

2.当多个文件放在一个文件夹中压缩的时候,解压的时候有可能没有先解压文件夹,导致其他文件找不到路径而报错(猜测的原因)

   所以代码选择直接先解压文件夹后再解压文件

filepath:压缩文件的完整路径

Basepath:压缩文件所在文件夹位置

public  String unzip(String filepath,String BasePath) {
        String name = "";
        try {
            BufferedOutputStream dest = null;
            BufferedInputStream is = null;
            ZipEntry entry;
            ZipFile zipfile = new ZipFile(filepath);
            int count1 = 1;
            Enumeration dir = zipfile.entries();
            while (dir.hasMoreElements()){
                entry = (ZipEntry) dir.nextElement();

                System.out.println("第"+ count1+"个文件");
                count1++;
                if( entry.isDirectory()){
                    name = entry.getName();
                    name = name.substring(0, name.length() - 1);
                    File f = new File(BasePath + name);
                    System.out.println("文件夹路径****    "+BasePath + name);
                    f.mkdir();
                }
            }

            Enumeration e = zipfile.entries();
            while (e.hasMoreElements()) {
                entry = (ZipEntry) e.nextElement();
           if( entry.isDirectory()){
           }else{
               System.out.println("Extracting: " + entry);
               is = new BufferedInputStream(zipfile.getInputStream(entry));
               int count;
               byte data[] = new byte[BUFFER];
               FileOutputStream fos = new FileOutputStream(BasePath+entry.getName());
               dest = new BufferedOutputStream(fos, BUFFER);
               while ((count = is.read(data, 0, BUFFER)) != -1) {
                   dest.write(data, 0, count);
               }
               dest.flush();
               dest.close();
               is.close();
           }
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  name;
    }

3.测试代码

 public static void main(String argv[]) {
            try {
                BufferedOutputStream dest = null;
                BufferedInputStream is = null;
                ZipEntry entry;
                ZipFile zipfile = new ZipFile("D:/aaa.zip");
                Enumeration e = zipfile.entries();
                while (e.hasMoreElements()){
                    entry = (ZipEntry) e.nextElement();
                    if( entry.isDirectory()){
                        String name = entry.getName();
                        name = name.substring(0, name.length() - 1);
                        File f = new File("D:/" + name);
                        f.mkdir();
                        System.out.println("Extracting******: " + name);
                    }
                }

                Enumeration f = zipfile.entries();
                while (f.hasMoreElements()) {
                    entry = (ZipEntry) f.nextElement();
                    if( entry.isDirectory()){
                    }else{
                        System.out.println("Extracting: " + entry);
                        is = new BufferedInputStream
                                (zipfile.getInputStream(entry));
                        int count;
                        byte data[] = new byte[BUFFER];
                        FileOutputStream fos = new
                                FileOutputStream("D:/"+entry.getName());
                        dest = new
                                BufferedOutputStream(fos, BUFFER);
                        while ((count = is.read(data, 0, BUFFER))
                                != -1) {
                            dest.write(data, 0, count);
                        }
                        dest.flush();
                        dest.close();
                        is.close();
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

猜你喜欢

转载自blog.csdn.net/xyy1028/article/details/80547728