Java Zip File Check isZipFile(String fileName)

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

Description

is Zip File

License

Apache License

Declaration

static public boolean isZipFile(String fileName) 

Method Source Code

//package com.java2s;
/*//w w  w. j  av a 2  s. c om
 *  Copyright 2015 the original author or authors. 
 *  @https://github.com/scouter-project/scouter
 *
 *  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.
 *
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

public class Main {
    static public boolean isZipFile(String fileName) {
        if (fileName == null) {
            return false;
        }
        File file = new File(fileName);
        if (!file.exists() || !file.isFile()) {
            return false;
        }
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] data = new byte[2];
            in.read(data);
            if (data[0] == 0x50 && data[1] == 0x4b) {
                return true;
            }
        } catch (Exception ex) {
            return false;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }
        return false;
    }
}

Related

  1. isZipFile(File file)
  2. isZipFile(File file)
  3. isZipFile(InputStream stream)
  4. isZipFile(java.io.File file)
  5. isZipFile(String fileName)
  6. isZipFile(String path)
  7. isZipOrJarArchive(File file)
  8. isZipped(File f)
  9. isZipped(File file)