java解压zip和rar

 解压zip类型压缩包

	/**
	 * 解压zip到指定目录
	 * @author hsj
	 * @param zipFile
	 * @param targetFilePath
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes" })
	public static void unZipFiles(File zipFile, String targetFilePath) {
		File pathFile = new File(targetFilePath);
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		ZipFile zip=null;
		try {
			//中文目录处理
			zip = new ZipFile(zipFile,Charset.forName("GBK"));
		} catch (ZipException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		for(Enumeration entries = zip.entries();entries.hasMoreElements();) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			String zipEntryName = entry.getName();
			String filePath=targetFilePath+File.separator+zipEntryName;
			File file = new File(filePath);
			//是文件夹则创建文件夹
			if(entry.isDirectory()){
				if (!file.exists()) {
					file.mkdirs();
				}
				continue;
			}else{
				InputStream in=null;
				try {
					in = zip.getInputStream(entry);
                    //写入本地
					writeFile(filePath, in);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if(in!=null){
						try {
							in.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

解压rar类型压缩包(rar分两种情况,rar5版本的解压未找到可使用类库,所以使用命令)

rar5以下用代码的话需要引入依赖

        <!-- 解压rar start-->
        <dependency>
            <groupId>com.github.junrar</groupId>
            <artifactId>junrar</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- 解压rar end -->
	/**
	 *  解压rar到指定目录
	 * @author hsj
	 * @param rarFile
	 * @param targetFilePath
	 * @throws Exception
	 */
	public static void unRarFiles(File rarFile, String targetFilePath){
        File pathFile = new File(targetFilePath);
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		Archive archive=null;
		try {
			archive = new Archive(new FileInputStream(rarFile));
			FileHeader fileHeader = archive.nextFileHeader();
			if(fileHeader!=null){
				while (fileHeader != null) {
		            if (fileHeader.isDirectory()) {
		                fileHeader = archive.nextFileHeader();
		                continue;
		            }
		            String tempFilePath=fileHeader.getFileNameW();
		            if(ToolUtil.isEmpty(tempFilePath)){
		            	tempFilePath=fileHeader.getFileNameString();
		            }
		            File out = new File(targetFilePath+File.separator+tempFilePath);
		            if (!out.exists()) {
		                if (!out.getParentFile().exists()) {
		                    out.getParentFile().mkdirs();
		                }
		                out.createNewFile();
		            }
		            FileOutputStream os = new FileOutputStream(out);
		            archive.extractFile(fileHeader, os);
		            os.close();
		            fileHeader = archive.nextFileHeader();
		        }
			}else{
				boolean result=unRarByCommand(rarFile, targetFilePath);
				log.error("解压rar文件失败,尝试命令形式解压"+result);
			}
	        
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(archive!=null){
				try {
					archive.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

    private static String RAR_PATH_WIN="\"C:\\Program Files\\WinRAR\\UnRAR.exe\"";
    private static String RAR_PATH_LINUX="rar ";
    /**
	 *  解压rar到指定目录 使用命令
	 * @author hsj
	 * @param rarFile
	 * @param targetFilePath
	 * @throws Exception
	 */
	public static boolean unRarByCommand(File rarFile, String target) {     
        boolean bool = false;     
        if(!rarFile.exists()){  
            return false;  
        }
        String rootPath="";
        if(OsUtil.IS_WINDOWS){
        	rootPath=RAR_PATH_WIN;
        }else if(OsUtil.IS_LINUX){
        	rootPath=RAR_PATH_LINUX;
        }
        String cmd = rootPath + " X " + rarFile.getAbsolutePath() + " "+target;    
        try {     
        	System.out.println(cmd);
            Process proc = Runtime.getRuntime().exec(cmd);     
            if (proc.waitFor() != 0) {     
                if (proc.exitValue() == 0) {     
                    bool = false;     
                }     
            } else {     
                bool = true;     
            } 
            proc.destroy();
        } catch (Exception e) {     
            e.printStackTrace();     
        }     
        return bool;     
    }

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值