    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.archivers.zip;
    020
    021import java.util.zip.ZipException;
    022
    023/**
    024 * An extra field who's sole purpose is to align and pad the local file header so that the entry's data starts at a certain position.
    025 *
    026 * <p>
    027 * The padding content of the padding is ignored and not retained when reading a padding field.
    028 * </p>
    029 *
    030 * <p>
    031 * This enables Commons Compress to create "aligned" archives similar to Android's {@code zipalign} command line tool.
    032 * </p>
    033 *
    034 * @since 1.14
    035 * @see "https://developer.android.com/studio/command-line/zipalign.html"
    036 * @see ZipArchiveEntry#setAlignment
    037 */
    038public class ResourceAlignmentExtraField implements ZipExtraField {
    039
    040    /**
    041     * Extra field id used for storing alignment and padding.
    042     */
    043    public static final ZipShort ID = new ZipShort(0xa11e);
    044
    045    public static final int BASE_SIZE = 2;
    046
    047    private static final int ALLOW_METHOD_MESSAGE_CHANGE_FLAG = 0x8000;
    048
    049    private short alignment;
    050
    051    private boolean allowMethodChange;
    052
    053    private int padding;
    054
    055    public ResourceAlignmentExtraField() {
    056    }
    057
    058    public ResourceAlignmentExtraField(final int alignment) {
    059        this(alignment, false);
    060    }
    061
    062    public ResourceAlignmentExtraField(final int alignment, final boolean allowMethodChange) {
    063        this(alignment, allowMethodChange, 0);
    064    }
    065
    066    public ResourceAlignmentExtraField(final int alignment, final boolean allowMethodChange, final int padding) {
    067        if (alignment < 0 || alignment > 0x7fff) {
    068            throw new IllegalArgumentException("Alignment must be between 0 and 0x7fff, was: " + alignment);
    069        }
    070        if (padding < 0) {
    071            throw new IllegalArgumentException("Padding must not be negative, was: " + padding);
    072        }
    073        this.alignment = (short) alignment;
    074        this.allowMethodChange = allowMethodChange;
    075        this.padding = padding;
    076    }
    077
    078    /**
    079     * Indicates whether method change is allowed when re-compressing the ZIP file.
    080     *
    081     * @return true if method change is allowed, false otherwise.
    082     */
    083    public boolean allowMethodChange() {
    084        return allowMethodChange;
    085    }
    086
    087    /**
    088     * Gets requested alignment.
    089     *
    090     * @return requested alignment.
    091     */
    092    public short getAlignment() {
    093        return alignment;
    094    }
    095
    096    @Override
    097    public byte[] getCentralDirectoryData() {
    098        return ZipShort.getBytes(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0));
    099    }
    100
    101    @Override
    102    public ZipShort getCentralDirectoryLength() {
    103        return new ZipShort(BASE_SIZE);
    104    }
    105
    106    @Override
    107    public ZipShort getHeaderId() {
    108        return ID;
    109    }
    110
    111    @Override
    112    public byte[] getLocalFileDataData() {
    113        final byte[] content = new byte[BASE_SIZE + padding];
    114        ZipShort.putShort(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0), content, 0);
    115        return content;
    116    }
    117
    118    @Override
    119    public ZipShort getLocalFileDataLength() {
    120        return new ZipShort(BASE_SIZE + padding);
    121    }
    122
    123    @Override
    124    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
    125        if (length < BASE_SIZE) {
    126            throw new ZipException("Too short content for ResourceAlignmentExtraField (0xa11e): " + length);
    127        }
    128        final int alignmentValue = ZipShort.getValue(buffer, offset);
    129        this.alignment = (short) (alignmentValue & ALLOW_METHOD_MESSAGE_CHANGE_FLAG - 1);
    130        this.allowMethodChange = (alignmentValue & ALLOW_METHOD_MESSAGE_CHANGE_FLAG) != 0;
    131    }
    132
    133    @Override
    134    public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length) throws ZipException {
    135        parseFromCentralDirectoryData(buffer, offset, length);
    136        this.padding = length - BASE_SIZE;
    137    }
    138}