Java GZip File Check isGzipped(String fileName)

Here you can find the source of isGzipped(String fileName)

Description

Tests if a file has been compressed using gzip.

License

Open Source License

Parameter

Parameter Description
fileName the name of the file to be tested

Return

true if the file has been gzip-ed, false otherwise

Declaration

public static boolean isGzipped(String fileName) throws IOException 

Method Source Code


//package com.java2s;
/*// w  w  w .  jav  a 2s. com
 *    Qizx/open 4.1
 *
 * This code is the open-source version of Qizx.
 * Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License 
 *  Version 1.1 (the "License"); you may not use this file except in 
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 * The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
 *
 */

import java.io.*;

public class Main {
    /**
     * Tests if a file has been compressed using gzip.
     * 
     * @param fileName the name of the file to be tested
     * @return <code>true</code> if the file has been gzip-ed,
     * <code>false</code> otherwise
     * @exception IOException if there is an IO problem
     */
    public static boolean isGzipped(String fileName) throws IOException {
        return isGzipped(new File(fileName));
    }

    /**
     * Tests if a file has been compressed using gzip.
     * 
     * @param file the file to be tested
     * @return <code>true</code> if the file has been gzip-ed,
     * <code>false</code> otherwise
     * @exception IOException if there is an IO problem
     */
    public static boolean isGzipped(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        int magic1 = in.read();
        int magic2 = in.read();
        in.close();
        return (magic1 == 0037 && magic2 == 0213);
    }
}

Related

  1. isGzipped(File f)
  2. isGzipped(File file)
  3. isGZipped(final File f)
  4. isGzipped(final File file)
  5. isGZipped(final File file)
  6. isGZipped(String name)