Java Zip File Check isZipFile(java.io.File file)

Here you can find the source of isZipFile(java.io.File file)

Description

Checks is specified file is zip file or not.

License

Open Source License

Declaration

public static boolean isZipFile(java.io.File file) throws IOException 

Method Source Code


//package com.java2s;

import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /**/*from w  w w  .ja  v  a 2s.co m*/
     * Checks is specified file is zip file or not. Zip file <a href="http://en.wikipedia.org/wiki/Zip_(file_format)#File_headers">headers
     * description</a>.
     */
    public static boolean isZipFile(java.io.File file) throws IOException {
        if (file.isDirectory()) {
            return false;
        }
        // NOTE: little-indian bytes order!
        byte[] bytes = new byte[4];
        try (FileInputStream fIn = new FileInputStream(file)) {
            if (fIn.read(bytes) != 4) {
                return false;
            }
        }
        final int header = bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);
        return 0x04034b50 == header;
    }
}

Related

  1. isZipFile(File file)
  2. isZipFile(File file)
  3. isZipFile(File file)
  4. isZipFile(File file)
  5. isZipFile(InputStream stream)
  6. isZipFile(String fileName)
  7. isZipFile(String fileName)
  8. isZipFile(String path)
  9. isZipOrJarArchive(File file)