I am trying to archive all the files in the root directory of the folder but have the backups as one of the folders in the root directory so I wanted to exclude it so each backup after the first one isn't backing up the backups folder (aka increasing the size of the archive exponentially) in java, If it is not possible with GZIPOutputStream or ZIPOutputStream could you recommend me an alternative method for backing up files that allows excluding files and/or directories from the backup? Thank you for reading!
Asked
Active
Viewed 934 times
1
-
If you don't want to iterate over all the files yourself, and thus have the ability to exclude certain files/folders, you can try [zip4j](https://github.com/srikanth-lingala/zip4j). That package allows you to exclude files with a filter. – Fullslack Sep 28 '20 at 16:47
-
Could you add that as an answer so I can mark it as the solution? or do you want to me quote you and hit the "Answer Your Question" button? – JordanPlayz158 Sep 29 '20 at 11:55
1 Answers
1
To be able to exclude certain files or folders from a Zip or GZip you have two options:
- Loop over all the files/folders and exclude any files/folders you don't want to have inside the zip.
- Use a zip package like zip4j. This allows you to exclude certain files with a filter.
List<File> filesToExclude = Arrays.asList(new File("/full/path/to/folder_to_add/sample.pdf"), new File("/full/path/to/folder_to_add/subfolder/sample_2.txt"), new File("/full/path/to/folder_to_add/subfolder_to_exclude"));
ExcludeFileFilter excludeFileFilter = filesToExclude::contains;
ZipParameters zipParameters = new ZipParameters();
zipParameters.setExcludeFileFilter(excludeFileFilter);
new ZipFile("filename.zip").addFolder(new File("/full/path/to/folder_to_add"), zipParameters); // On Windows you need to include drive letter as well
Don't forget to include the driveletter (ie: C:
) on Windows systems.
You can see that it is also possible to exclude complete folders, not only files. But the full (or relative) path to file/folder is necessary!
Edit: not only is the documentation missing the filesToExclude
list definition, it is also unclear about the absolute path requirements for all new File()
calls. Updated answer to reflect this. You either need to use a absolute path or a relative path. Relative path are made with the .
prepend.
Current example is tested and working on Windows 10.

Fullslack
- 290
- 1
- 2
- 11
-
I've tried a number of types but I was unable to find any that worked for "filesToExclude" (String, File, CharSequence, etc.), do you know the type of variable "filesToExclude" is suppose to be, the readme never mentions what type they used for "filesToExclude" – JordanPlayz158 Sep 29 '20 at 17:00
-
@JordanPlayz158 it seems that part has been dropped from the readme yes, I've updated my answer. `List
filesToExclude = Arrays.asList(new File("sample.pdf"), new File("sample_2.txt"));`. Could you check if this is working? – Fullslack Sep 29 '20 at 19:09 -
After trying many different things, sadly it doesn't seem to be working. – JordanPlayz158 Sep 30 '20 at 16:25
-
@JordanPlayz158 I've updated it again. I did not expect it as well, but you need to include the full file path for both the folder to zip as well as the files/folders to exclude. Current example is tested on Windows 10 and works perfectly. – Fullslack Sep 30 '20 at 18:33
-
Well I don't believe you need absolute paths because I was trying to compress things with "." and it zipped everything in the root directory the jar was running in. Edit: yeah, just tested it, you can use "." for relative jar path and for excluding files. – JordanPlayz158 Sep 30 '20 at 21:50
-
@JordanPlayz158 I hoped relative would work but forgot to test it. Glad to hear it's working now! – Fullslack Oct 01 '20 at 05:42