Java GZip File Check isGzip(File file)

Here you can find the source of isGzip(File file)

Description

Gets if the file is compressed with gzip.

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

gzip

Declaration

private static boolean isGzip(File file) 

Method Source Code

//package com.java2s;
/**//from   w w  w .jav  a  2s. c o  m
 * This software is part of the MobileTools
 * <p>
 * MobileTools 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, either version 3 of the License, or
 * any later version.
 * <p>
 * MobileTools 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.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with MobileTools. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Gets if the file is compressed with gzip.
     * @param file
     * @return gzip
     */
    private static boolean isGzip(File file) {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            int magic = raf.read() & 0xff | (raf.read() << 8) & 0xff00;
            raf.close();
            return magic == GZIPInputStream.GZIP_MAGIC;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}

Related

  1. isGzip(String file)
  2. isGZipEnding(File file)
  3. isGzipFile(File f)
  4. isGZipFile(File file)