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

Return

Returns true if the given file is a zip file.

Declaration

public static boolean isZipFile(File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   www.  j  a  v  a 2s  .c om*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class Main {
    /**
     * @return Returns <code>true</code> if the given file is a zip file.
     */
    public static boolean isZipFile(File file) {
        if (file == null || file.isDirectory() || !file.canRead() || file.length() < 4) {
            return false;
        }
        try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) {
            int test = in.readInt();
            return test == 0x504b0304; // magic number of a zip file
        } catch (Exception e) {
            return false;
        }
    }
}

Related

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