Java examples for File Path IO:GZIP
Checks if a file is gzipped.
/*//from www . j a va 2 s. com * Copyright 2014 davidherod. * * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //package com.java2s; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.util.zip.GZIPInputStream; public class Main { public static boolean isGZipped(InputStream in) { if (!in.markSupported()) { in = new BufferedInputStream(in); } in.mark(2); int magic = 0; try { magic = in.read() & 0xff | ((in.read() << 8) & 0xff00); in.reset(); } catch (IOException e) { e.printStackTrace(System.err); return false; } return magic == GZIPInputStream.GZIP_MAGIC; } /** * Checks if a file is gzipped. * * @param f * @return */ public static boolean isGZipped(File f) { int magic = 0; try { RandomAccessFile raf = new RandomAccessFile(f, "r"); magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00); raf.close(); } catch (Throwable e) { e.printStackTrace(System.err); } return magic == GZIPInputStream.GZIP_MAGIC; } }