4

I have a problem to get the extension of given file name. I receive 1 file URL.
It may be like

/var/www.dir/file.tar.gz OR /var/www.dir/file.exe

Now I need to split the file extension (like .tar.gz OR .exe) from the given URL. The URL is given by the user dynamically. Any one please help me out, how to solve this problem

Nunser
  • 4,512
  • 8
  • 25
  • 37
Bathakarai
  • 1,517
  • 6
  • 23
  • 39

8 Answers8

8

This is not a terrible complicated task but one that reoccurs repeatedly. Looks like something that can be produced quickly by yourself - and it can. But you still should use a ready-made, tested library for this: Apache IO Tools, specifically FilenameUtils#getExtension. This way you avoid creepy bugs or inconsistencies. Plus you get a lot of other helpful, tested stuff.

Of course in the case of the double file extension you may need to use the methods more than once, as the library can not guess up to what level you need the extension(s). It is a .gz file, only after un-gunzipping it is a .tar file.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
4

You could use a combination of lastIndexOf and indexOf:

String s = "/var/www.dir/file.tar.gz";
String file = s.substring(s.lastIndexOf("/"));
String extension = file.substring(file.indexOf(".")); // .tar.gz

DEMO.

João Silva
  • 89,303
  • 29
  • 152
  • 158
3

You could do something like:

String str = "/var/www.dir/file.tar.gz";
Matcher m = Pattern.compile(".*/.*?(\\..*)").matcher(str);
if (m.matches()) {
   fileExtension = m.group(1);
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Hey, one issue with this might be for a path with a dot in it, like a git repository. For example, /my/path/to/a.git/repositry – shortstuffsushi Sep 19 '12 at 21:51
  • No, still works with that, first part of expression is _greedy_. – Reimeus Sep 19 '12 at 21:54
  • I guess I missed that when I first saw the post. The only remaining issue would be handling files with no extensions, but I'm guessing that isn't a case that has to be handled here. – shortstuffsushi Sep 19 '12 at 22:01
0

It seems like the simplest solution to this would be to create separate cases for the outliers (like .tar.gz) and treat the rest as the same (i.e., process out everything after the last . character).

Extensions with multiple . characters in them are so extremely uncommon that you don't need a convoluted method to parse out what you need. So:

int length = url.length();
String extension = null;
if(url.substring(length-7).equalsIgnoreCase(".tar.gz")) {
    //special case
} else {
    extension = url.substring(url.lastIndexOf("."));
}
asteri
  • 11,402
  • 13
  • 60
  • 84
  • It's not mean like .tar.gz , the number of dot may goes long. we couldn't determine how long it is??? – Bathakarai Sep 19 '12 at 21:38
  • Extensions so rarely contain more than one '.' that it's probably easier to just handle those special cases than create a regex is the point. Of course, you could assume that everything after the first occurrence of '.' is the extension, but this will produce its own problems if a file name contains that character. – asteri Sep 19 '12 at 21:43
  • Ya, it's true. I accept your words @Jeff – Bathakarai Sep 19 '12 at 21:46
  • 2
    The extension is per definition only the part after the least '.' In a file name. So a file AAbc.tar. gz is a gzipped file. It doesn't matter that a tar archive was zipped, that's only convenient for you. If it matters for you, you should describe why. – Timo Hahn Sep 19 '12 at 21:47
0

I just realized that I read your question incorrectly. You could gather the extensions in a similar method, though

String yourString = "/your/whole/path.ext.si.ons";
String[] pathPieces = yourString.split("/"); // [ "your", "whole", "path.ext.si.ons" ]

String lastPiece = pathPieces[pathPieces.length - 1];
String[] namePieces = lastPiece.split("."); // [ "path", "ext", "si", "ions" ]

At this point, the first piece would probably be your name, where the rest of the pieces would be the extensions. You could then join these to have your extension type.

shortstuffsushi
  • 2,271
  • 19
  • 33
0

You can use this code to get the file extension

public static String getFileExtension(String filename)
{
if (filename == null) {
            return null;
}
int lastUnixPos = filename.lastIndexOf('/');
int lastWindowsPos = filename.lastIndexOf('\\');
int indexOfLastSeparator = Math.max(lastUnixPos, lastWindowsPos);
int extensionPos = filename.lastIndexOf('.');
int lastSeparator = indexOfLastSeparator;
int indexOfExtension = lastSeparator > extensionPos ? -1 : extensionPos;
int index = indexOfExtension;
if (index == -1) {
  return "";
} else {
  return filename.substring(index + 1);
}
}
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30
0

Assuming that URL pattern to be : "/somurl/blah-blah/filename.extension" . I would suggest the following code snippet as a solution to your problem.

String getFileExtension(String url) {
    String[] urlSplit = url.split("/");
    String filename = urlSplit[urlSplit.length - 1];
    String[] nameSplit = filename.split("[.]");
    StringBuffer fileExtension = new StringBuffer();
// to prevent appending . after extension type                                      
    if (nameSplit.length > 1) {
        for (int index = 1; index < nameSplit.length; index++) {
            if (index != nameSplit.length - 1)
                fileExtension.append(nameSplit[index] + ".");
            else
                fileExtension.append(nameSplit[index]);
        }
    } else {
        fileExtension.append(nameSplit[0]);
    }
    return fileExtension.toString();
}
Mrugen Deshmukh
  • 249
  • 2
  • 8
  • 18
  • Not bad. But what would you say for such url type - "https://scontent-frt3-2.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p261x260/19884496_1441777802579911_6542689024155960641_n.jpg?efg=eyJpIjoidCJ9&oh=9882051a07b89b32c9b6d4d996b2a941&oe=5A11F44A" ? – Anton Kizema Jul 12 '17 at 09:41
-1

Get the file input, then split it using a '.' as a delimiter maybe? to set a delimiter:

Scanner line_scanner = new Scanner(line).useDelimiter(".");

read the file URL twice, once for the filename and once for the extension.

Azulflame
  • 1,534
  • 2
  • 15
  • 30