Here you can find the source of isZip(File file)
Parameter | Description |
---|---|
file | the file to check. |
Parameter | Description |
---|---|
IOException | if an IO problem occurs. |
public static boolean isZip(File file) throws IOException
//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; } } }