解压,压缩 Windows文件 or 文件夹

用于解压,压缩Windows上的压缩文件

解压缩文件方法:FileUtil.java

package com.feike;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.log4j.Logger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
 * 解压缩文件Util
 * @author: Feike
 * @date: 2017年8月6日下午12:56:47
 */
public class FileUtil {
    private static Logger logger = Logger.getLogger(FileUtil.class);
   /**
    * 压缩文件
    * @author Feike
    * @date 2017年8月6日下午12:57:25 
    * @param zipFilePath 压缩以后的路径+文件名
    * @param srcPathName 要压缩的文件夹或文件路径
    */
    public static void zipFiles(String zipFilePath, String srcPathName) {
        File zipFile = new File(zipFilePath);
        File srcdir = new File(srcPathName);
        if (!srcdir.exists()){
            throw new RuntimeException(srcPathName + "路径不存在");
        }
        Project prj = new Project();
        FileSet fileSet = new FileSet();
        fileSet.setProject(prj);
        if(srcdir.isDirectory()) { //是目录
            fileSet.setDir(srcdir);
           //fileSet.setIncludes("*.csv"); //只压缩某种类型的文件或文件夹 eg:zip.setIncludes("*.java");
            //fileSet.setExcludes(...); //排出某种类型的文件或文件夹
        } else {
            fileSet.setFile(srcdir);
        }
        Zip zip = new Zip();
        zip.setProject(prj);
        zip.setDestFile(zipFile);
        zip.setEncoding("gbk"); //Windows的默认字符集编码为GBK
        zip.addFileset(fileSet);
        zip.execute();
        logger.debug("---compress files success---");
    }
    /**
     * 解压文件
     * @author Feike
     * @date 2017年8月6日下午1:01:08 
     * @param zipFilePath 要解压的文件路径+文件名
     * @param fileSavePath 解压后的路径
     * @param isDelete 是否删除源解压文件
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static void unZipFiles(String zipFilePath, String fileSavePath, boolean isDelete) throws Exception{
         try {
                File f = new File(zipFilePath);
                if ((!f.exists()) && (f.length() <= 0)) {
                    throw new RuntimeException("要解压的文件不存在!");
                }
                //一定要设置解压编码,否则有坑
                ZipFile zipFile = new ZipFile(f, "gbk");
                String strPath, gbkPath, strtemp;
                strPath = fileSavePath;
                Enumeration<ZipEntry> e = zipFile.getEntries();
                while (e.hasMoreElements()) {
                    org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();
                    gbkPath = zipEnt.getName();
                    strtemp = strPath + File.separator + gbkPath;
                    if (zipEnt.isDirectory()) { //目录
                        File dir = new File(strtemp);
                        if(!dir.exists()){
                            dir.mkdirs();
                        }
                        continue;
                    } else {
                        InputStream is = zipFile.getInputStream(zipEnt);
                        BufferedInputStream bis = new BufferedInputStream(is);

                        String strsubdir = gbkPath;
                        for (int i = 0; i < strsubdir.length(); i++) {
                            if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
                                String temp = strPath + File.separator
                                        + strsubdir.substring(0, i);
                                File subdir = new File(temp);
                                if (!subdir.exists())
                                    subdir.mkdir();
                            }
                        }
                        FileOutputStream fos = new FileOutputStream(strtemp);
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        int len;
                        byte[] buff = new byte[1024];
                        while ((len = bis.read(buff)) != -1) {
                            bos.write(buff, 0, len);
                        }
                        bos.close();
                        fos.close();
                    }
                }
                zipFile.close();
            } catch (Exception e) {
                logger.error("解压失败", e);
                throw e;
            }
         /**
          * 源文件有可能被别的进程占用,导致无法删除。
          */
         if(isDelete){
             boolean flag = new File(zipFilePath).delete();
             logger.debug("是否成功删除源文件: " + flag);
         }
         logger.debug("compress files success");
    }
    public static void main(String[] args) throws Exception {
        zipFiles("D:\\test.zip", "D:\\test");//压缩文件
//      unZipFiles("D:\\test.zip", "D:\\test", false);//解压文件不删除源文件
//      unZipFiles("D:\\test.zip", "D:\\test", true);//解压文件删除源文件
    }
}

log日志:log4j.properties

log4j.rootLogger=WARN,stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
#日志输出到指定文件
log4j.appender.R.File=D\:\\mylog.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# com.feike 包线的log日志级别是  DEBUG 
log4j.logger.com.feike=DEBUG

jar 包

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值