Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if the file is of a certain type

I want to validate if all the files in a directory are of a certain type. What I did so far is.

private static final String[] IMAGE_EXTS = { "jpg", "jpeg" };

private void validateFolderPath(String folderPath, final String[] ext) {

        File dir = new File(folderPath);

        int totalFiles = dir.listFiles().length;

        // Filter the files with JPEG or JPG extensions.
        File[] matchingFiles = dir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(ext[0])
                        || pathname.getName().endsWith(ext[1]);
            }
        });

        // Check if all the files have JPEG or JPG extensions
        // Terminate if validation fails.
        if (matchingFiles.length != totalFiles) {
            System.out.println("All the tiles should be of type " + ext[0]
                    + " or " + ext[1]);
            System.exit(0);
        } else {
            return;
        }

    }

This works fine if the file name have an extension like {file.jpeg, file.jpg} This fails if the files have no extensions {file1 file2}. When I do the following in my terminal I get:

$ file folder/file1 
folder/file1: JPEG image data, JFIF standard 1.01

Update 1:

I tried to get the magic numbers of the file to check if it is JPEG:

for (int i = 0; i < totalFiles; i++) {
            DataInputStream input = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(
                            dir.listFiles()[i])));

            if (input.readInt() == 0xffd8ffe0) {
                isJPEGFlag = true;
            } else {
                isJPEGFlag = false;
                try {
                    input.close();
                } catch (IOException ignore) {
                }
                System.out.println("File not JPEG");
                System.exit(0);
            }
        }

I ran into another problem. There are some .DS_Store files in my folder. Any idea how to ignore them ?

like image 381
yesh Avatar asked Oct 11 '12 17:10

yesh


People also ask

How can I tell what file type an upload is in Python?

We can use Python os module splitext() function to get the file extension.

How do I check if a file exists in R?

To check if the file or folder exists in R, use the file. exists() method. The file. exists() method returns the logical vector indicating whether the files named by its argument exist.


1 Answers

Firstly, file extensions are not mandatory, a file without extension could very well be a valid JPEG file.

Check the RFC for JPEG format, the file formats generally start with some fixed sequence of bytes to identify the format of the file. This is definitely not straight forward, but I am not sure if there is a better way.

In a nutshell you have to open each file, read first n bytes depending on file format, check if they match to file format you expect. If they do, its a valid JPEG file even if it has an exe extension or even if it does not have any extension.

like image 157
Kalpak Gadre Avatar answered Sep 30 '22 08:09

Kalpak Gadre