本偏主要使用java工具类对文件夹操作
压缩存在的文件夹
代码详情
/**
* 压缩文件夹及文件
*
* @param zipFilePath 压缩文件目录
* @param fileName 压缩的文件名
*/
public static void zipDir(String zipFilePath, String fileName) {
try (FileOutputStream fos = new FileOutputStream(fileName); ZipOutputStream zos = new ZipOutputStream(fos)) {
Files.walkFileTree(Paths.get(zipFilePath), new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
Objects.requireNonNull(dir);
Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// 压缩使用相对路径,getParent会把根路径压缩进来
zos.putNextEntry(new ZipEntry(Paths.get(zipFilePath).relativize(file.getParent()) + File.separator + file.toFile().getName()));
FileInputStream fis = new FileInputStream(file.toFile());
int len;
while ((len = fis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
fis.close();
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
Objects.requireNonNull(file);
throw exc;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
logger.error("zip file error:", e);
e.printStackTrace();
}
}
测试
@Test
public void zipDirTest() {
FileUtils.zipDir("D:/util", "D:/util.zip");
}
结果
基于内存压缩文件流
代码详情
/**
* 压缩流
*
* @param isMap 流与路径映射关系
* @param fileName 文件名
*/
public static void zipInputStream(Map<String, InputStream> isMap, String fileName) {
try (FileOutputStream fos = new FileOutputStream(fileName);
ZipOutputStream zos = new ZipOutputStream(fos)) {
for (Map.Entry<String, InputStream> map : isMap.entrySet()) {
zos.putNextEntry(new ZipEntry(map.getKey()));
InputStream is = map.getValue();
int len;
while ((len = is.read(buf)) != -1) {
zos.write(buf, 0, len);
}
is.close();
zos.closeEntry();
}
} catch (IOException e) {
logger.error("zip file error:", e);
e.printStackTrace();
}
}
代码示例
@Test
public void zipInputstreamTest() {
String a = "hello";
String b = "fdafwefafa";
Map<String, InputStream> map = new HashMap<>();
map.put("a.txt", IOUtils.toInputStream(a, Charset.defaultCharset()));
map.put("b.txt", IOUtils.toInputStream(b, Charset.defaultCharset()));
FileUtils.zipInputStream(map, "D:/test.zip");
}
结果
删除文件夹
/**
* 删除目录
*
* @param dir 目录
* @throws IOException 异常
*/
public static void deleteDir(String dir) throws IOException {
if (!Files.exists(Paths.get(dir))) {
return;
}
Files.walkFileTree(Paths.get(dir), new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Stream<Path> pathStream = Files.list(dir);
pathStream.forEach(path -> {
try {
if (Files.isDirectory(path) && Files.list(path).count() == 0) {
Files.deleteIfExists(path);
}
} catch (Exception e) {
logger.error("delete dir:{}", e.getMessage());
e.printStackTrace();
}
});
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (Files.exists(path) && !Files.isDirectory(path)) {
Files.deleteIfExists(Paths.get(path.getParent() + File.separator + path.getFileName()));
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (Files.list(dir).count() == 0) {
File file = new File(dir.toString());
file.deleteOnExit();
}
return FileVisitResult.CONTINUE;
}
});
}
具体代码