1234567891011121314151617181920212223242526272829303132333435 |
- package com.nokia.finance.tasks.utils;
- import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
- import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
- import java.io.IOException;
- import java.nio.file.FileVisitResult;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.SimpleFileVisitor;
- import java.nio.file.attribute.BasicFileAttributes;
- public class ZipUtil {
- /**
- * zip压缩文件夹不包括文件夹本身
- * @param inputDir 文件夹
- * @param outputFile 压缩文件
- */
- public static void zipDirNonSelf(Path inputDir, Path outputFile) throws IOException {
- try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(Files.newOutputStream(outputFile))) {
- os.setLevel(9);
- Files.walkFileTree(inputDir, new SimpleFileVisitor<>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- String relativePath = inputDir.relativize(file).toString();
- ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), relativePath);
- os.putArchiveEntry(entry);
- Files.copy(file, os);
- os.closeArchiveEntry();
- return FileVisitResult.CONTINUE;
- }
- });
- }
- }
- }
|