基于java原生api实现生成zip和解压zip

一、基于java原生api实现生成zip和解压zip

1、生成zip

使用到ZipOutputStream

    /**
     * 生成zip文件
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    private static void toZip() throws FileNotFoundException, IOException {
    	String filePath = "D:\\TODO";
    	try(ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filePath.concat(File.separator).concat("toZip.zip")))){
    		List<File> filesList = getAllFiles("D:\\GAME\\RES", new ArrayList<>());
    		filesList.stream().filter(f->!f.getName().contains(".zip")).forEach(f->{
    			try {
    				System.out.println(f.getName());
					zos.putNextEntry(new ZipEntry(f.getName()));
	    			zos.write(getFileDataAsByte(f));
	    			zos.closeEntry();
				} catch (IOException e) {
					e.printStackTrace();
				}
    		});
    	}
	}
    
    /**
     * file转byte
     * @param f
     * @return
     * @throws IOException 
     */
    private static byte[] getFileDataAsByte(File f) throws IOException {
    	InputStream is = new FileInputStream(f);
    	ByteArrayOutputStream bStream = new ByteArrayOutputStream(1024);
    	byte[] by = new byte[1024];
    	int len = -1;
    	while ((len=is.read(by))!=-1) {
			bStream.write(by, 0, len);
		}
		return bStream.toByteArray();
	}

    /**
     * 获取指定磁盘文件下的所有文件
     * @param filePath
     * @param filesList
     * @return
     */
	private static List<File> getAllFiles(String filePath,List<File> filesList) {
		File file = new File(filePath);
		File[] files=file.listFiles();
		for(File f:files) {
			if (f.isFile()) {
				filesList.add(f);
			}
			if (f.isDirectory()) {
				getAllFiles(f.getAbsolutePath(), filesList);
			}
		}
		return filesList;
    }

2、解压zip

使用到ZipOutputStream和ZipFile

/**
	 * 以zip文件名解压到对应文件夹下面
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	private static void fromZip() throws FileNotFoundException, IOException {
    	String zipPath = "D:\\TODO\\toZip.zip";
    	File file = new File(zipPath);
    	if(!file.exists()) {
    		return;
    	}
		try(	ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
				ZipFile zipFile = new ZipFile(file)){
			ZipEntry zipEntry = null;
			while ((zipEntry=zis.getNextEntry())!=null) {
				System.err.println(zipEntry.getName());
				if (!zipEntry.isDirectory()) {
					File outFile = new File(
							file.getParent()
							.concat(File.separator)
							.concat(file.getName().substring(0,file.getName().indexOf(".")))
							.concat(File.separator)
							.concat(zipEntry.getName())
							);
					if(!outFile.getParentFile().exists()) {
						outFile.getParentFile().mkdir();
					}
					if(!outFile.exists()) {
						outFile.createNewFile();
					}
					BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
					BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
					byte b[] =  new byte[1024];
					int len = -1;
					while ((len=bis.read(b))!=-1) {
						bos.write(b,0,len);
					}
				}
			}
		}
	}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IQ小王子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值