4

I will create a sample.txt file. And then I will change extension of sample.txt to sample.tar

How do I know the real type of the sample file?

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Jar Yit
  • 955
  • 11
  • 22
  • [Possible duplicate maybe](http://stackoverflow.com/questions/10516354/how-to-find-file-extension-if-the-file-has-been-renamed) – RanRag Jan 27 '13 at 17:47
  • http://stackoverflow.com/questions/2729038/is-there-a-java-library-equivalent-to-file-command-in-unix – PeterMmm Jan 27 '13 at 17:48

4 Answers4

5

A file only contains bytes. What you believe those bytes mean is entirely up to you. Any notion that a file has a real type is an illusion. For example you could have called it sample.txt but it was actually a TAR file.

There are tools to guess what the file format might be. However this is just a guess. A file doesn't have a "real" file type.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • i no mean real file type.i want to know file is tar file or not – Jar Yit Jan 27 '13 at 18:08
  • You can try untarring it (at least in memory). If this is successful, it is a valid tar file. If unsuccessful it is not. You can look at the start of the file (which is what most of the generic tools do) but this won't tell you the file is valid. – Peter Lawrey Jan 27 '13 at 18:12
  • 1
    Do you mean you want to know how to untar a file to check it is valid? – Peter Lawrey Jan 27 '13 at 18:28
  • yes ,i want to check before untar a file in java coding.pls help me. – Jar Yit Jan 27 '13 at 18:37
  • I haven't done this myself so I did a quick google and it appears that using this library is the best approach http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.4.1 A link to some sample code is here http://stackoverflow.com/a/7556307/57695 – Peter Lawrey Jan 27 '13 at 18:39
  • thank for your help.but i ready use this link,but not work for me. – Jar Yit Jan 27 '13 at 18:49
  • I would not use looping to check tar file is valid or not before untar. – Jar Yit Jan 27 '13 at 18:53
  • 1
    What would you do instead? – Peter Lawrey Jan 27 '13 at 19:02
2

You can use a library such as Java Mime Magic to check if the supposed MIME type of the file matches its contents.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0
import java.net.FileNameMap;
import java.net.URLConnection;

public class FileUtils {

public static String getMimeType(String fileUrl) throws java.io.IOException {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String type = fileNameMap.getContentTypeFor(fileUrl);

    return type;
}

public static void main(String args[]) throws Exception {
    System.out.println(FileUtils.getMimeType("fileName.extension"));
     //for a.txt
    // output : text/plain
}
}

This works fine if the file is not rar or compressed.

Refer to this link. Get the Mime Type from a File.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

If you just want to know if a file is a tar file you can read the bytes 257, 258, 259, 260, 261. If they are ASCII 'ustar' it is a tar archive.

Sebastian Annies
  • 2,438
  • 1
  • 20
  • 38