    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.commons.compress.archivers.arj;
    019
    020import java.io.File;
    021import java.util.Date;
    022import java.util.regex.Matcher;
    023
    024import org.apache.commons.compress.archivers.ArchiveEntry;
    025import org.apache.commons.compress.archivers.zip.ZipUtil;
    026
    027/**
    028 * An entry in an ARJ archive.
    029 *
    030 * @NotThreadSafe
    031 * @since 1.6
    032 */
    033public class ArjArchiveEntry implements ArchiveEntry {
    034    private final LocalFileHeader localFileHeader;
    035
    036    public ArjArchiveEntry() {
    037        localFileHeader = new LocalFileHeader();
    038    }
    039
    040    ArjArchiveEntry(final LocalFileHeader localFileHeader) {
    041        this.localFileHeader = localFileHeader;
    042    }
    043
    044    /**
    045     * Get this entry's name.
    046     *
    047     * @return This entry's name.
    048     */
    049    @Override
    050    public String getName() {
    051        if ((localFileHeader.arjFlags & LocalFileHeader.Flags.PATHSYM) != 0) {
    052            return localFileHeader.name.replaceAll("/",
    053                    Matcher.quoteReplacement(File.separator));
    054        }
    055        return localFileHeader.name;
    056    }
    057
    058    /**
    059     * Get this entry's file size.
    060     *
    061     * @return This entry's file size.
    062     */
    063    @Override
    064    public long getSize() {
    065        return localFileHeader.originalSize;
    066    }
    067
    068    /** True if the entry refers to a directory.
    069     *
    070     * @return True if the entry refers to a directory
    071     */
    072    @Override
    073    public boolean isDirectory() {
    074        return localFileHeader.fileType == LocalFileHeader.FileTypes.DIRECTORY;
    075    }
    076
    077    /**
    078     * The last modified date of the entry.
    079     *
    080     * <p>Note the interpretation of time is different depending on
    081     * the HostOS that has created the archive.  While an OS that is
    082     * {@link #isHostOsUnix considered to be Unix} stores time in a
    083     * timezone independent manner, other platforms only use the local
    084     * time.  I.e. if an archive has been created at midnight UTC on a
    085     * machine in timezone UTC this method will return midnight
    086     * regardless of timezone if the archive has been created on a
    087     * non-Unix system and a time taking the current timezone into
    088     * account if the archive has beeen created on Unix.</p>
    089     *
    090     * @return the last modified date
    091     */
    092    @Override
    093    public Date getLastModifiedDate() {
    094        final long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000l
    095            : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified);
    096        return new Date(ts);
    097    }
    098
    099    /**
    100     * File mode of this entry.
    101     *
    102     * <p>The format depends on the host os that created the entry.</p>
    103     *
    104     * @return the file mode
    105     */
    106    public int getMode() {
    107        return localFileHeader.fileAccessMode;
    108    }
    109
    110    /**
    111     * File mode of this entry as Unix stat value.
    112     *
    113     * <p>Will only be non-zero of the host os was UNIX.
    114     *
    115     * @return the Unix mode
    116     */
    117    public int getUnixMode() {
    118        return isHostOsUnix() ? getMode() : 0;
    119    }
    120
    121    /**
    122     * The operating system the archive has been created on.
    123     * @see HostOs
    124     * @return the host OS code
    125     */
    126    public int getHostOs() {
    127        return localFileHeader.hostOS;
    128    }
    129
    130    /**
    131     * Is the operating system the archive has been created on one
    132     * that is considered a UNIX OS by arj?
    133     * @return whether the operating system the archive has been
    134     * created on is considered a UNIX OS by arj
    135     */
    136    public boolean isHostOsUnix() {
    137        return getHostOs() == HostOs.UNIX || getHostOs() == HostOs.NEXT;
    138    }
    139
    140    int getMethod() {
    141        return localFileHeader.method;
    142    }
    143
    144    /**
    145     * The known values for HostOs.
    146     */
    147    public static class HostOs {
    148        public static final int DOS = 0;
    149        public static final int PRIMOS = 1;
    150        public static final int UNIX = 2;
    151        public static final int AMIGA = 3;
    152        public static final int MAC_OS = 4;
    153        public static final int OS_2 = 5;
    154        public static final int APPLE_GS = 6;
    155        public static final int ATARI_ST = 7;
    156        public static final int NEXT = 8;
    157        public static final int VAX_VMS = 9;
    158        public static final int WIN95 = 10;
    159        public static final int WIN32 = 11;
    160    }
    161
    162}