Here you can find the source of isZipFile(File f)
Parameter | Description |
---|---|
f | the file to test. |
public static boolean isZipFile(File f)
//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; } }