当前位置:网站首页>Tool classes for extracting zip files

Tool classes for extracting zip files

2022-06-12 09:03:00 LiBie lzh

This method can directly decompress zip To the specified directory ,zip The file name in cannot contain traditional characters .


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

/**
 * @description:  Unzip file 
 * @author: libie
 * @createTime: 2022/6/10  In the morning  08:50
 **/
public class ZipFileUtil {

    /**
     * @description:  Unzip file 
     * @author:  H2103424
     * @dateTime: 2022/6/10  Afternoon  01:50
     *
     * @param file  Need to decompress zip file 
     * @param uncompressPath  Unzip the destination directory 
     * @return  Files and folders contained in the compressed package 
     * @throws IOException
     */
    public static List<String> uncompress(File file, String uncompressPath) throws IOException {
        List<String> filePaths = new ArrayList<>();
        File path = new File(uncompressPath);
        if (!path.exists()){
            if (!path.mkdirs())
                throw new IOException(" Decompression path creation failed :"+uncompressPath);
            System.out.println(" The decompression path was created successfully :"+uncompressPath);
        }

        BufferedOutputStream os = null;
        BufferedInputStream is = null;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(file);

        Enumeration ele = zipfile.entries();
        while (ele.hasMoreElements()) {
            try {
                entry = (ZipEntry) ele.nextElement();
            } catch (IllegalArgumentException e){
                System.err.println(" Illegal folder or file name , Please do not include traditional Chinese characters and special characters :"+e);
                continue;
            }
            if( entry.isDirectory()){
                System.out.println(" Create folder  "+entry.getName());

                String name = entry.getName();
                name = name.substring(0, name.length() - 1);
                File fileObject = new File(uncompressPath + name);
                if (!fileObject.exists() && !fileObject.mkdir()){
                    System.err.println(" Folder creation failed :"+fileObject.getName());
                }
            }else{
                System.out.println(" Unzip the file    "+entry.getName());

                is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                int buffer = 1024;
                byte[] dataByte = new byte[buffer];
                FileOutputStream fos = new FileOutputStream(uncompressPath+entry.getName());
                os = new BufferedOutputStream(fos, buffer);
                while ((count = is.read(dataByte, 0, buffer)) != -1) {
                    os.write(dataByte, 0, count);
                }
                os.flush();
                os.close();
                is.close();

            }
            filePaths.add(uncompressPath+entry.getName());
        }
        zipfile.close();
        return filePaths;
    }

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\H2103424\\Desktop\\Pictures.zip");
        System.out.println(uncompress(file, "C:\\Users\\H2103424\\Desktop\\testZip\\"));
    }
}

Compressed package :

  

Decompression effect :

  Method output and return value :


 The decompression path was created successfully :C:\Users\H2103424\Desktop\testZip\
 Create folder  Pictures/
 Unzip the file    Pictures/1.bmp
 Unzip the file    Pictures/1.jpeg
 Unzip the file    Pictures/1.jpg
 Create folder  Pictures/1a/
 Unzip the file    Pictures/1a/1.jpeg
 Unzip the file    Pictures/1a/1.jpg
 Create folder  Pictures/1a/3c/
 Unzip the file    Pictures/1a/3c/2.jpg
 Unzip the file    Pictures/1a/3c/3.jpg
 Create folder  Pictures/1a/4d/
 Unzip the file    Pictures/2.jpg
 Create folder  Pictures/2b/
 Unzip the file    Pictures/2b/2.jpg
 Unzip the file    Pictures/3.jpg
 Unzip the file    Pictures/3241_45235_342.3ds
 Unzip the file    Pictures/4.jpg
 Create folder  Pictures/5e/
 Unzip the file    Pictures/aaa.jpg
 Unzip the file    Pictures/desktop.ini
 Unzip the file    Pictures/m03_j03_sa.dwg
 Unzip the file    Pictures/rewq_rewq.dwg
[C:\Users\H2103424\Desktop\testZip\Pictures/, C:\Users\H2103424\Desktop\testZip\Pictures/1.bmp, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/4d/, C:\Users\H2103424\Desktop\testZip\Pictures/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/2b/, C:\Users\H2103424\Desktop\testZip\Pictures/2b/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3241_45235_342.3ds, C:\Users\H2103424\Desktop\testZip\Pictures/4.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/5e/, C:\Users\H2103424\Desktop\testZip\Pictures/aaa.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/desktop.ini, C:\Users\H2103424\Desktop\testZip\Pictures/m03_j03_sa.dwg, C:\Users\H2103424\Desktop\testZip\Pictures/rewq_rewq.dwg]

Process finished with exit code 0

Controller call :

    @Override
    public ResultObj uploadPhotos(MultipartFile file, WebSysUserEntity loginUser){
        String uncompressPath = "C:\\Users\\H2103424\\Desktop\\testZip\\";
        try(InputStream inputStream = file.getInputStream()){
            File photosZip = new File("photos.zip");
            FileUtils.copyInputStreamToFile(inputStream, photosZip);
            photosZip.delete();
            ZipFileUtil.uncompress(photosZip, uncompressPath);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

原网站

版权声明
本文为[LiBie lzh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120856486118.html