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 * Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data. 23 * 24 * <p>The header-id is artificial (and not listed as a known ID in <a 25 * href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>). Since it isn't used anywhere 26 * except to satisfy the ZipExtraField contract it shouldn't matter anyway.</p> 27 * 28 * @since 1.1 29 * @NotThreadSafe 30 */ 31 public final class UnparseableExtraFieldData implements ZipExtraField { 32 private static final ZipShort HEADER_ID = new ZipShort(0xACC1); 33 34 private byte[] localFileData; 35 private byte[] centralDirectoryData; 36 37 /** 38 * The Header-ID. 39 * 40 * @return a completely arbitrary value that should be ignored. 41 */ 42 @Override getHeaderId()43 public ZipShort getHeaderId() { 44 return HEADER_ID; 45 } 46 47 /** 48 * Length of the complete extra field in the local file data. 49 * 50 * @return The LocalFileDataLength value 51 */ 52 @Override getLocalFileDataLength()53 public ZipShort getLocalFileDataLength() { 54 return new ZipShort(localFileData == null ? 0 : localFileData.length); 55 } 56 57 /** 58 * Length of the complete extra field in the central directory. 59 * 60 * @return The CentralDirectoryLength value 61 */ 62 @Override getCentralDirectoryLength()63 public ZipShort getCentralDirectoryLength() { 64 return centralDirectoryData == null 65 ? getLocalFileDataLength() 66 : new ZipShort(centralDirectoryData.length); 67 } 68 69 /** 70 * The actual data to put into local file data. 71 * 72 * @return The LocalFileDataData value 73 */ 74 @Override getLocalFileDataData()75 public byte[] getLocalFileDataData() { 76 return ZipUtil.copy(localFileData); 77 } 78 79 /** 80 * The actual data to put into central directory. 81 * 82 * @return The CentralDirectoryData value 83 */ 84 @Override getCentralDirectoryData()85 public byte[] getCentralDirectoryData() { 86 return centralDirectoryData == null 87 ? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData); 88 } 89 90 /** 91 * Populate data from this array as if it was in local file data. 92 * 93 * @param buffer the buffer to read data from 94 * @param offset offset into buffer to read data 95 * @param length the length of data 96 */ 97 @Override parseFromLocalFileData(final byte[] buffer, final int offset, final int length)98 public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length) { 99 localFileData = new byte[length]; 100 System.arraycopy(buffer, offset, localFileData, 0, length); 101 } 102 103 /** 104 * Populate data from this array as if it was in central directory data. 105 * 106 * @param buffer the buffer to read data from 107 * @param offset offset into buffer to read data 108 * @param length the length of data 109 */ 110 @Override parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length)111 public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, 112 final int length) { 113 centralDirectoryData = new byte[length]; 114 System.arraycopy(buffer, offset, centralDirectoryData, 0, length); 115 if (localFileData == null) { 116 parseFromLocalFileData(buffer, offset, length); 117 } 118 } 119 120 } 121