Use Apache commons-compress to automatically handle various compressed archives

648 Views Asked by At

I would like to use commons-compress to work with various compression/archive formats.

However on first look it seems commons-compress only supports detecting some types of files, but only based on the first few bytes.

Is there a way I can use commons-compress to automatically detect file-types based on file extension? I surely can build this myself, but it would be nice to have this provided by the compression library itself.

1

There are 1 best solutions below

0
On

After some more digging, I found that there are a few classes that help here, namely FilenameUtil, BZip2Utils, GzipUtils, ... so for each supported format, there is a *Utils class which allows to detect this type by extension.

See e.g. http://commons.apache.org/proper/commons-compress/javadocs/api-1.10/org/apache/commons/compress/compressors/bzip2/BZip2Utils.html