Java Zip File Check isZip(File file)

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

Description

is Zip

License

Open Source License

Parameter

Parameter Description
file the file to check.

Exception

Parameter Description
IOException if an IO problem occurs.

Return

true if this file is a zip file.

Declaration

public static boolean isZip(File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  ww .  j  a  v  a 2  s  .c  o m*/
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**
     * @return true if this file is a zip file.
     * @param file
     *            the file to check.
     * @throws IOException
     *             if an IO problem occurs.
     */
    public static boolean isZip(File file) throws IOException {
        if (file.exists() == false)
            return false;

        try (InputStream in = new FileInputStream(file); ZipInputStream zipIn = new ZipInputStream(in)) {
            ZipEntry e = zipIn.getNextEntry();
            if (e == null)
                return false;
            int ctr = 0;
            while (e != null && ctr < 4) {
                e = zipIn.getNextEntry();
                ctr++;
            }
            return true;
        } catch (Throwable t) {
            return false;
        }
    }
}

Related

  1. isZip(BufferedInputStream in)
  2. isZip(byte[] bytes)
  3. isZip(File candidate)
  4. isZip(File f)
  5. isZip(File file)
  6. isZip(File file)
  7. isZip(File file)
  8. isZip(File zip)
  9. isZip64EndOfCentralDirectoryLocatorPresent(RandomAccessFile zip, long zipEndOfCentralDirectoryPosition)