码迷,mamicode.com
首页 > 编程语言 > 详细

java 小工具之--zip包解压,删除目录及文件、

时间:2018-01-11 16:03:40      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:except   setfile   myeclipse   oid   throws   获得   gpo   rac   direct   

 

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

import java.io.File;


public class Zipper {
    public final static String encoding = "GBK";

    // 压缩
    public static void zip(String srcPathname, String zipFilepath)
            throws BuildException, RuntimeException {
        File file = new File(srcPathname);
        if (!file.exists())
            throw new RuntimeException("source file or directory "
                    + srcPathname + " does not exist.");

        Project proj = new Project();
        FileSet fileSet = new FileSet();
        fileSet.setProject(proj);
        // 判断是目录还是文件
        if (file.isDirectory()) {
            fileSet.setDir(file);
            // ant中include/exclude规则在此都可以使用
            // 比如:
            // fileSet.setExcludes("**/*.txt");
            // fileSet.setIncludes("**/*.xls");
        } else {
            fileSet.setFile(file);
        }

        Zip zip = new Zip();
        zip.setProject(proj);
        zip.setDestFile(new File(zipFilepath));
        zip.addFileset(fileSet);
        zip.setEncoding(encoding);
        zip.execute();
    }

    // 解压缩
    public static void unzip(String zipFilepath(//目标文件), String destDir(//目标路径))
            throws BuildException, RuntimeException {
        if (!new File(zipFilepath).exists())
            throw new RuntimeException("zip file " + zipFilepath
                    + " does not exist.");

        Project proj = new Project();
        Expand expand = new Expand();
        expand.setProject(proj);
        expand.setTaskType("unzip");
        expand.setTaskName("unzip");
        expand.setEncoding(encoding);

        expand.setSrc(new File(zipFilepath));
        expand.setDest(new File(destDir));
        expand.execute();
    }



    // 创建目录
    public  boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {// 判断目录是否存在
            System.out.println("创建目录失败,目标目录已存在!");
            boolean result = deleteDirectory(destDirName);
            if(result){
                dir.mkdirs();
                return true;
            }
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {// 结尾是否以"/"结束
            destDirName = destDirName + File.separator;
        }
        if (dir.mkdirs()) {// 创建目标目录
            System.out.println("创建目录成功!" + destDirName);
            return true;
        } else {
            System.out.println("创建目录失败!");
            return false;
        }
    }



    public boolean deleteDirectory(String dirPath) {// 删除目录(文件夹)以及目录下的文件
        boolean flag = false;
        // 如果sPath不以文件分隔符结尾,自动添加文件分隔符
        if (!dirPath.endsWith(File.separator)) {
            dirPath = dirPath + File.separator;
        }
        File dirFile = new File(dirPath);
        // 如果dir对应的文件不存在,或者不是一个目录,则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        flag = true;
        File[] files = dirFile.listFiles();// 获得传入路径下的所有文件
        for (int i = 0; i < files.length; i++) {// 循环遍历删除文件夹下的所有文件(包括子目录)
            if (files[i].isFile()) {// 删除子文件
                flag = deleteFile(files[i].getAbsolutePath());
                System.out.println(files[i].getAbsolutePath() + " 删除成功");
                if (!flag)
                    break;// 如果删除失败,则跳出
            } else {// 运用递归,删除子目录
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag)
                    break;// 如果删除失败,则跳出
            }
        }
        if (!flag)
            return false;
        if (dirFile.delete()) {// 删除当前目录
            return true;
        } else {
            return false;
        }
    }


    public boolean deleteFile(String filePath) {// 删除单个文件
        boolean flag = false;
        File file;
        flag = false;
        file = new File(filePath);
        if (file.isFile() && file.exists()) {// 路径为文件且不为空则进行删除
            file.delete();// 文件删除
            flag = true;
        }
        return flag;
    }

/*    public static void main(String[] args) {

        // String dir = new String("F:\\我的备份\\文档\\MyEclipse+9.0正式版破解与激活(亲测可用)");
        //  dir = new String("F:/111.JPG");
        //  zip(dir, "f:/BZBXB/zipant.zip");
        unzip("f:/Desktop.zip", "f:/test/");
    }*/

 

 

 




}

 

 

 

 

 

 

 

 

 

 

 

//解析里面的文件

  //解析Excel
    public  List<MpmsFiles> parseMpFiles(String sb){
        try {
            List<String[]> list=null;
            CSVReader reader = new CSVReader( new InputStreamReader(new FileInputStream(sb), "GBK"));
            list= reader.readAll();
            List<MpmsFiles> list5 = new ArrayList<MpmsFiles>();
            if(list!=null&&list.size()>0){
                for(int j=0;j<list.size();j++){
                    MpmsFiles e = new MpmsFiles();
                    int  length =list.get(j).length;
                    if( length==1){
                        if(!StringUtils.isEmpty(list.get(j)[0])){
                            return null;
                        }
                        else{
                            return null;//空文件
                        }
                    }
                    else if(length==2){
                        if(StringUtils.isEmpty(list.get(j)[0])){
                            return null;
                        }
                        if(StringUtils.isEmpty(list.get(j)[1])){
                            return null;
                        }
                        else{
                            return null;
                        }
                    }
                    else if(length==3){
                        if(StringUtils.isEmpty(list.get(j)[0])){
                            return null;
                        }
                        if(StringUtils.isEmpty(list.get(j)[1])||StringUtils.isEmpty(list.get(j)[2])){
                            return null;
                        }
                        e.setQuestion(list.get(j)[0]);
                        e.setAnswer(list.get(j)[1]);
                        e.setKeyword(list.get(j)[2]);
                    }
                    else {
                        return null;
                    }
                   list5.add(e);
                }
                reader.close();
                return  list5;
            }
            else{
                return  null;
            }
        }catch (Exception e) {
        
            e.printStackTrace();
 
        }
    }

java 小工具之--zip包解压,删除目录及文件、

标签:except   setfile   myeclipse   oid   throws   获得   gpo   rac   direct   

原文地址:https://www.cnblogs.com/dw89/p/8267420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!