Java Zip File Check isZipped(InputStream inputStream)

Here you can find the source of isZipped(InputStream inputStream)

Description

Checks the first four bytes of a given resource and determines if it is zipped or not.

License

Open Source License

Exception

Parameter Description
IOException an exception

Return

true if the resource URI represents a zipped file.

Declaration

public static boolean isZipped(InputStream inputStream) throws IOException 

Method Source Code


//package com.java2s;
/* license-start//from   w ww.  j a  va  2  s .c  o  m
 * 
 * Copyright (C) 2008 - 2013 Crispico, <http://www.crispico.com/>.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details, at <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *   Crispico - Initial API and implementation
 *
 * license-end
 */

import java.io.DataInputStream;
import java.io.EOFException;

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

public class Main {
    /**
     * Checks the first four bytes of a given resource and determines if it is
     * zipped or not.
     * 
     * @return <code>true</code> if the resource URI {@link #getURI()}
     *         represents a zipped file.
     * @throws IOException
     */
    public static boolean isZipped(InputStream inputStream) throws IOException {
        DataInputStream dis = new DataInputStream(inputStream);
        try {
            return dis.readInt() == 0x504b0304;
        } catch (EOFException e) {
            // has less then 4 bytes
            // it can't be zipped
            return false;
        } finally {
            dis.close();
        }
    }
}

Related

  1. isZipFile(String path)
  2. isZipOrJarArchive(File file)
  3. isZipped(File f)
  4. isZipped(File file)
  5. isZipped(final File file)
  6. isZipped(String name)
  7. isZippedFile(File file)
  8. isZipStream(ByteArrayInputStream input)
  9. isZipStream(InputStream in)