ArjArchiveEntry.java

  1. /*
  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
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  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.
  16.  */
  17. package org.apache.commons.compress.archivers.arj;

  18. import java.io.File;
  19. import java.util.Date;

  20. import org.apache.commons.compress.archivers.ArchiveEntry;
  21. import org.apache.commons.compress.archivers.zip.ZipUtil;

  22. /**
  23.  * An entry in an ARJ archive.
  24.  *
  25.  * @NotThreadSafe
  26.  * @since 1.6
  27.  */
  28. public class ArjArchiveEntry implements ArchiveEntry {

  29.     /**
  30.      * The known values for HostOs.
  31.      */
  32.     public static class HostOs {

  33.         /**
  34.          * {@value}
  35.          */
  36.         public static final int DOS = 0;

  37.         /**
  38.          * {@value}
  39.          */
  40.         public static final int PRIMOS = 1;

  41.         /**
  42.          * {@value}
  43.          */
  44.         public static final int UNIX = 2;

  45.         /**
  46.          * {@value}
  47.          */
  48.         public static final int AMIGA = 3;

  49.         /**
  50.          * {@value}
  51.          */
  52.         public static final int MAC_OS = 4;

  53.         /**
  54.          * {@value}
  55.          */
  56.         public static final int OS_2 = 5;

  57.         /**
  58.          * {@value}
  59.          */
  60.         public static final int APPLE_GS = 6;

  61.         /**
  62.          * {@value}
  63.          */
  64.         public static final int ATARI_ST = 7;

  65.         /**
  66.          * {@value}
  67.          */
  68.         public static final int NEXT = 8;

  69.         /**
  70.          * {@value}
  71.          */
  72.         public static final int VAX_VMS = 9;

  73.         /**
  74.          * {@value}
  75.          */
  76.         public static final int WIN95 = 10;

  77.         /**
  78.          * {@value}
  79.          */
  80.         public static final int WIN32 = 11;
  81.     }

  82.     private final LocalFileHeader localFileHeader;

  83.     public ArjArchiveEntry() {
  84.         localFileHeader = new LocalFileHeader();
  85.     }

  86.     ArjArchiveEntry(final LocalFileHeader localFileHeader) {
  87.         this.localFileHeader = localFileHeader;
  88.     }

  89.     @Override
  90.     public boolean equals(final Object obj) {
  91.         if (this == obj) {
  92.             return true;
  93.         }
  94.         if (obj == null || getClass() != obj.getClass()) {
  95.             return false;
  96.         }
  97.         final ArjArchiveEntry other = (ArjArchiveEntry) obj;
  98.         return localFileHeader.equals(other.localFileHeader);
  99.     }

  100.     /**
  101.      * The operating system the archive has been created on.
  102.      *
  103.      * @see HostOs
  104.      * @return the host OS code
  105.      */
  106.     public int getHostOs() {
  107.         return localFileHeader.hostOS;
  108.     }

  109.     /**
  110.      * The last modified date of the entry.
  111.      *
  112.      * <p>
  113.      * Note the interpretation of time is different depending on the HostOS that has created the archive. While an OS that is {@link #isHostOsUnix considered to
  114.      * be Unix} stores time in a time zone independent manner, other platforms only use the local time. I.e. if an archive has been created at midnight UTC on a
  115.      * machine in time zone UTC this method will return midnight regardless of time zone if the archive has been created on a non-UNIX system and a time taking
  116.      * the current time zone into account if the archive has been created on Unix.
  117.      * </p>
  118.      *
  119.      * @return the last modified date
  120.      */
  121.     @Override
  122.     public Date getLastModifiedDate() {
  123.         final long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000L : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified);
  124.         return new Date(ts);
  125.     }

  126.     int getMethod() {
  127.         return localFileHeader.method;
  128.     }

  129.     /**
  130.      * File mode of this entry.
  131.      *
  132.      * <p>
  133.      * The format depends on the host os that created the entry.
  134.      * </p>
  135.      *
  136.      * @return the file mode
  137.      */
  138.     public int getMode() {
  139.         return localFileHeader.fileAccessMode;
  140.     }

  141.     /**
  142.      * Gets this entry's name.
  143.      *
  144.      * <p>
  145.      * This method returns the raw name as it is stored inside of the archive.
  146.      * </p>
  147.      *
  148.      * @return This entry's name.
  149.      */
  150.     @Override
  151.     public String getName() {
  152.         if ((localFileHeader.arjFlags & LocalFileHeader.Flags.PATHSYM) != 0) {
  153.             return localFileHeader.name.replace("/", File.separator);
  154.         }
  155.         return localFileHeader.name;
  156.     }

  157.     /**
  158.      * Gets this entry's file size.
  159.      *
  160.      * @return This entry's file size.
  161.      */
  162.     @Override
  163.     public long getSize() {
  164.         return localFileHeader.originalSize;
  165.     }

  166.     /**
  167.      * File mode of this entry as UNIX stat value.
  168.      *
  169.      * <p>
  170.      * Will only be non-zero of the host os was UNIX.
  171.      *
  172.      * @return the UNIX mode
  173.      */
  174.     public int getUnixMode() {
  175.         return isHostOsUnix() ? getMode() : 0;
  176.     }

  177.     @Override
  178.     public int hashCode() {
  179.         final String name = getName();
  180.         return name == null ? 0 : name.hashCode();
  181.     }

  182.     /**
  183.      * True if the entry refers to a directory.
  184.      *
  185.      * @return True if the entry refers to a directory
  186.      */
  187.     @Override
  188.     public boolean isDirectory() {
  189.         return localFileHeader.fileType == LocalFileHeader.FileTypes.DIRECTORY;
  190.     }

  191.     /**
  192.      * Is the operating system the archive has been created on one that is considered a UNIX OS by arj?
  193.      *
  194.      * @return whether the operating system the archive has been created on is considered a UNIX OS by arj
  195.      */
  196.     public boolean isHostOsUnix() {
  197.         return getHostOs() == HostOs.UNIX || getHostOs() == HostOs.NEXT;
  198.     }

  199. }