Java Zip File Check isZipFile(File file)

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

Description

is Zip File

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Return

True if file is an archive zip file

Declaration

public static boolean isZipFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/**/* w w  w.j  a  v a2  s.  c  om*/
 * This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /**
     * @param file
     * @return True if file is an archive zip file
     * @throws IOException 
     */
    public static boolean isZipFile(File file) throws IOException {
        // Signature first 4 bytes of zip file
        final byte[] sig = new byte[] { 0x50, 0x4B, 0x3, 0x4 };

        if (file.canRead()) {
            byte[] buf = new byte[4];

            FileInputStream is = null;
            try {
                is = new FileInputStream(file);
                is.read(buf);
            } finally {
                if (is != null) {
                    is.close();
                }
            }

            for (int i = 0; i < buf.length; i++) {
                if (buf[i] != sig[i]) {
                    return false;
                }
            }

            return true;
        }

        return false;
    }
}

Related

  1. isZipFile(File f)
  2. isZipFile(File f)
  3. isZipFile(File f)
  4. isZipFile(File file)
  5. isZipFile(File file)
  6. isZipFile(File file)
  7. isZipFile(File file)
  8. isZipFile(File file)
  9. isZipFile(File file)