1

I have created a backup system which I sell called EasyBackup for servers. The system works great, and it uses zipped files. Someone requested that I add a feature to add a password to the zipped folder, but I could not find a way to do that with Java's zip utility classes, so I decided to use Zip4j.

Zip4j adds passwords perfectly, but when I add a folder, it adds all files and subfolders.

I have an option for users to exempt files and folders, so I need a way to retain this ability.

How can I exempt files and folders with Zip4J. Is there a way to add folders and files manually so the children are not added?

If not, would modifying the sourcecode for Zip4J be practical? Is there another library that does this already?

Forseth11
  • 1,418
  • 1
  • 12
  • 21
  • Did you check out https://stackoverflow.com/questions/38314881/zip4j-excluding-folders-from-zip? – clinomaniac Apr 03 '18 at 18:00
  • @clinomaniac Yes. That solution will not work here, and that is a very bad solution. I cannot move any files around. I am creating a backup for a minecraft server while it is running, and saving it within the server files. Without being able to exempt files, it will backup forever. Most of my clients use VPSs and do not have access to any other directories too. – Forseth11 Apr 03 '18 at 18:03
  • Found a few other options with a quick search but cannot confirm if they allow skipping files as you need without going deeper into it. Just a hacky idea, not sure if you would want to use this but might be simple enough for a fix for now. You can create the zip using Java's zip utility and then extract contents into a temp folder and then zip those again using Zip4j and adding a password. – clinomaniac Apr 03 '18 at 18:10
  • @clinomaniac The issue what that, is that the zipped file is saved in the same path that is being zipped. It will go on forever reading the zipped file. I was looking through the Zip4J source, and I found this method: `Zip4jUtil.getFilesInDirectoryRec(file, false);`. This returns a list of files and folders under a folder to be zipped. Should I modify this to include another parameter to exclude files and folders based on name? – Forseth11 Apr 03 '18 at 18:18

1 Answers1

0

I solved this by modifying Zip4j. I made a crude change which kind of abuses static, but it is in a Utility class, so I am not too concerned.

I made the following changes in the Zip4jUtil class, and I created an interface called ExemptFileHandler with one method:

public boolean isExempt(File file);

Then in the Zip4jUtil class I made the following static object:

private static ExemptFileHandler handler;

Finially, I modified the getFilesInDirectoryRec recursive method as so:

for(int i = 0; i < filesDirs.size(); i++) {
    File file = (File)filesDirs.get(i);
    if (file.isHidden() && !readHiddenFiles) {
        return result;
    }
    if(handler == null || !handler.isExempt(file)) {
        result.add(file);
        if (file.isDirectory()) {
            List deeperList = getFilesInDirectoryRec(file, readHiddenFiles);
            result.addAll(deeperList);
        }
    }
}

This now enables me to exempt files and folders. This is a crude fix. With this fix, I am only able to create one zipped file at a time, unless I am okay with exempt files being the same for each zipped file. I could modify it further, but this is all I need to make it work, and it does.

Forseth11
  • 1,418
  • 1
  • 12
  • 21