Java ZipUtil


import org.springframework.util.AntPathMatcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    /**
     * 获取zip文件列表
     * @param zipPath zip路径
     * @return
     */
    public static List<String> getZipFilesPaths(String zipPath) {
        List<String> filePathList = ZipUtil.getFileList(zipPath);
        return filePathList;
    }

    private static List<String> getFileList(String zipPath) {
        List<String> result = new ArrayList<>();
        File file = new File(zipPath);
        if(file.exists()){
            try {
                ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath), Charset.forName("GBK"));
                while (true) {
                    ZipEntry zipEntry = zipInputStream.getNextEntry();
                    if (zipEntry==null)
                        break;
                    if(zipEntry.isDirectory()){ //遇到文件夹就跳过
                        continue;
                    }else{
                        result.add(zipEntry.getName());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 压缩文件
     * @param sourceFilePathList 待压缩文件绝对路径列表
     * @param targetFilePath 压缩后zip文件绝对路径
     * @param excludePath 排除目录
     * @return
     * @throws IOException
     */
    private static int compress(List<String> sourceFilePathList, String targetFilePath, String excludePath) throws IOException {
        byte[] buf = new byte[1024];

        File excludeFile = new File(excludePath);

        File zipFile = new File(targetFilePath);
        if(!zipFile.exists())
            zipFile.createNewFile();

        int count = 0;
        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
            for (String sourceFilePath : sourceFilePathList) {
                if(sourceFilePath == null || sourceFilePath.length()<1){
                    continue;
                }
                File sourceFile = new File(sourceFilePath);
                if(sourceFile == null || !sourceFile.exists()){
                    continue;
                }
                FileInputStream fileInputStream = new FileInputStream(sourceFile);

                if (sourceFilePath.endsWith(zipFile.getName().replace(".zip",".xml"))) {
                    zipOutputStream.putNextEntry(new ZipEntry(sourceFile.getName()));
                } else {
                    String relativePath = (sourceFile.getPath()).replace(excludeFile.getPath(), "");
                    if (relativePath.startsWith("/")) {
                        relativePath = relativePath.substring(1);
                    }
                    if (relativePath.startsWith("\\")) {
                        relativePath = relativePath.substring(1);
                    }
                    zipOutputStream.putNextEntry(new ZipEntry(relativePath));
                }

                int len;
                while((len = fileInputStream.read(buf)) > 0){
                    zipOutputStream.write(buf, 0, len);
                }
                zipOutputStream.closeEntry();
                fileInputStream.close();
                count++;
            }
            zipOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 整理待压缩文件路径
     * @param sourcePath 待压缩路径
     * @return
     */
    private static List<String> collectFile(String sourcePath) {
        List<String> result = new ArrayList<>();
        File file = new File(sourcePath);
        File[] listFiles = file.listFiles();
        if (listFiles!=null && listFiles.length>0) {
            for(File file_ : listFiles){
                if(!file_.isDirectory()) {
                    result.add(file_.getPath());
                } else {
                    List<String> tempList = collectFile(file_.getPath());
                    if (tempList!=null && tempList.size()>0) {
                        result.addAll(tempList);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 压缩文件
     * @param sourceFilePath 待压缩文件路径
     * @param targetFilePath 压缩后zip文件路径
     * @throws IOException
     */
    public static int compress(String sourceFilePath, String targetFilePath) throws IOException {
        List<String> sourceFilePathList = ZipUtil.collectFile(sourceFilePath);
        return ZipUtil.compress(sourceFilePathList, targetFilePath, sourceFilePath);
    }
	
	
	/**
     * 解压
     * @param filePath 压缩包全路径
     * @param zipDir 解压路径
     */
    public  static void unzip(String filePath,String zipDir) {
        String name = "";
        try {
            BufferedOutputStream dest = null;
            BufferedInputStream is = null;
            ZipEntry entry;
            ZipFile zipfile = new ZipFile(filePath, Charset.forName("GBK"));
            Enumeration e = zipfile.entries();
            while (e.hasMoreElements()) {
                entry = (ZipEntry) e.nextElement();
                if( entry.isDirectory()){
                    name = entry.getName();
                    name = name.substring(0, name.length() - 1);
                    File fileObject = new File(zipDir + name);
                    fileObject.mkdir();
                }else{
                    is = new BufferedInputStream(zipfile.getInputStream(entry));
                    int count;
                    byte[] dataByte = new byte[1024];
                    FileOutputStream fos = new FileOutputStream(zipDir+entry.getName());
                    dest = new BufferedOutputStream(fos, 1024);
                    while ((count = is.read(dataByte, 0, 1024)) != -1) {
                        dest.write(dataByte, 0, count);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        com.xqx.bdcbsdt.controller.ZipUtil.compress("E:\\sgasg","E:\\sgasg\\1.zip");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值