Java GZip File Check isGzipped(final File file)

Here you can find the source of isGzipped(final File file)

Description

Checks if the file is gzipped.

License

Open Source License

Parameter

Parameter Description
file The file to be tested

Exception

Parameter Description
IOException if file does not exist or isn't gzipped.

Return

true if file is gzipped, and false otherwise

Declaration

public static boolean isGzipped(final File file) throws IOException 

Method Source Code


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

import java.io.File;

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

public class Main {
    /**/*from  w w  w  .j av  a 2  s.c  o  m*/
     * Checks if the file is gzipped.
     * @param file The file to be tested
     * @throws IOException if file does not exist or isn't gzipped.
     * @return true if  {@code file} is gzipped, and false otherwise
     */
    public static boolean isGzipped(final File file) throws IOException {
        int magic = 0;
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
        raf.close();
        return magic == GZIPInputStream.GZIP_MAGIC;
    }
}

Related

  1. isGZIPFileEmpty(File f)
  2. isGzipFilename(File file)
  3. isGzipped(File f)
  4. isGzipped(File file)
  5. isGZipped(final File f)
  6. isGZipped(final File file)
  7. isGzipped(String fileName)
  8. isGZipped(String name)