解压ZIP文件工具类

 解压ZIP文件工具类

package com.xx;
 
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
/**
 * @author Zongkai.Li
 * @version 1.0
 * @date 2020/12/1 16:17
 */
public class UnZipUtils {
 
//    使用此工具类的一般形式为:
 
//    String path = "D:"+ File.separator +"fileupload"+ File.separator;
//    List<String> unZipFileList = UnZipUtils.UnZip(multipartFile, path);
//    通过unZipFileList 对解压后的文件进行操作
//    boolean isDelete = UnZipUtils.deleteTempFileList(unZipFileList);
//    UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP + File.separator));
 
//    如果确保指定目录没有其他不需要删除的文件或文件夹,也可以更直接的使用  UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP)); 方法来进行删除.
    public static final String TEMP = "temp";
 
    /**
     * @Description: 解压zip文件到指定目录,返回解压后的文件路径列表.   解压后的文件在tempDir参数的temp文件夹下
     * @Param:  multipartFile = zip文件的multipartFile对象;  tempDir  = 文件将要解压到tempDir目录下     注意:tempDir最后需要加操作系统对应的目录分隔符 /或\
     * @return: 返回解压后的文件的路径列表,返回null表示解压失败.
     * @Author: Zongkai.Li
     */
    public static List<String> UnZip(MultipartFile multipartFile, String tempDir){
        List<String> fileList = new ArrayList<>();
        try(ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream(), Charset.forName("GBK"))) {
            ZipEntry entry = zipInputStream.getNextEntry();
            tempDir = tempDir + TEMP + File.separator;
            while (entry != null){
                File file = createFileAndAddFileList(tempDir, fileList, entry);
                if (entry.getName().contains(".")) {
                    unZipFile(zipInputStream, file);
                }
                entry = zipInputStream.getNextEntry();
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return null;
        }
        return fileList;
    }
 
    /**
     * @Description: 解压zip文件
     * @Param: file=解压后文件的存放路径
     * @Author: Zongkai.Li
     */
    private static void unZipFile(ZipInputStream zipInputStream, File file) throws IOException {
        try (FileOutputStream fileOutput = new FileOutputStream(file)){
            byte[] buffer = new byte[4096];
            for (int bufferSize = zipInputStream.read(buffer); bufferSize > 0; bufferSize = zipInputStream.read(buffer)) {
                fileOutput.write(buffer, 0, bufferSize);
            }
        }
    }
 
    /**
     * @Description: 解压zip文件到指定目录,返回解压后的文件路径列表.     解压后的文件在tempDir参数的temp文件夹下
     * @Param:  multipartFile = zip文件的流对象;  tempDir  = 文件将要解压到tempDir目录下     注意:tempDir最后需要加操作系统对应的目录分隔符 /或\
     * @return: 返回解压后的文件的路径列表,返回null表示解压失败.
     * @Author: Zongkai.Li
     */
    public static List<String> UnZip(InputStream inputStream, String tempDir){
        List<String> fileList = new ArrayList<>();
        try(ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"))) {
            ZipEntry entry = zipInputStream.getNextEntry();
            tempDir = tempDir + TEMP + File.separator;
            while (entry != null){
                File file = createFileAndAddFileList(tempDir, fileList, entry);
                if (entry.getName().contains(".")) {
                    unZipFile(zipInputStream, file);
                }
                entry = zipInputStream.getNextEntry();
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return null;
        }
        return fileList;
    }
    
    /**
     * @Description: 创建解压后的空文件,等待写入.
     * @Author: Zongkai.Li
     */
    private static File createFileAndAddFileList(String tempDir, List<String> fileList, ZipEntry entry) throws IOException {
        File file = new File(tempDir + entry.getName());
        if (!file.isDirectory()) {
            if (!file.exists()) {
                if (file.getParentFile() != null) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            }
            fileList.add(file.getCanonicalPath());
        }
        return file;
    }
    
    /**
     * @Description: 删除解压后的临时文件
     * @Param:  需要删除的文件路径列表   一般是unZipFile方法返回的列表
     * @return:  true表示删除成功,false表示删除失败
     * @Author: Zongkai.Li
     */
    public static boolean deleteTempFileList(List<String> fileList){
        try {
            for (String fileStr : fileList) {
                File file = new File(fileStr);
                if (file.exists()) {
                    file.delete();
                }
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return false;
        }
        return true;
    }
 
    /**
     * @Description: 删除解压后的文件
     * @Param:  需要删除的文件路径  一般是unZipFile方法返回的列表
     * @return:  true表示删除成功,false表示删除失败
     * @Author: Zongkai.Li
     */
    public static boolean deleteTempFile(String filePath){
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
            return false;
        }
        return true;
    }
 
    /**
     * @Description: 删除解压后产生的临时文件夹
     * @Param:  需要删除的目录  如果要删除之前解压产生的临时文件夹,需要传入 new File(folderPath + UnZipUtils.TEMP) 形式的参数
     * @Author: Zongkai.Li
     */
    public static void deleteFolder(File folder) {
        if (!folder.exists()) {
            return;
        }
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }
        folder.delete();
    }
 
 
}
//    使用此工具类的一般形式为:
      String path = "D:"+ File.separator +"fileupload"+ File.separator;
      List<String> unZipFileList = UnZipUtils.UnZip(multipartFile, path);
//    TODO 通过 unZipFileList 对解压后的文件进行操作
//    boolean isDelete = UnZipUtils.deleteTempFileList(unZipFileList);
//    确保指定目录下没有其他不需要删除的文件或文件夹
      UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP + File.separator));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值