java8结合lambda表达式解析zip包获取zip包中的条目以及将文件压缩成zip包

  1. java解析zip文件获取所需的文件
        //最低jdk1.8
        ZipFile zfile=null;
		String path="D:\\test\\ceshicopy.zip";
		File file1 = new File(path);
		try {
            zfile=new ZipFile(file1);
            //获取流文件
			Stream<? extends ZipEntry> stream = zfile.stream();
             //断言函数即条件返回boolean类型的结果,如我想获取zip包中的某个文件,由于我的zip包中只有一个xml文件
             //所以正则写法如下。Predicate函数式接口,具体查看jdk文档。
			Predicate<? super ZipEntry> xml=ze->ze.getName().matches(".*\\.xml");
            //Optional容器对象 ,结合Predicate获取到zip包中你所需要的文件,isPresent()判断是否存在对象,通过get()获取对象
            //过滤出你需要的文件,接下来具体操作根据需求
			Optional<? extends ZipEntry> first = stream.filter(xml).findFirst();
			if(first.isPresent()){
				ZipEntry fe = (ZipEntry) first.get();
				InputStream inputStream = zfile.getInputStream(fe);
				InputStreamReader ir = new InputStreamReader(inputStream);
				StringBuffer bf=new StringBuffer();
				BufferedReader	br = new BufferedReader(ir);
				String temp=null;
				while ((temp=br.readLine())!=null){
				bf.append(temp+"\n");
				}
				inputStream.close();
				ir.close();
				br.close();
				System.out.println(bf.toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			zfile.close();
			file1.delete();
		}
  1. java压缩zip文件
如下图所示压缩该目录下的所有文件,且按照该目录下的目录结构压缩成zip包

在这里插入图片描述

public void toZipCompress( ) throws IOException {
        //获取需要压缩的文件目录,如d盘下的test目录
		File file1=new File("D:\\test");
		//File file2=new File("D:\\test2");
		//File file3=new File("D:\\test3");
		//map集合key为压缩包中的目录名称,value为文件或文件目录,最终将该文件或目录放在名称为key的目录下
        Map<String,File> map=new HashMap<>();
        map.put("yasuo",file1);
        //多个目录或多个文件压缩则可以如下
        // map.put("yasuo2",file2);
        // map.put("yasuo3",file3);
        //zip输出流
		ZipOutputStream out=new ZipOutputStream(new FileOutputStream("D:\\yasuo.zip"));
		toZip(map,out);
       //全部压缩完毕后关闭zip流,如果在toZip中关闭,由于该方法中有递归会导致文件未压缩完毕而流关闭,使得压缩失败
		try {
			out.closeEntry();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void  toZip(Map<String,File> files,ZipOutputStream out){
	 //支持多个文件或文件目录压缩,本例子只压缩一个文件目录
    	files.forEach((x,y)->{
    	  //如果是文件直接压缩
    		if(y.isFile()){
    			ZipEntry entry=new ZipEntry(x+"/"+y.getName());
				try {
					out.putNextEntry(entry);
					FileInputStream  in=new FileInputStream(y);
					byte[] buf=new byte[1024];
					int temp;
					while ((temp = in.read(buf)) != -1){
						out.write(buf,0,temp);
					}

					in.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			//如果是文件夹,获取文件夹下的文件或目录保持原有结构压缩
    		if(y.isDirectory()){
				File[] files1 = y.listFiles();
				//如果文件夹内没有文件则保留目录结构压缩
				if(files1.length==0){
					ZipEntry entry=new ZipEntry(x+File.separator+y.getName()+File.separator);
					try {
						out.putNextEntry(entry);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}else{
				 //如果存在文件,则判断是文件夹还是目录,如果是目录则继续递归查找直至不存在文件夹或目录
					for (File f:files1){
						if(f.isFile()){
							ZipEntry entry=new ZipEntry(x+File.separator+y.getName()+File.separator+f.getName());
							try {
								out.putNextEntry(entry);
								FileInputStream  in=new FileInputStream(f);
								byte[] buf=new byte[1024];
								int temp;
								while ((temp = in.read(buf)) != -1){
									out.write(buf,0,temp);
								}

								in.close();

							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						//文件夹则递归处理
						if(f.isDirectory()){
							Map<String,File> map=new HashMap<>();
							//需要保持原目录结构,所有map集合put如下
							map.put(x+File.separator+y.getName(),f);
							//递归
							toZip(map,out);
						}
					}
				}

			}

		});
	}

压缩包结构(yasuo.zip[yasuo/test/原结构]) 如下图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值