Java zip,rar解压中文文件,以及包含在文件夹中的中文文件

首先我们要引用ant.jar 因为jdk本身的zip处理的方法不支持中文 所以我们要引用org.apache.tools.zip.ZipEntry和org.apache.tools.zip.ZipFile这两个类 而这两个类都包含在ant.jar之中,然后代码来了

/**
     * 解析附件zip包
     *
     * @param unZipFileName   解压的zip文件
     * @param outputDirectory 输出目录
     */
    public static void unZip(String unZipFileName, String outputDirectory) {
        FileOutputStream fileOut = null;
        InputStream inputStream = null;
        ZipFile zipFile = null;
        File file = null;
        int readedBytes;
        try {

            //创建输出目录
            File outputDirFile = new File(outputDirectory);
            if (!outputDirFile.exists()) {
                outputDirFile.mkdir();
            }

            //这里需要考虑压缩工具是什么,如果是好压的话就需要把它改为UTF-8的编码,如果是WinRAR的话就是GBK编码
            // 然后具体如何判断压缩包是什么压缩的需要参考 http://blog.csdn.net/songylwq/article/details/6139753 这篇文章
            zipFile = new ZipFile(unZipFileName, "UTF-8");
            //不断遍历zip中的文件
            for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String filename = entry.getName();
                file = new File(outputDirectory + "/" + filename);
                //如果是文件夹先创建
                if (entry.isDirectory()) {
                    file.mkdirs();
                    continue;
                } else { //如果是文件,先查看文件目录是否存在,不存在就创建他
                    File parent = file.getParentFile();
                    if (parent != null && !parent.exists()) {
                        parent.mkdirs();
                    }
                }
                file.createNewFile(); //创建文件
                // 输出文件
                inputStream = zipFile.getInputStream(entry);
                fileOut = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                while ((readedBytes = inputStream.read(buf)) > 0) {
                    fileOut.write(buf, 0, readedBytes);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭文件流
            try {
                if (fileOut != null) {
                    fileOut.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

解析rar需要引入另一个jar包junrar

/**
     * 解析附件rar包
     *
     * @param unRarFileName   解压的zip文件
     * @param outputDirectory 输出目录
     * @return result 成功与否
     */
    public static boolean unRar(String  unRarFileName, String outputDirectory) {
        //返回结果
        Boolean result;
        //生成解压文件
        Archive a = null;
        FileOutputStream fos = null;
        try{
            a = new Archive(new File(unRarFileName));
            FileHeader fh = a.nextFileHeader();
            while(fh!=null){
                if(!fh.isDirectory()){
                    //当前文件的相对路径
                    String compressFileName = fh.getFileNameW().isEmpty()?fh.getFileNameString():fh.getFileNameW();
                    String destFileName = "";
                    //非windows系统 很重要
                    if(File.separator.equals("/")){
                        //当前文件的绝对路径
                        destFileName = outputDirectory + File.separator + compressFileName.replaceAll("\\\\", "/");
                        //windows系统
                    }else{
                        destFileName = outputDirectory + File.separator + compressFileName.replaceAll("/", "\\\\");
                    }
                    //取得母文件夹,如果不存在就创建文件夹
                    File file = new File(destFileName);
                    File parent = file.getParentFile();
                    if (parent != null && !parent.exists()) {
                        parent.mkdirs();
                    }
                    //解压缩文件
                    fos = new FileOutputStream(new File(destFileName));
                    a.extractFile(fh, fos);
                }
                fh = a.nextFileHeader();
            }
            result = true;
        }catch(Exception e){
            e.printStackTrace();
            result = false;
        }finally{
            if(fos!=null){
                try{fos.close();}catch(Exception e){e.printStackTrace();}
            }
            if(a!=null){
                try{a.close();}catch(Exception e){e.printStackTrace();}
            }
        }
        return result;
    }


感觉里面的注释以及比较完善了 如果有什么需要解释的 可以问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值