Here you can find the source of isZipFile(File file)
Parameter | Description |
---|---|
file | the file to interrogate for being a zip file |
Parameter | Description |
---|---|
IOException | an exception |
public static boolean isZipFile(File file) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 CA. All rights reserved. * * This source file is licensed under the terms of the Eclipse Public License 1.0 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; public class Main { private final static byte[] ZIP_SIGNATURE = { 'P', 'K', 0x03, 0x04 }; /**//from ww w. j a v a2s . c o m * A quick test to determine if uploaded file is a ZIP file * <p> * @param file the file to interrogate for being a zip file * @return true is the specified file is a zip file * @throws IOException */ public static boolean isZipFile(File file) throws IOException { boolean isZipFile = false; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); if (fileInputStream.available() > ZIP_SIGNATURE.length) { byte[] magic = new byte[ZIP_SIGNATURE.length]; if (ZIP_SIGNATURE.length == fileInputStream.read(magic, 0, ZIP_SIGNATURE.length)) { isZipFile = Arrays.equals(magic, ZIP_SIGNATURE); } } } finally { if (null != fileInputStream) { fileInputStream.close(); } } return isZipFile; } }