ZIP/RAR解压缩(JAVA)

ZIP/RAR解压缩工具类方法,以此作为自己的第一篇博客,也算作为一个记录吧。

/**
 * 上传打包文件(支持rar和zip),解压到服务器/本地
 * @param folderPath 路径,以“/”结尾
 * @param file 文件
 * @param fileType 文件类型(rar/zip)
 * @return 
 * @throws Exception
 */
public static String putPackObject(String folderPath,File file,String fileType) throws Exception{
    if(folderPath==null || file==null || fileType==null) return null;

    //文件夹名(返回目录参数)
    String destDirName =  folderPath+file.getName().substring(0,file.getName().lastIndexOf("."))+"/";
    try {
        //解压文件
        if("rar".equals(fileType.toLowerCase())){
            Archive a = null;
            FileOutputStream fos = null;
            try {
                a = new Archive(file);
                FileHeader fh = a.nextFileHeader();
                while (fh != null) {                    
                    if (!fh.isDirectory()) {//文件
                        String fileName = null;
                        if(fh.isUnicode()){//解決中文乱码  
                            fileName = fh.getFileNameW().trim().replace("\\", "/");
                        }else{  
                            fileName = fh.getFileNameString().trim().replace("\\", "/");  
                        }
                        //文件类型验证
                        if(fileName.toLowerCase().endsWith(".rar") || fileName.toLowerCase().endsWith(".zip")){                         
                            throw new Exception("rar file error");
                        }                   
                        //本地文件路径
                        String destFilePath = destDirName+fileName;
                        //创建本地文件
                        File f = new File(destFilePath.replace("/", File.separator));
                        if(!f.exists() && !f.isDirectory()) {  
                            //判断目标文件所在的目录是否存在  
                            if(!f.getParentFile().exists()) {  
                                //如果目标文件所在的目录不存在,则创建父目录  
                                if(!f.getParentFile().mkdirs()) throw new Exception("create folder error");
                            }
                        }else{
                            fh = a.nextFileHeader();
                            continue;
                        }
                        fos = new FileOutputStream(f);
                        a.extractFile(fh, fos);
                        fos.close();
                        fos = null;
                    }
                    fh = a.nextFileHeader();
                }
                a.close();
                a = null;
            } catch (Exception e) {
                throw e;
            } finally {
                if (a!=null) {
                    a.close();
                    a = null;
                }
            }
        }else if("zip".equals(fileType.toLowerCase())){
            ZipFile readfile = null;
            try {
                //文件
                readfile = new ZipFile(file);
                Enumeration<ZipEntry> takeentrie = readfile.getEntries();
                ZipEntry zipEntry = null;
                while (takeentrie.hasMoreElements()){
                    zipEntry = (ZipEntry) takeentrie.nextElement(); 
                    InputStream in = null;
                    FileOutputStream out = null;
                    try {                           
                        if(!zipEntry.isDirectory()){//文件
                            String filePath = zipEntry.getName().replace(File.separator, "/");
                            //文件类型验证
                            if(filePath.toLowerCase().endsWith(".zip") || filePath.toLowerCase().endsWith(".rar")){                             
                                throw new Exception("zip file error");
                            }                               
                            //本地文件路径
                            String destFilePath = destDirName + filePath;
                            //创建本地文件
                            File unpackfile = new File(destFilePath.replace("/", File.separator));
                            if(!unpackfile.exists() && !unpackfile.isDirectory()) {  
                                //判断目标文件所在的目录是否存在  
                                if(!unpackfile.getParentFile().exists()) {  
                                    //如果目标文件所在的目录不存在,则创建父目录  
                                    if(!unpackfile.getParentFile().mkdirs()) throw new Exception("create folder error");
                                }  
                            }else{
                                continue;
                            }
                            in = readfile.getInputStream(zipEntry);
                            out = new FileOutputStream(unpackfile);
                            int c;
                            byte[] by = new byte[1024];
                            while ((c = in.read(by)) != -1) {
                                out.write(by, 0, c);
                            }
                            out.flush();                                
                        }
                    } catch (IOException ex) {
                        throw new IOException("extract file error:" + ex.toString());
                    } finally {
                        if (in!=null) {
                            in.close();
                            in=null;
                        }
                        if (out!=null) {
                            out.close();
                            out=null;
                        }
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                throw new IOException("extract file error:" + ex.toString());
            } finally {
                if (readfile != null) {
                    readfile.close();
                }
            }
        }else{
            throw new Exception("file type error");
        }
    }catch (Exception e) {
        throw new Exception(e.getMessage());
    }
    return destDirName;
}

注:该方法只是简单的解压缩文件,权当做参考,具体功能代码还需根据业务需求来写。
相关JAR包
1、ant-1.9.7.jar(解压zip)
2、junrar-0.7.jar(解压rar)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值