Java Zip File Check isZip(BufferedInputStream in)

Here you can find the source of isZip(BufferedInputStream in)

Description

Test for ZIP stream signature.

License

Open Source License

Parameter

Parameter Description
in the BufferedInputStream to test.

Return

true if input stream is zip.

Declaration

public static boolean isZip(BufferedInputStream in) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;

import java.util.Arrays;

public class Main {
    /**/*from  w w  w .  jav  a2s . c o m*/
     * Test for ZIP stream signature.
     *
     * @param in the BufferedInputStream to test.
     * @return true if input stream is zip.
     */
    public static boolean isZip(BufferedInputStream in) {
        in.mark(4);
        byte[] b = new byte[4];
        byte[] zipSig = new byte[4];
        zipSig[0] = 0x50;
        zipSig[1] = 0x4b;
        zipSig[2] = 0x03;
        zipSig[3] = 0x04;

        try {
            in.read(b, 0, 4);
        } catch (Exception ex) {
            throw new RuntimeException("Couldn't read header from stream ",
                    ex);
        }
        try {
            in.reset();
        } catch (Exception ex) {
            throw new RuntimeException("Couldn't reset stream ", ex);
        }

        return Arrays.equals(b, zipSig);
    }
}

Related

  1. isZip(byte[] bytes)
  2. isZip(File candidate)
  3. isZip(File f)
  4. isZip(File file)