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.tar; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025/** 026 * This class represents a sparse entry in a Tar archive. 027 * 028 * <p> 029 * The C structure for a sparse entry is: 030 * <pre> 031 * struct posix_header { 032 * struct sparse sp[21]; // TarConstants.SPARSELEN_GNU_SPARSE - offset 0 033 * char isextended; // TarConstants.ISEXTENDEDLEN_GNU_SPARSE - offset 504 034 * }; 035 * </pre> 036 * Whereas, "struct sparse" is: 037 * <pre> 038 * struct sparse { 039 * char offset[12]; // offset 0 040 * char numbytes[12]; // offset 12 041 * }; 042 * </pre> 043 */ 044 045public class TarArchiveSparseEntry implements TarConstants { 046 /** If an extension sparse header follows. */ 047 private final boolean isExtended; 048 049 private List<TarArchiveStructSparse> sparseHeaders; 050 051 /** 052 * Construct an entry from an archive's header bytes. File is set 053 * to null. 054 * 055 * @param headerBuf The header bytes from a tar archive entry. 056 * @throws IOException on unknown format 057 */ 058 public TarArchiveSparseEntry(final byte[] headerBuf) throws IOException { 059 int offset = 0; 060 sparseHeaders = new ArrayList<>(); 061 for(int i = 0; i < SPARSE_HEADERS_IN_EXTENSION_HEADER;i++) { 062 TarArchiveStructSparse sparseHeader = TarUtils.parseSparse(headerBuf, 063 offset + i * (SPARSE_OFFSET_LEN + SPARSE_NUMBYTES_LEN)); 064 065 // some sparse headers are empty, we need to skip these sparse headers 066 if(sparseHeader.getOffset() > 0 || sparseHeader.getNumbytes() > 0) { 067 sparseHeaders.add(sparseHeader); 068 } 069 } 070 071 offset += SPARSELEN_GNU_SPARSE; 072 isExtended = TarUtils.parseBoolean(headerBuf, offset); 073 } 074 075 public boolean isExtended() { 076 return isExtended; 077 } 078 079 /** 080 * Obtains information about the configuration for the sparse entry. 081 * @since 1.20 082 * @return information about the configuration for the sparse entry. 083 */ 084 public List<TarArchiveStructSparse> getSparseHeaders() { 085 return sparseHeaders; 086 } 087}