java实现多层级zip解压的示例代码
作者:铁流是宝宝
这篇文章主要为大家详细介绍了java实现多层级zip解压的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
前言
项目中偶然需要,希望能处理嵌套的压缩包,但是又不希望把文件解压处理。原本不希望重复造轮子,但没有发现很好用的现成案例,就简单处理了一下。
正文
java做zip解压一般使用 ZipFile
或者 ZipInputStream
。
在实际使用中,遇到了zip清单属性无法读取的报错,最终采用了apache的ZipArchiveInputStream。主要是allowStoredEntriesWithDataDescriptor
属性。
代码完整使用的依赖如下:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.26</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency>
代码主要为符合业务需求而写,比较简陋。支持单次解压和递归解压,均通过回调返回缓冲流(无法关闭的缓冲流)。
必须要注意的是,一定不能提前关闭ZipArchiveInputStream,这个流一次会在getNextZipEntry后再次填充。
回调如果采用字节对内存的压力可能会比较大,所以通过缓冲流返回数据。为防止多人协作中出现误关闭流,使用不关闭源流的缓冲流工具。
如果有需要解压指定包,在入参加一个filter就可以实现。
完整代码实例
package xxx; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; /** 用于辅助的不关闭原流的缓冲流 */ public class NoCloseBufferStream extends BufferedInputStream { public NoCloseBufferStream(InputStream in) { super(in); } @Override public void close() throws IOException { //不实现任何东西就不会关闭原流 } }
package xxx; //your package import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; /** * 注意:初始的输入流是不会主动关闭的 * * @author 铁流 */ @Slf4j public class UnZipUtil { public static void main(String[] args) throws IOException { try (InputStream inputStream = Files.newInputStream(new File("/Users/tieliu/Desktop/test/aaaa.zip").toPath());) { loopUnzip(inputStream, (level, path, basePath, is) -> { is.close(); log.info(" level: {},path: {},basePath: {}", level, path, basePath); return true; }); } } /** * 递归解压zip,只能解压zip后缀名的压缩文件 * * @param inputStream 初始文件输入流 * @param loopCallBack 递归回调,返回值控制是否向下递归 * @throws IOException 文件流异常 */ public static void loopUnzip(InputStream inputStream, LoopCallBack loopCallBack) throws IOException { loopUnzip(inputStream, 0, "", loopCallBack); } private static void loopUnzip(InputStream inputStream, int level, String basePath, LoopCallBack loopCallBack) throws IOException { decompress(inputStream, (path, is) -> { // 此处决定是否继续向下 if (loopCallBack.call(level, path, basePath, is) && path.endsWith(".zip")) { loopUnzip(is, level + 1, basePath + "/" + path, loopCallBack); } }); } /** * 解压zip,必须是zip结尾的文件(错误属性的文件会被排除,因为不排除java也解压不了) * * @param inputStream 初始输入流 * @param callBack 回调 * @throws IOException io异常 */ public static void decompress(InputStream inputStream, CallBack callBack) throws IOException { try (NoCloseBufferStream bufferedInputStream = new NoCloseBufferStream(inputStream); ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(bufferedInputStream, CharsetUtil.defaultCharset().name(), true, true)) { decompress(zipInputStream, callBack); } } public static void decompress(byte[] bytes, CallBack callBack) throws IOException { try (ByteArrayInputStream inputStream = IoUtil.toStream(bytes);) { bytes = null; decompress(inputStream, callBack); } } private static void decompress(ZipArchiveInputStream inputStream, CallBack callBack) throws IOException { ZipArchiveEntry nextEntry = inputStream.getNextZipEntry(); while (nextEntry != null) { final String name = nextEntry.getName(); //过滤无用文件 if (!name.startsWith("__MACOSX") && !name.contains(".DS_Store") && !name.contains("Thumbs.db") && !name.startsWith("._")) { if (!nextEntry.isDirectory()) { callBack.call(name, new NoCloseBufferStream(inputStream)); } } nextEntry = inputStream.getNextZipEntry(); } } @FunctionalInterface public static interface CallBack { void call(String relativePath, InputStream is) throws IOException; } @FunctionalInterface public static interface LoopCallBack { boolean call(int level, String relativePath, String basePath, InputStream is) throws IOException; } }
到此这篇关于java实现多层级zip解压的示例代码的文章就介绍到这了,更多相关java zip解压内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!