Java GZip Byte Array Check isGZipped(byte[] bytes)

Here you can find the source of isGZipped(byte[] bytes)

Description

is G Zipped

License

Open Source License

Declaration

public static boolean isGZipped(byte[] bytes) 

Method Source Code


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

import java.io.File;
import java.io.RandomAccessFile;
import java.util.zip.GZIPInputStream;

public class Main {
    public static boolean isGZipped(byte[] bytes) {
        if ((bytes == null) || (bytes.length < 2)) {
            return false;
        } else {/*from  w  w w  . j a  va 2s. c o  m*/
            return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
                    && (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
        }
    }

    public static boolean isGZipped(File file) {
        int magic = 0;

        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
            magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
        } catch (Throwable e) {
            e.printStackTrace(System.err);
        }
        return magic == GZIPInputStream.GZIP_MAGIC;
    }
}

Related

  1. isGzip(byte[] bytes)
  2. isGzipped(byte[] data)