解压文件-压缩文件操作工具类

这个Java代码提供了一个ZipUtil工具类,包含了读取ZIP文件内文件名、文件内容,保存ZIP文件以及获取ZIP文件数量的功能。此外,还包含了解压缩ZIP文件到指定路径的方法。主要涉及IO流、ZipEntry和ZipInputStream等类的操作。
摘要由CSDN通过智能技术生成
package com.dxy.study_0628.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * 压缩文件操作工具类
 */
public class ZipUtil {
    //读取zip文件内的文件,返回文件名称列表
    public static List<String> readZipFileName(String path){
        List<String> list = new ArrayList<>();
        try {
            ZipFile zipFile = new ZipFile(path);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                list.add(entries.nextElement().getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    //读取zip文件内的文件,返回文件内容列表
    public static List<List<String>> readZipFile(String path){
        List<String> list = new ArrayList<>();
        List<List<String>> ddlList=null;
        try {
            ZipFile zipFile = new ZipFile(path);
            InputStream in = new BufferedInputStream(new FileInputStream(path));
            ZipInputStream zin = new ZipInputStream(in);
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                ddlList=new ArrayList<>();
                if (ze.isDirectory()) {
                } else {
                    System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
                    long size = ze.getSize();
                    if (size > 0) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze), Charset.forName("gbk")));
                        String line;
                        while ((line = br.readLine()) != null) {
                            String[] index = line.split(",");
                            List<String> indexList = Arrays.asList(index);
                            ddlList.add(indexList);
                        }
                        br.close();
                    }
                }
                //处理ddlList,此时ddlList为每个文件的内容,while每循环一次则读取一个文件
            }
            zin.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //此处返回无用,懒得修改了
        return ddlList;
    }

    //保存zip文件
    public String saveZipFile(MultipartFile file, String path) {
        String resultPath = "";
        ZipInputStream zipInputStream = null;
        FileOutputStream fs = null;
        try {
            resultPath = "D:" + path+"/" + file.getOriginalFilename();
            File zipFile = new File(resultPath);
            if (!zipFile.exists()) {
                new File(zipFile.getParent()).mkdirs();
                zipFile.createNewFile();
            }
            zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
            BufferedInputStream stream = new BufferedInputStream(file.getInputStream());
            fs = new FileOutputStream(zipFile);
            byte[] buffer = new byte[1024];
            int i = -1;
            while ((i = stream.read(buffer)) != -1) {
                fs.write(buffer, 0, i);
                fs.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                zipInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultPath;
    }

    public int getZipFileCount(String zipFilePath) {
        ZipFile zf = null;
        int count = 0;
        try {
            zf = new ZipFile(zipFilePath);
            count = zf.size();     //返回zip文件中的条目数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return count;
    }


    /**
     * 解压zip压缩包
     * @param path  要解压的文件路径
     * @param name  要解压的文件名
     * @param resultPath    解压后文件存放路径
     */
    public static final void unzip(String path,String name,String resultPath,String code){
        try {
            ZipFile zipFile = new ZipFile(path+"/"+name);
            InputStream in = new BufferedInputStream(new FileInputStream(path+"/"+name));
            ZipInputStream zin = new ZipInputStream(in);
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
                //创建文件
                String newTextPath = resultPath+"/"+ze.getName();
                long size = ze.getSize();
                if (size > 0) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze), Charset.forName(code)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        FileUtil.appendString(line+"\r\n", newTextPath, "UTF-8");
                    }
                    br.close();
                }
            }
            zin.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值