Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Extension for special cases like tar.gz [closed]

I need to extract extensions from file names.

I know this can be done for single extensions like .gz or .tar by using filePath.lastIndexOf('.') or using utility methods like FilenameUtils.getExtension(filePath) from Apache commons-io.

But, what if I have a file with an extension like .tar.gz? How can I manage files with extensions that contain . characters?

like image 974
Bernice Avatar asked Jul 16 '13 12:07

Bernice


People also ask

What is the extension of TAR GZ?

The tar GZ file extension is widely used on UNIX based operating systems but can also be used on Windows and macOS with WinZip. Tar GZ files are most commonly used for: Storing multiple files in one archive. Sending and receiving larger files in a compressed format.

Which of the following is the correct way to get an extension of a file in Nodejs?

The path. extname() method returns the extension of a file path.

How do I get the file extension in react JS?

To get a filename extension, you can use a combination of split() and pop() methods. The split() method will convert a string into an array of substrings, separated by the character you passed as the method's parameter. And that's how you can get the file extension from a filename.

What programs can open TAR GZ files?

Since Windows doesn't natively support tar. gz files, you need a third-party tool to open them for you. Most file extraction applications like 7-Zip or WinZip will get the job done. Begin by downloading and installing 7-Zip on your computer if you don't already have it.


2 Answers

If you know what extensions are important, you can simply check for them explicitly. You would have a collection of known extensions, like this:

List<String> EXTS = Arrays.asList("tar.gz", "tgz", "gz", "zip");

You could get the (first) longest matching extension like this:

String getExtension(String fileName) {
  String found = null;
  for (String ext : EXTS) {
    if (fileName.endsWith("." + ext)) {
      if (found == null || found.length() < ext.length()) {
        found = ext;
      }
    }
  }
  return found;
}

So calling getExtension("file.tar.gz") would return "tar.gz".

If you have mixed-case names, perhaps try changing the check to filename.toLowerCase().endsWith("." + ext) inside the loop.

like image 83
grkvlt Avatar answered Sep 30 '22 20:09

grkvlt


A file can just have one extension!

If you have a file test.tar.gz,

  • .gz is the extension and
  • test.tar is the Basename!

.tar in this case is part of the basename, not the part of the extension!

If you like to have a file encoded as tar and gz you should call it .tgz. To use a .tar.gz is bad practice, if you need to handle thesse files you should make a workaround like rename the file to test.tgz.

like image 21
Grim Avatar answered Sep 30 '22 20:09

Grim