Java Zip File Check isZipFile(File f)

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

Description

Test if a file is a zip file.

License

Apache License

Parameter

Parameter Description
f the file to test.

Declaration

public static boolean isZipFile(File f) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static byte[] MAGIC = { 'P', 'K', 0x3, 0x4 };

    /**/* www. j  a v  a  2  s . co m*/
     * Test if a file is a zip file.
     * 
     * @param f
     *            the file to test.
     * @return
     */
    public static boolean isZipFile(File f) {

        boolean isZip = true;
        byte[] buffer = new byte[MAGIC.length];
        try {
            RandomAccessFile raf = new RandomAccessFile(f, "r");
            raf.readFully(buffer);
            for (int i = 0; i < MAGIC.length; i++) {
                if (buffer[i] != MAGIC[i]) {
                    isZip = false;
                    break;
                }
            }
            raf.close();
        } catch (Throwable e) {
            isZip = false;
        }
        return isZip;
    }
}

Related

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