• 原生ZipOutputStream
      1. public void zip(File fileToZip, String relativePath, ZipOutputStream zipOut) throws IOException {
      2. if (fileToZip.isHidden()) {
      3. return;
      4. }
      5. if (fileToZip.isDirectory()) {
      6. if (relativePath.endsWith("/")) {
      7. zipOut.putNextEntry(new ZipEntry(relativePath));
      8. zipOut.closeEntry();
      9. } else {
      10. zipOut.putNextEntry(new ZipEntry(relativePath + "/"));
      11. zipOut.closeEntry();
      12. }
      13. File[] children = fileToZip.listFiles();
      14. if (children != null) {
      15. for (File childFile : children) {
      16. zip(childFile, relativePath + "/" + childFile.getName(), zipOut);
      17. }
      18. }
      19. return;
      20. }
      21. FileInputStream fis = new FileInputStream(fileToZip);
      22. ZipEntry zipEntry = new ZipEntry(relativePath);
      23. zipOut.putNextEntry(zipEntry);
      24. byte[] bytes = new byte[1024];
      25. int length;
      26. while ((length = fis.read(bytes)) >= 0) {
      27. zipOut.write(bytes, 0, length);
      28. }
      29. fis.close();
      30. }
    • commons-compress
      1. <dependency>
      2. <groupId>org.apache.commons</groupId>
      3. <artifactId>commons-compress</artifactId>
      4. <version>1.20</version>
      5. </dependency>
    1. private void zip(File source, File destination) throws IOException, ArchiveException {
    2. OutputStream archiveStream = new FileOutputStream(destination);
    3. ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
    4. Collection<File> fileList = FileUtils.listFiles(source, null, true);
    5. for (File file : fileList) {
    6. String entryName = getEntryName(source, file);
    7. ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
    8. archive.putArchiveEntry(entry);
    9. BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
    10. IOUtils.copy(input, archive);
    11. input.close();
    12. archive.closeArchiveEntry();
    13. }
    14. archive.finish();
    15. archiveStream.close();
    16. }
    17. private String getEntryName(File source, File file) throws IOException {
    18. int index = source.getAbsolutePath().length() + 1;
    19. String path = file.getCanonicalPath();
    20. return path.substring(index);
    21. }
    • zip4j
      使用maven:
      1. <!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
      2. <dependency>
      3. <groupId>net.lingala.zip4j</groupId>
      4. <artifactId>zip4j</artifactId>
      5. <version>2.6.1</version>
      6. </dependency>


    代码:

    1. <!-- 压缩文件,并下载 -->
    2. public void zip(File fileToZip, File zip, HttpServletResponse response) throws ZipException {
    3. ZipFile zipFile = new ZipFile(zip);
    4. ZipParameters parameters = new ZipParameters();
    5. // 压缩方式
    6. parameters.setCompressionMethod(CompressionMethod.DEFLATE);
    7. // 压缩级别
    8. parameters.setCompressionLevel(CompressionLevel.FAST);
    9. // 要打包的文件夹
    10. File[] fs = fileToZip.listFiles();
    11. // 遍历test文件夹下所有的文件、文件夹
    12. for (File f : fs) {
    13. if (f.isDirectory()) {
    14. zipFile.addFolder(f, parameters);
    15. } else {
    16. zipFile.addFile(f, parameters);
    17. }
    18. }
    19. OutputStream outputStream = response.getOutputStream();
    20. InputStream fis = new FileInputStream(zipFile.getFile());
    21. // 将压缩文件写入到输出流
    22. IOUtils.copy(fis, outputStream);
    23. }