2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.compress.archivers.zip;
21
* Parser/encoder for the "general purpose bit" field in ZIP's local
22
* file and central directory headers.
23
* @since Apache Commons Compress 1.1
26
public final class GeneralPurposeBit {
28
* Indicates that the file is encrypted.
30
private static final int ENCRYPTION_FLAG = 1 << 0;
33
* Indicates that a data descriptor stored after the file contents
34
* will hold CRC and size information.
36
private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
39
* Indicates strong encryption.
41
private static final int STRONG_ENCRYPTION_FLAG = 1 << 6;
44
* Indicates that filenames are written in utf-8.
46
* <p>The only reason this is public is that {@link
47
* ZipArchiveOutputStream#EFS_FLAG} was public in Apache Commons
48
* Compress 1.0 and we needed a substitute for it.</p>
50
public static final int UFT8_NAMES_FLAG = 1 << 11;
52
private boolean languageEncodingFlag = false;
53
private boolean dataDescriptorFlag = false;
54
private boolean encryptionFlag = false;
55
private boolean strongEncryptionFlag = false;
57
public GeneralPurposeBit() {
61
* whether the current entry uses UTF8 for file name and comment.
63
public boolean usesUTF8ForNames() {
64
return languageEncodingFlag;
68
* whether the current entry will use UTF8 for file name and comment.
70
public void useUTF8ForNames(boolean b) {
71
languageEncodingFlag = b;
75
* whether the current entry uses the data descriptor to store CRC
76
* and size information
78
public boolean usesDataDescriptor() {
79
return dataDescriptorFlag;
83
* whether the current entry will use the data descriptor to store
84
* CRC and size information
86
public void useDataDescriptor(boolean b) {
87
dataDescriptorFlag = b;
91
* whether the current entry is encrypted
93
public boolean usesEncryption() {
94
return encryptionFlag;
98
* whether the current entry will be encrypted
100
public void useEncryption(boolean b) {
105
* whether the current entry is encrypted using strong encryption
107
public boolean usesStrongEncryption() {
108
return encryptionFlag && strongEncryptionFlag;
112
* whether the current entry will be encrypted using strong encryption
114
public void useStrongEncryption(boolean b) {
115
strongEncryptionFlag = b;
122
* Encodes the set bits in a form suitable for ZIP archives.
124
public byte[] encode() {
126
ZipShort.getBytes((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
128
(languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
130
(encryptionFlag ? ENCRYPTION_FLAG : 0)
132
(strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0)
137
* Parses the supported flags from the given archive data.
138
* @param data local file header or a central directory entry.
139
* @param offset offset at which the general purpose bit starts
141
public static GeneralPurposeBit parse(final byte[] data, final int offset) {
142
final int generalPurposeFlag = ZipShort.getValue(data, offset);
143
GeneralPurposeBit b = new GeneralPurposeBit();
144
b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
145
b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
146
b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG)
148
b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
152
public int hashCode() {
153
return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0)
154
+ (strongEncryptionFlag ? 1 : 0))
155
+ (languageEncodingFlag ? 1 : 0))
156
+ (dataDescriptorFlag ? 1 : 0));
159
public boolean equals(Object o) {
160
if (!(o instanceof GeneralPurposeBit)) {
163
GeneralPurposeBit g = (GeneralPurposeBit) o;
164
return g.encryptionFlag == encryptionFlag
165
&& g.strongEncryptionFlag == strongEncryptionFlag
166
&& g.languageEncodingFlag == languageEncodingFlag
167
&& g.dataDescriptorFlag == dataDescriptorFlag;