RAR文件解析

引入jar包     junrar-2.0.0.jar

http://mvnrepository.com/artifact/com.github.junrar/junrar

问题:

        junrar只支持PC上WinRAR5.0以下版本压缩生成的文件,WinRAR5.0压缩出来的文件格式其实是rar5格式,是解压不出来的。WinRAR5.0以下版本压缩生成的文件是rar4,可以解压出来。

            MultipartFile multipartFile = request.getFile(it);
            String extension = multipartFile.contentType;
            InputStream is = multipartFile.getInputStream();
            InputStream stream = new BufferedInputStream(is);
            Archive archive = new Archive(stream);
            for(FileHeader fh : archive.getFileHeaders()){
					String fileName=  fh.getFileNameW().trim();
					if(!existZH(fileName)){
						fileName = fh.getFileNameString().trim();
					}
					println fileName;
					
					File dir = null
					File out = null
					FileOutputStream os = null
					String path = (fileName).replaceAll("\\\\", "/");
					String[] arr = path.split("/");
					int length = arr.length;
					String dirPath = "";
					int end = path.lastIndexOf("/");
					if (end != -1) {
						dirPath = path.substring(0, end);
					}
					try {
						dir = new File(dirPath);
						if (!dir.exists()) {
//							dir.mkdirs();
						}
					} catch (RuntimeException e1) {
						e1.printStackTrace();
					} finally {
						if (dir != null) {
							dir = null;
						}
					}
					if(arr.length>2&&arr[length-2].lastIndexOf("_")>0&&!dataMap.containsKey(arr[length-2])){
						dataMap.put(arr[length-2],new ArrayList<String>());
					}
					
					System.err.println("file - " + fh.getFileNameW() + " : "
							+ fh.getDataSize() + " bytes");
					long sizes = fh.getDataSize();
					if (sizes > 0) {
						if(arr[length-1].lastIndexOf("_")<0&&arr[length-2].lastIndexOf("_")<0){
							InputStream dirStream = archive.getInputStream(fh)
							Workbook workbook = WorkbookFactory.create(dirStream);
							//具体业务数据
                            ...
						}else if(arr[length-2].lastIndexOf("_")>0){
							InputStream dirStream = archive.getInputStream(fh);
							byte[] bytes = dirStream.getBytes();
							//具体业务数据
						}
						
					}
						
					fh = archive.nextFileHeader();
//					out = new File(fileName);
					try {
//						os = new FileOutputStream(out);
//						archive.extractFile(fh, os);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}finally {
						if (os != null) {
							try {
								os.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						if (out != null) {
							out = null;
						}
					}
				}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值