Java Array Starts With startsWithZipMagicBytes(byte[] data)

Here you can find the source of startsWithZipMagicBytes(byte[] data)

Description

Returns whether the given bytes start with the <a href="http://en.wikipedia.org/wiki/Zip_%28file_format%29#File_headers" >magic bytes</a> that mark a ZIP file.

License

Apache License

Declaration

public static boolean startsWithZipMagicBytes(byte[] data) 

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 the ConQAT Project                                   |
|                                                                          |
| 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.                                           |
+-------------------------------------------------------------------------*/

public class Main {
    /**// w ww  . j a v a2  s .com
     * Returns whether the given bytes start with the <a
     * href="http://en.wikipedia.org/wiki/Zip_%28file_format%29#File_headers"
     * >magic bytes</a> that mark a ZIP file.
     */
    public static boolean startsWithZipMagicBytes(byte[] data) {
        return isPrefix(new byte[] { 0x50, 0x4b, 0x03, 0x04 }, data);
    }

    /** Returns whether the prefix is a prefix of the given key. */
    public static boolean isPrefix(byte[] prefix, byte[] key) {
        if (key.length < prefix.length) {
            return false;
        }
        for (int i = 0; i < prefix.length; ++i) {
            if (prefix[i] != key[i]) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. startsWith(char[] src, char[] find, int startAt)
  2. startsWith(char[] str1, String str2)
  3. startsWith(final byte[] array, final byte[] prefix)
  4. startsWith(final byte[] str, int startIndex, int endIndex, final byte ch)
  5. startsWithGaps(final byte[] aFrag, final int numStartGaps)