把多个file文件导出zip(怎样快速将多个文件夹里的文件导出来)

小编:迷魂冰 更新时间:2022-10-11

最近项目后台需要给业务添加二维码图片并且要导出成包提供下载,希望这个方法能帮助到大家

注意代码中有写类没贴出来,大家看方法名称应该能猜出来

public void exportZipFile (List fileList, String zipName) {

// File target = new File(StorageTypes.IMAGE.getPath()+"/"+zipName);

File target = new File(StorageTypes.ROOT.getPath()+"/"+zipName);

try (ArchiveOutputStream o = new ZipArchiveOutputStream(target)) {

for (File f : fileList) {

ArchiveEntry entry = o.createArchiveEntry(f, f.getName());

o.putArchiveEntry(entry);

if (f.isFile()) {

try (InputStream i = Files.newInputStream(f.toPath())) {

IOUtils.copy(i, o);

}

}

o.closeArchiveEntry();

}

} catch (IOException e) {

e.printStackTrace();

}

}