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

来源:互联网 发布:snh48知乎娱乐圈地位 编辑:程序博客网 时间:2024/05/22 10:36

用于解压,压缩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, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender#日志输出到指定文件log4j.appender.R.File=D\:\\mylog.loglog4j.appender.R.MaxFileSize=100KBlog4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n# com.feike 包线的log日志级别是  DEBUG log4j.logger.com.feike=DEBUG

jar 包

这里写图片描述

原创粉丝点击