代码&优雅着&生活

导航

Zip包解压工具类

最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错。

package com.winning.polaris.admin.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ZipUtil {
    private static final Logger logger = Logger.getLogger(ZipUtil.class);

    /**
     *
     * 解压压缩文件
     * @param filePath 目标文件路径
     * @param fileName 目标文件名称
     * @param decompressionPath 解压到的目录路径
     * @return
     */
    public static boolean decompressionZipFile(String filePath, String fileName, String decompressionPath) {
        boolean result = true;
        File file = new File(filePath +File.separator+ fileName);
        if(!file.exists()){
            logger.info("【解压压缩文件】时间:"+TimeTool.getDateTime()+"文件路径" + filePath +File.separator+ fileName+"文件不存在");
            return false;
        }
        ZipArchiveInputStream zipArchiveInputStream = null;
        BufferedOutputStream os=null;
        try {
            zipArchiveInputStream =new ZipArchiveInputStream(new FileInputStream(file)) ;
            ArchiveEntry archiveEntry = null;
            String firstFolderName=fileName.substring(0,fileName.indexOf(".zip"));
            int count=0;
            while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {
                logger.info("时间:"+TimeTool.getDateTime()+"解压文件名称:" + archiveEntry.getName());
                boolean sign=StringUtils.equals((firstFolderName+"/").toLowerCase(),archiveEntry.getName().toLowerCase());
                count++;
                if(count==1 && !sign){
                    decompressionPath=decompressionPath+File.separator+firstFolderName;
                }
                File outFile =new File(decompressionPath,archiveEntry.getName());
                if(archiveEntry.isDirectory()) {
                    if (!outFile.exists())
                        outFile.mkdirs();
                }else{
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(outFile));
                        IOUtils.copy(zipArchiveInputStream, os);
                    }finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } catch (Exception ex) {
            result = false;
            logger.error("【解压更新文件】时间:" + TimeTool.getDateTime() + ",解压文件出错:" + ex.getMessage());
            ex.printStackTrace();
        }finally {
            try {
                if (zipArchiveInputStream != null) {
                    zipArchiveInputStream.close();
                }
            }catch (Exception e){
                //result = false;
                logger.error("【解压更新文件】时间:" + TimeTool.getDateTime() + ",关闭流出错:" + e.getMessage());
                e.printStackTrace();
            }
        }
        return result;
    }



    public static void main(String[] args) {

        String filePath = "F:\\hmap111\\HMAP_V2.0";
        String fileName = "megrez.zip";
        String decompressionPath = "F:\\hmap111\\HMAP_V2.0";
        System.out.println(decompressionZipFile(filePath, fileName, decompressionPath));
    }
}

 

posted on 2018-04-09 16:57  幸运的凌人  阅读(411)  评论(0编辑  收藏  举报