Java解压zip压缩文件

该代码段展示了如何使用Java的ZipFile和InputStream等类来解压一个zip压缩文件到指定的目标路径。它遍历zip文件中的每个条目,如果是目录则创建,如果是文件则读取并写入目标文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java解压zip压缩文件

/**
	 * 方法描述:解压zip压缩文件
	 * @param file 文件
	 * @param targetPath 解压后的指定路径
	 * @author hjh
	 * @date 2023-05-15
	 */
	public static void decompress(File file, String targetPath){
		//创建ZipInputStream对象
		ZipFile zipFile;
		try {
			//FileInputStream 代表压缩文件的输入流;
			zipFile = new ZipFile(file.getAbsoluteFile());
			// 实例化对象,指明要进行解压的文件
			// 获取下一个ZipEntry,获取zipfile里面的每一个zipentry实例
			Enumeration<? extends ZipEntry> entries = zipFile.entries();
			while (entries.hasMoreElements()){
				ZipEntry  entry=entries.nextElement();
				File entryDestination = new File(targetPath,  entry.getName());
				if (entry.isDirectory()) {
					entryDestination.mkdirs();
				} else {
					entryDestination.getParentFile().mkdirs();
					InputStream in = zipFile.getInputStream(entry);
					OutputStream out = new FileOutputStream(entryDestination);
					IOUtils.copy(in, out);
					IOUtils.closeQuietly(in);
					out.close();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值