java - Groovy - 如何在 Groovy 中压缩整个目录?

标签 java groovy directory zip

我找到了an example on how to zip all the files in a folder但我想压缩整个文件夹,及其所有子文件夹以及其中包含的文件。

我使用的是 groovy,因此 Java 解决方案完全没问题。

如何在 groovy 中压缩整个文件夹及其内容?

最佳答案

您可以使用Zeroturnaround Zip为此。

ZipUtil.pack(new File("/your/folder/here"), new File("/your/zip/here.zip"));

添加此依赖项

<dependency>
  <groupId>org.zeroturnaround</groupId>
  <artifactId>zt-zip</artifactId>
  <version>1.13</version>
  <type>jar</type>
</dependency>

[已更新]

这是使用 Java 8 解决方案的一种方法:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class AppZip {

    private List<String> fileList;
    private String sourceFolder;

    AppZip(String sourceFolder) {
        this.sourceFolder = sourceFolder;
        fileList = new ArrayList<>();
    }

    /**
     * Zip it
     *
     * @param zipFile output ZIP file location
     */
    public void zipIt(String zipFile) {

        byte[] buffer = new byte[1024];

        try {
            generateFileList(new File(sourceFolder));

            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (String file : this.fileList) {

                ZipEntry ze = new ZipEntry(file);
                zos.putNextEntry(ze);

                FileInputStream fin =
                        new FileInputStream(sourceFolder + File.separator + file);

                int len;
                while ((len = fin.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }

                fin.close();
            }

            zos.closeEntry();
            zos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Traverse a directory and get all files,
     * and add the file into fileList
     *
     * @param node file or directory
     */
    public void generateFileList(File node) {

        //add file only
        if (node.isFile()) {
            fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
        }

        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                generateFileList(new File(node, filename));
            }
        }

    }

    /**
     * Format the file path for zip
     *
     * @param file file path
     * @return Formatted file path
     */
    private String generateZipEntry(String file) {
        return file.substring(sourceFolder.length() + 1);
    }
}

然后调用:

        AppZip appZip = new AppZip("/your/folder/here");
        appZip.zipIt("/your/zip/here");

关于java - Groovy - 如何在 Groovy 中压缩整个目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56478053/

相关文章:

java - 使用JAX-RS(RESTEasy)作为中间件: brokering a client's request to another server

validation - 向外部系统发送请求之前的其他验证逻辑

java - 如何使用Java代码将文件夹(包括子目录和文件) move 到新文件夹中

java - 打印目录中多个文件的所有内容

directory - Go语言如何检查zip入口是否为目录

java - java中的默认类加载器是将所有内容都保存在内存中还是留在文件系统中?

Java:自定义向当前日期添加 1 个月

JavaFX 11 : Add a graphic to a TitledPane on the right side

grails - 如何保存具有多个多对一关系的 GORM 对象?

java - 打开一个新的数据库连接或保持连接打开直到进程完成是好事吗?