集册 Java实例教程 检查文件是否被压缩。

检查文件是否被压缩。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

599
检查文件是否被压缩。

/*

 * 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.nowjava;
/**
N o w J a v a . c o m - 时  代  Java
**/

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 {
        /*
         from N o w  J a v a  .   c o m 
        */

            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 {

            
展开阅读全文