当前位置: 代码迷 >> 综合 >> Java ZIP压缩
  详细解决方案

Java ZIP压缩

热度:79   发布时间:2023-10-28 09:05:05.0

目录

1.压缩

2.解压

3.嵌套压缩zip文件思路


1.压缩

package com.example.practice.srp;import lombok.extern.slf4j.Slf4j;import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** @author liuxiaobai*/
@Slf4j
public class Compress {private static final Integer ZIP_BUFFER_SIZE = 2048 * 10;public static void main(String[] args) {List<String> listPath = new ArrayList<>();listPath.add("/Users/lalala/Documents/梗咽啊我擦.gif");listPath.add("/Users/lalala/Documents/梗咽啊我擦 2.gif");listPath.add("/Users/lalala/Documents/梗咽啊我擦 3.gif");java.io.File zipFile = new java.io.File("/Users/lalala/Documents/test1.zip");boolean status = zip(zipFile, listPath);log.info("压缩结果:{}", status ? "成功" : "失败");}/*** @param zipFile  压缩包文件对象* @param listPath 被压缩对象的绝对路径*/public static boolean zip(File zipFile, List<String> listPath) {ZipOutputStream zipStream = null;FileInputStream zipSource = null;BufferedInputStream bufferStream = null;byte[] buffer = new byte[ZIP_BUFFER_SIZE];int read;try {long startTime = System.currentTimeMillis();zipStream = new ZipOutputStream(new FileOutputStream(zipFile));for (String addr : listPath) {File file = new File(addr);if (file.isFile() && file.exists()) {zipSource = new FileInputStream(file);ZipEntry zipEntry = new ZipEntry(file.getName());zipStream.putNextEntry(zipEntry);bufferStream = new BufferedInputStream(zipSource, ZIP_BUFFER_SIZE);while ((read = bufferStream.read(buffer, 0, ZIP_BUFFER_SIZE)) != -1) {zipStream.write(buffer, 0, read);log.info("写入bugger中:{}",System.currentTimeMillis());}} else {log.info("文件不存在!");return false;}}long endTime = System.currentTimeMillis();log.info("压缩耗时:{}", endTime - startTime);} catch (Exception e) {throw new RuntimeException("zip error", e);} finally {if(Objects.nonNull(bufferStream)){try {bufferStream.close();}catch (IOException e) {e.printStackTrace();}}if(Objects.nonNull(zipStream)){try {zipStream.close();}catch (IOException e) {e.printStackTrace();}}if(Objects.nonNull(zipSource)){try {zipSource.close();}catch (IOException e) {e.printStackTrace();}}}return true;}
}

2.解压

package com.example.practice.srp;import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;/*** @author liuxiaobai*/
@Slf4j
public class Decompression {private static final Integer BUFFER_SIZE = 1024 * 10;public static void main(String[] args) {File file = new File("/Users/lalala/Documents/test1.zip");boolean status = unZip(file, "/Users/lalala/Documents");log.info("解压结果:{}", status ? "成功" : "失败");}/*** @param srcFile     zip源文件* @param destDirPath 解压后的目标文件夹* @throws RuntimeException 运行时异常*/public static boolean unZip(File srcFile, String destDirPath) throws RuntimeException {long startTime = System.currentTimeMillis();if (!srcFile.exists()) {throw new RuntimeException(srcFile.getPath() + " 路径下文件不存在");}ZipFile zipFile = null;InputStream unZipStream = null;FileOutputStream unZipFileStream = null;try {zipFile = new ZipFile(srcFile);Enumeration<?> entries = zipFile.entries();int len;while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();if (entry.isDirectory()) {String dirPath = destDirPath + "/" + entry.getName();File dir = new File(dirPath);boolean status = dir.mkdirs();log.info("创建文件夹结果结果:{}", status ? "成功" : "失败");} else {File file = new File(destDirPath + "/" + entry.getName());unZipStream = zipFile.getInputStream(entry);unZipFileStream = new FileOutputStream(file);byte[] buf = new byte[BUFFER_SIZE];while ((len = unZipStream.read(buf)) != -1) {unZipFileStream.write(buf, 0, len);}}}long endTime = System.currentTimeMillis();log.info("解压耗时:{}", endTime - startTime);return true;} catch (Exception e) {throw new RuntimeException("unzip error", e);} finally {if (Objects.nonNull(unZipFileStream)) {try {unZipFileStream.close();} catch (IOException e) {e.printStackTrace();}}if (Objects.nonNull(unZipStream)) {try {unZipStream.close();} catch (IOException e) {e.printStackTrace();}}if (Objects.nonNull(zipFile)) {try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}}}

3.嵌套压缩zip文件思路

        嵌套压缩:先找一个工作目录生成zip1、zip2,文件生成完毕后再次读取该目录下zip1、zip2文件进行压缩,实现嵌套压缩(曲线救国,哈哈)