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 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());
				}

				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
	*/
	public static List getFiles(String filePath) {
		List 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包失败!");
		}
	}

你可能感兴趣的:(java)