TarArchiveSparseEntry.java

  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.tar;

  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;

  23. /**
  24.  * A sparse entry in a <a href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">Tar archive</a>.
  25.  *
  26.  * <p>
  27.  * The C structure for a sparse entry is:
  28.  *
  29.  * <pre>
  30.  * struct posix_header {
  31.  * struct sparse sp[21]; // TarConstants.SPARSELEN_GNU_SPARSE     - offset 0
  32.  * char isextended;      // TarConstants.ISEXTENDEDLEN_GNU_SPARSE - offset 504
  33.  * };
  34.  * </pre>
  35.  *
  36.  * Whereas, "struct sparse" is:
  37.  *
  38.  * <pre>
  39.  * struct sparse {
  40.  * char offset[12];   // offset 0
  41.  * char numbytes[12]; // offset 12
  42.  * };
  43.  * </pre>
  44.  *
  45.  * <p>
  46.  * Each such struct describes a block of data that has actually been written to the archive. The offset describes where in the extracted file the data is
  47.  * supposed to start and the numbytes provides the length of the block. When extracting the entry the gaps between the sparse structs are equivalent to areas
  48.  * filled with zero bytes.
  49.  * </p>
  50.  */

  51. public class TarArchiveSparseEntry implements TarConstants {
  52.     /** If an extension sparse header follows. */
  53.     private final boolean isExtended;

  54.     private final List<TarArchiveStructSparse> sparseHeaders;

  55.     /**
  56.      * Constructs an entry from an archive's header bytes. File is set to null.
  57.      *
  58.      * @param headerBuf The header bytes from a tar archive entry.
  59.      * @throws IOException on unknown format
  60.      */
  61.     public TarArchiveSparseEntry(final byte[] headerBuf) throws IOException {
  62.         int offset = 0;
  63.         sparseHeaders = new ArrayList<>(TarUtils.readSparseStructs(headerBuf, 0, SPARSE_HEADERS_IN_EXTENSION_HEADER));
  64.         offset += SPARSELEN_GNU_SPARSE;
  65.         isExtended = TarUtils.parseBoolean(headerBuf, offset);
  66.     }

  67.     /**
  68.      * Gets information about the configuration for the sparse entry.
  69.      *
  70.      * @since 1.20
  71.      * @return information about the configuration for the sparse entry.
  72.      */
  73.     public List<TarArchiveStructSparse> getSparseHeaders() {
  74.         return sparseHeaders;
  75.     }

  76.     public boolean isExtended() {
  77.         return isExtended;
  78.     }
  79. }