• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.commons.compress.archivers.zip;
20 
21 /**
22  * Simple placeholder for all those extra fields we don't want to deal
23  * with.
24  *
25  * <p>Assumes local file data and central directory entries are
26  * identical - unless told the opposite.</p>
27  * @NotThreadSafe
28  */
29 public class UnrecognizedExtraField implements ZipExtraField {
30 
31     /**
32      * The Header-ID.
33      */
34     private ZipShort headerId;
35 
36     /**
37      * Set the header id.
38      * @param headerId the header id to use
39      */
setHeaderId(final ZipShort headerId)40     public void setHeaderId(final ZipShort headerId) {
41         this.headerId = headerId;
42     }
43 
44     /**
45      * Get the header id.
46      * @return the header id
47      */
48     @Override
getHeaderId()49     public ZipShort getHeaderId() {
50         return headerId;
51     }
52 
53     /**
54      * Extra field data in local file data - without
55      * Header-ID or length specifier.
56      */
57     private byte[] localData;
58 
59     /**
60      * Set the extra field data in the local file data -
61      * without Header-ID or length specifier.
62      * @param data the field data to use
63      */
setLocalFileDataData(final byte[] data)64     public void setLocalFileDataData(final byte[] data) {
65         localData = ZipUtil.copy(data);
66     }
67 
68     /**
69      * Get the length of the local data.
70      * @return the length of the local data
71      */
72     @Override
getLocalFileDataLength()73     public ZipShort getLocalFileDataLength() {
74         return new ZipShort(localData != null ? localData.length : 0);
75     }
76 
77     /**
78      * Get the local data.
79      * @return the local data
80      */
81     @Override
getLocalFileDataData()82     public byte[] getLocalFileDataData() {
83         return ZipUtil.copy(localData);
84     }
85 
86     /**
87      * Extra field data in central directory - without
88      * Header-ID or length specifier.
89      */
90     private byte[] centralData;
91 
92     /**
93      * Set the extra field data in central directory.
94      * @param data the data to use
95      */
setCentralDirectoryData(final byte[] data)96     public void setCentralDirectoryData(final byte[] data) {
97         centralData = ZipUtil.copy(data);
98     }
99 
100     /**
101      * Get the central data length.
102      * If there is no central data, get the local file data length.
103      * @return the central data length
104      */
105     @Override
getCentralDirectoryLength()106     public ZipShort getCentralDirectoryLength() {
107         if (centralData != null) {
108             return new ZipShort(centralData.length);
109         }
110         return getLocalFileDataLength();
111     }
112 
113     /**
114      * Get the central data.
115      * @return the central data if present, else return the local file data
116      */
117     @Override
getCentralDirectoryData()118     public byte[] getCentralDirectoryData() {
119         if (centralData != null) {
120             return ZipUtil.copy(centralData);
121         }
122         return getLocalFileDataData();
123     }
124 
125     /**
126      * @param data the array of bytes.
127      * @param offset the source location in the data array.
128      * @param length the number of bytes to use in the data array.
129      * @see ZipExtraField#parseFromLocalFileData(byte[], int, int)
130      */
131     @Override
parseFromLocalFileData(final byte[] data, final int offset, final int length)132     public void parseFromLocalFileData(final byte[] data, final int offset, final int length) {
133         final byte[] tmp = new byte[length];
134         System.arraycopy(data, offset, tmp, 0, length);
135         setLocalFileDataData(tmp);
136     }
137 
138     /**
139      * @param data the array of bytes.
140      * @param offset the source location in the data array.
141      * @param length the number of bytes to use in the data array.
142      * @see ZipExtraField#parseFromCentralDirectoryData(byte[], int, int)
143      */
144     @Override
parseFromCentralDirectoryData(final byte[] data, final int offset, final int length)145     public void parseFromCentralDirectoryData(final byte[] data, final int offset,
146                                               final int length) {
147         final byte[] tmp = new byte[length];
148         System.arraycopy(data, offset, tmp, 0, length);
149         setCentralDirectoryData(tmp);
150         if (localData == null) {
151             setLocalFileDataData(tmp);
152         }
153     }
154 
155 }
156