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

时间:2019-06-06 12:54:35

标签: java groovy directory zip

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

我正在使用groovy,因此Java解决方案非常好。

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

3 个答案:

答案 0 :(得分:1)

您可以为此使用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");

答案 1 :(得分:1)

这个解决方案难道不是很时髦吗?

def zipDir(srcDir, zipFile) {
    def dir = new File(srcDir)
    def files = []
    dir.eachFileRecurse(groovy.io.FileType.FILES) { file ->
        files << file
    }

    FileOutputStream fos = new FileOutputStream(zipFile)
    def zos = new java.util.zip.ZipOutputStream(fos)

    for (file in files) {
        zos.putNextEntry(new java.util.zip.ZipEntry(file.path.minus(srcDir)))
        zos << file
        zos.closeEntry()
    }

    // close the ZipOutputStream
    zos.close()
}

答案 2 :(得分:1)

基于@urichenslin 的回答,这里有一个适用于子目录并解决@MistaWizard 评论的问题的方法:

static File compress(final File srcDir, final File zipFile) {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))
    srcDir.eachFileRecurse({
        zos.putNextEntry(new ZipEntry(it.path - srcDir.path + (it.directory ? "/" : "")))
        if(it.file) { zos << it.bytes }
        zos.closeEntry()
    })
    zos.close()
    return zipFile
}