JDK实现压缩、解压

1.JDK实现压缩、解压

1.1 JDK实现压缩

/**
	*压缩成zip包
	* @author Benjamin
	* @date 
	* @version 1.0.0
	* @description 压缩成zip包
	* @param sourceFilePath 源文件路径
	* @param targetZipFilePath 压缩包的路径,需要指定到压缩包的名字和压缩包后缀
	* @return void
	*/
	public static void zipCompress(String sourceFilePath, String targetZipFilePath) throws IOException {
		File sourceFile = new File(sourceFilePath);
		List<File> compressFileList = new ArrayList<>();
		if (sourceFile.isDirectory()) {
			// 获取包里面的所有文件
			compressFileList.addAll(getFiles(sourceFilePath));
		} else {
			// 文件直接添加
			compressFileList.add(sourceFile);
		}
		try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetZipFilePath))){
			ZipEntry zipEntry = null;
			for (int i = 0; i < compressFileList.size(); i++) {
				File file = compressFileList.get(i);
				// 创建一个新的文件条目
				//zipEntry = new ZipEntry(file.getName()); // 文件名字
				//zipEntry = new ZipEntry(file.getAbsolutePath()); // 绝对路径

				/*if (sourceFile.isDirectory()) {
					String prefix = StringUtils.substringBeforeLast(sourceFile.getAbsolutePath(), "\\");
					prefix = prefix + "\\";
					// 相对路径
					String compressPath = StringUtils.substringAfterLast(file.getAbsolutePath(), prefix);
					// 创建一个新的文件条目
					zipEntry = new ZipEntry(compressPath);
				} else {
					zipEntry = new ZipEntry(file.getAbsolutePath());
				}*/

                String prefix = StringUtils.substringBeforeLast(sourceFile.getAbsolutePath(), "\\");
				prefix = prefix + "\\";
					// 相对路径
				String compressPath = StringUtils.substringAfterLast(file.getAbsolutePath(), prefix);
					// 创建一个新的文件条目
				zipEntry = new ZipEntry(compressPath);

				zipOutputStream.putNextEntry(zipEntry);
				// 将文件写入压缩包
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
				int length = 0;
				byte[] bytes = new byte[1024];
				while ((length = bis.read(bytes)) != -1) {
					zipOutputStream.write(bytes, 0, bytes.length);
				}
				bis.close();
				// 关闭当前zipEntry
				zipOutputStream.closeEntry();
			}
		} catch (IOException e) {
			throw new IOException(e);
		}
	}

	/**
	*获取文件的路径
	* @author Benjamin
	* @date 
	* @version 1.0.0
	* @description 获取文件的路径
	* @param filePath 文件路径
	* @return java.util.List<java.io.File>
	*/
	public static List<File> getFiles(String filePath) {
		List<File> fileList = new ArrayList<>();
		File file = new File(filePath);
		if (file.exists()) {
			File[] files = file.listFiles();
			for (File f : files) {
				if (f.isDirectory()) {
					fileList.addAll(getFiles(f.getAbsolutePath()));
				} else {
					// 添加文件的路径
					fileList.add(f);
				}
			}
		}
		return fileList;
	}

1.2 JDK实现解压

/**
	*解压zip包
	* @author Benjamin
	* @date 
	* @version 1.0.0
	* @description 解压zip格式压缩包
	* @param zipPath 需要解压的包路径,指定到包的名字和后缀
	* @param unZipPath  解压后的包路径
	* @return void
	*/
	public static void zipDecompress(String zipPath, String unZipPath) throws Exception {
		try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath))){
			ZipEntry zipEntry = null;
			while ((zipEntry = zipInputStream.getNextEntry()) != null) {
				// zipEntry.getName() 是相对路径
				File zipFile = new File(unZipPath, zipEntry.getName());
				if (zipEntry.isDirectory() && !zipFile.exists()) {
					zipFile.mkdirs();
				} else {
					if (!zipFile.getParentFile().exists()) {
						zipFile.getParentFile().mkdirs();
					}
					FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
					int len = 0;
					byte[] bytes = new byte[1024];
					while ((len = zipInputStream.read(bytes)) != -1) {
						fileOutputStream.write(bytes, 0, len);
					}
					fileOutputStream.close();
				}
				zipInputStream.closeEntry();
			}
		} catch (Exception e) {
			throw new Exception("解压zip包失败!");
		}
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值