Zip4j 从 Zip 中排除文件夹

Zip4j Excluding Folders from Zip

我有一个包含一些文件夹的目录,这些文件夹应该被跳过,而不是添加到目标 ZIP 文件中。我在 Windows 上将它们标记为隐藏,我可以使用 Java 代码查询此属性,如下所示:

new File("C:\myHiddenFolder").isHidden();

但是,我不知道如何将其与以下基于 Zip4j 的方法结合使用来跳过添加这些相应的目录:

public File createZipArchive(String sourceFilePath) throws ZipException, IOException
{
    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
    zipParameters.setPassword("MyPassword");

    String baseFileName = FileNameUtilities.getBaseFileName(sourceFilePath);
    String destinationZipFilePath = baseFileName + "." + EXTENSION;

    ZipFile zipFile = new ZipFile(destinationZipFilePath);
    File sourceFile = new File(sourceFilePath);

    // Recursively add directories
    if (sourceFile.isDirectory())
    {
        File[] childrenFiles = sourceFile.listFiles();

        if (childrenFiles != null)
        {
            for (File folder : childrenFiles)
            {
                if (folder.isHidden()) // Nope, no recursive checking!
                {
                    // This is the problem, it adds the parent folder and all child folders without allowing me to check whether to exclude any of them...
                    zipFile.addFolder(folder.getAbsolutePath(), zipParameters);
                }
            }
        }
    } else
    {
        // Add just the file
        zipFile.addFile(new File(sourceFilePath), zipParameters);
    }

    return zipFile.getFile();
}

请注意,此方法仅在(隐藏)文件夹位于最上层时有效,但它应该适用于任何深度。

您可以添加zipParameters.setReadHiddenFiles(false);,zip4j不会将隐藏的文件夹和文件添加到ZipFile

为了解决这个问题,我将所有隐藏文件夹移出,打包 zip 文件并将文件夹移回:

HiddenDirectoriesMover hiddenDirectoriesMover = new HiddenDirectoriesMover(sourceFilePath);
        hiddenDirectoriesMover.removeFiles();

// Create zip

hiddenDirectoriesMover.returnFiles();

一个肮脏的解决方法,但自 zipParameters.setReadHiddenFiles(false); is not working as expected:

public static ArrayList getFilesInDirectoryRec(File path, 
        boolean readHiddenFiles) throws ZipException {

    if (path == null) {
        throw new ZipException("input path is null, cannot read files in the directory");
    }

    ArrayList result = new ArrayList();
    File[] filesAndDirs = path.listFiles();
    List filesDirs = Arrays.asList(filesAndDirs);

    if (!path.canRead()) {
        return result; 
    }

    for(int i = 0; i < filesDirs.size(); i++) {
        File file = (File)filesDirs.get(i);
        if (file.isHidden() && !readHiddenFiles) {
            // The first hidden file causes a return and skipping everything else (!)
            return result;
        }
        result.add(file);
        if (file.isDirectory()) {
            List deeperList = getFilesInDirectoryRec(file, readHiddenFiles);
            result.addAll(deeperList);
        }
    }
    return result;
}