在使用Java中的GZIPOutputStream或ZIPOutputStream时,是否可以排除文件和/或目录?

huangapple go评论107阅读模式
英文:

Is it possible to exclude files and/or directories when using GZIPOutputStream or ZIPOutputStream in java?

问题

我正在尝试归档文件夹的根目录中的所有文件,但是希望备份作为根目录中的一个文件夹,所以我想要将其排除在外,以便每次备份都不会备份备份文件夹(即不会呈指数级增加归档大小)。我想在Java中实现这个,如果使用GZIPOutputStream或ZIPOutputStream无法实现,您能否为我推荐一种备份文件的替代方法,允许从备份中排除文件和/或目录?感谢您的阅读!

英文:

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!

答案1

得分: 1

要能够从Zip或GZip中排除某些文件或文件夹,您有两个选项:

  1. 遍历所有文件/文件夹,排除任何您不想在zip中包含的文件/文件夹。
  2. 使用类似于 zip4j 的zip包。这允许您使用过滤器排除某些文件。
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

不要忘记在Windows系统上包含驱动器号(例如:C:)。

您可以看到,还可以排除完整的文件夹,不仅限于文件。但是文件/文件夹的完整(或相对)路径是必要的!

编辑: 文档不仅缺少filesToExclude列表的定义,还不清楚所有new File()调用的<strike>绝对</strike>路径要求。更新答案以反映这一点。您需要使用绝对路径或相对路径。相对路径使用.前缀创建。
当前示例在Windows 10上经过测试并且可正常工作。

英文:

To be able to exclude certain files or folders from a Zip or GZip you have two options:

  1. Loop over all the files/folders and exclude any files/folders you don't want to have inside the zip.
  2. Use a zip package like zip4j. This allows you to exclude certain files with a filter.
List&lt;File&gt; filesToExclude = Arrays.asList(new File(&quot;/full/path/to/folder_to_add/sample.pdf&quot;), new File(&quot;/full/path/to/folder_to_add/subfolder/sample_2.txt&quot;), new File(&quot;/full/path/to/folder_to_add/subfolder_to_exclude&quot;));
ExcludeFileFilter excludeFileFilter = filesToExclude::contains;
ZipParameters zipParameters = new ZipParameters();
zipParameters.setExcludeFileFilter(excludeFileFilter);
new ZipFile(&quot;filename.zip&quot;).addFolder(new File(&quot;/full/path/to/folder_to_add&quot;), 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 <strike>absolute</strike> 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.

huangapple
  • 本文由 发表于 2020年9月29日 00:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64105981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定