解压嵌套zip文件

解压zip文件时遇到嵌套的zip文件会继续解压。

以下代码是将压缩文件解压到本地磁盘,然后再遍历本地磁盘文件进行上传到云服务,上传完再进行删除本地磁盘文件。

压缩文件并保存在本地磁盘临时目录下:

/**
 * 解压文件
 * @param zipFile
 */
public void decompresComponentFile(ZipFile zipFile, String destDirPath) {
    try {
        File destDir = new File(destDirPath);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipEntry entry;
        String entryName;
        File entryFile, entryDir;
        byte[] buffer = new byte[1024];
        int bytesRead;
        for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
            entry = (ZipEntry) e.nextElement();
            entryName = entry.getName();
            entryFile = new File(destDir, entryName);
            if (entry.isDirectory()) {
                entryFile.mkdirs();
            } else {
                entryDir = new File(entryFile.getParent());
                if (!entryDir.exists()) {
                    entryDir.mkdirs();
                }
                try (InputStream inStream = zipFile.getInputStream(entry);
                     OutputStream outStream = new FileOutputStream(entryFile)){
                    while ((bytesRead = inStream.read(buffer)) != -1) {
                        outStream.write(buffer, 0, bytesRead);
                    }
                }
                if (entryName.endsWith(".zip")) {
                    decompresComponentFile(new ZipFile(entryFile.getAbsolutePath()), entryDir.getAbsolutePath());
                }
            }
        }
        zipFile.close();
    } catch (IOException ex) {
        try {
            zipFile.close();
            deleteFile(destDirPath);
        } catch (IOException e) {
            throw new ServiceException("解压文件失败", ex);
        }
        throw new ServiceException("解压文件失败", ex);
    }
}

遍历文件进行上传再删除:

/**
 * 上传并删除文件,只上传文件,不上传zip包
 * @param basePath
 * @return
 */
public List<String> uploadAndDelete(String basePath){
    List<String> downPathList = new ArrayList<>();
    try {
        Files.walkFileTree(Paths.get(basePath), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String filePath = file.toString().replace(basePath.replace("/",File.separator), "");
                if (!filePath.endsWith(".zip")) {
                    filePath = PREFIX_FOLDER.replace("/",File.separator) + filePath;
                    //上传文件
                    upLoadFile(Files.newInputStream(file), filePath, file.toFile().length());
                    //返回文件下载路劲
                    UriComponentsBuilder builder = UriComponentsBuilder.fromPath(downPath);
                    MediaTypeFactory.getMediaType(filePath)
                            .ifPresent(mediaType -> builder.queryParam("type", mediaType.toString()));
                    builder.queryParam("macroPath", FilenameUtils.separatorsToUnix(macroPath + filePath));
                    downPathList.add(builder.toUriString());
                }
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        deleteFile(basePath);
        throw new ServiceException(String.format("{%s}路劲下的文件上传失败;{%s}", TEMP_PATH, e.getMessage()));
    }
    return downPathList;
}

遍历删除文件:

private void deleteFile(String basePath){
        try {
            Files.walkFileTree(Paths.get(basePath), new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            throw new ServiceException("删除文件失败", e);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值