Java Zip File Check isZipStream(InputStream in)

Here you can find the source of isZipStream(InputStream in)

Description

The method to test if a input stream is a zip archive.

License

Open Source License

Parameter

Parameter Description
in the input stream to test.

Declaration

public static boolean isZipStream(InputStream in) throws Throwable 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static byte[] MAGIC = { 'P', 'K', 0x3, 0x4 };

    /**/*w w  w . j a  v a2s.  c o  m*/
     * The method to test if a input stream is a zip archive.
     * 
     * @param in
     *            the input stream to test.
     * @return
     */
    public static boolean isZipStream(InputStream in) throws Throwable {

        if (!in.markSupported()) {
            throw new IOException("The stream does not support mark.");
        }
        boolean isZip = true;
        try {
            in.mark(MAGIC.length);
            for (int i = 0; i < MAGIC.length; i++) {
                if (MAGIC[i] != (byte) in.read()) {
                    isZip = false;
                    break;
                }
            }
            in.reset();
        } catch (IOException e) {
            isZip = false;
        }
        return isZip;
    }
}

Related

  1. isZipped(final File file)
  2. isZipped(InputStream inputStream)
  3. isZipped(String name)
  4. isZippedFile(File file)
  5. isZipStream(ByteArrayInputStream input)
  6. isZipValid(File file)
  7. isZipValid(final File zip)