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 021 022import java.util.zip.ZipException; 023 024/** 025 * An extra field who's sole purpose is to align and pad the local file header 026 * so that the entry's data starts at a certain position. 027 * 028 * <p>The padding content of the padding is ignored and not retained 029 * when reading a padding field.</p> 030 * 031 * <p>This enables Commons Compress to create "aligned" archives 032 * similar to Android's zipalign command line tool.</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 = 0; 054 055 public ResourceAlignmentExtraField() { 056 } 057 058 public ResourceAlignmentExtraField(int alignment) { 059 this(alignment, false); 060 } 061 062 public ResourceAlignmentExtraField(int alignment, boolean allowMethodChange) { 063 this(alignment, allowMethodChange, 0); 064 } 065 066 public ResourceAlignmentExtraField(int alignment, boolean allowMethodChange, int padding) { 067 if (alignment < 0 || alignment > 0x7fff) { 068 throw new IllegalArgumentException("Alignment must be between 0 and 0x7fff, was: " + alignment); 069 } 070 this.alignment = (short) alignment; 071 this.allowMethodChange = allowMethodChange; 072 this.padding = padding; 073 } 074 075 /** 076 * Gets requested alignment. 077 * 078 * @return 079 * requested alignment. 080 */ 081 public short getAlignment() { 082 return alignment; 083 } 084 085 /** 086 * Indicates whether method change is allowed when re-compressing the zip file. 087 * 088 * @return 089 * true if method change is allowed, false otherwise. 090 */ 091 public boolean allowMethodChange() { 092 return allowMethodChange; 093 } 094 095 @Override 096 public ZipShort getHeaderId() { 097 return ID; 098 } 099 100 @Override 101 public ZipShort getLocalFileDataLength() { 102 return new ZipShort(BASE_SIZE + padding); 103 } 104 105 @Override 106 public ZipShort getCentralDirectoryLength() { 107 return new ZipShort(BASE_SIZE); 108 } 109 110 @Override 111 public byte[] getLocalFileDataData() { 112 byte[] content = new byte[BASE_SIZE + padding]; 113 ZipShort.putShort(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0), 114 content, 0); 115 return content; 116 } 117 118 @Override 119 public byte[] getCentralDirectoryData() { 120 return ZipShort.getBytes(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0)); 121 } 122 123 @Override 124 public void parseFromLocalFileData(byte[] buffer, int offset, int length) throws ZipException { 125 parseFromCentralDirectoryData(buffer, offset, length); 126 this.padding = length - BASE_SIZE; 127 } 128 129 @Override 130 public void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) throws ZipException { 131 if (length < BASE_SIZE) { 132 throw new ZipException("Too short content for ResourceAlignmentExtraField (0xa11e): " + length); 133 } 134 int alignmentValue = ZipShort.getValue(buffer, offset); 135 this.alignment = (short) (alignmentValue & (ALLOW_METHOD_MESSAGE_CHANGE_FLAG - 1)); 136 this.allowMethodChange = (alignmentValue & ALLOW_METHOD_MESSAGE_CHANGE_FLAG) != 0; 137 } 138}