Zip4j从Zip排除文件夹 [英] Zip4j Excluding Folders from Zip

查看:296
本文介绍了Zip4j从Zip排除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a directory with some folders that should be skipped and not added to the target ZIP file. I marked them as hidden on Windows and I can query this attribute using Java code as follows:

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

但是,我不知道如何在以下 Zip4j 为基础的方法,以跳过添加相应目录的操作:

However, I don't know how to use this with the following Zip4j-based method to skip adding those respective directories:

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();
}

请注意,此方法仅在(隐藏)文件夹位于最

Note that this method only works when the (hidden) folders are in the most upper level but it should work for any depth.

推荐答案

为解决此问题,我将所有隐藏文件夹移出,打包了zip文件文件,然后将文件夹移回:

To solve this I went with moving all hidden folders out, package the zip file and move the folders back:

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

// Create zip

hiddenDirectoriesMover.returnFiles();

一个肮脏的解决方法,但是由于 zipParameters.setReadHiddenFiles(false ); 无法正常工作

A dirty work-around but does the job since 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;
}

这篇关于Zip4j从Zip排除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆