2016-07-11 178 views
0

我有一個目錄,其中應該跳過一些文件夾,但不會將其添加到目標ZIP文件中。我將其標記爲隱藏在Windows,我可以如下查詢使用Java代碼此屬性:Zip4j從Zip中排除文件夾

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

請注意,此方法僅適用於(隱藏)文件夾位於最上層但適用於任何深度的情況。

回答

0

爲了解決這個我就跟着移動所有隱藏的文件夾時,包中的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; 
} 
-1

您可以添加zipParameters.setReadHiddenFiles(false);,zip4j不會將隱藏的文件夾和文件添加到ZipFile

+0

這是錯誤的,因爲當找到第一個隱藏文件夾時它會停止。我也閱讀了實現,這就是它的作用。 – BullyWiiPlaza