Zip4j Excluding Folders from Zip

无人久伴 提交于 2019-12-12 04:23:50

问题


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

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.


回答1:


You can add zipParameters.setReadHiddenFiles(false);, zip4j will not add hidden folders and files to ZipFile.




回答2:


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

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


来源:https://stackoverflow.com/questions/38314881/zip4j-excluding-folders-from-zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!