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    public String getName() {
050        if ((localFileHeader.arjFlags & LocalFileHeader.Flags.PATHSYM) != 0) {
051            return localFileHeader.name.replaceAll("/",
052                    Matcher.quoteReplacement(File.separator));
053        } else {
054            return localFileHeader.name;
055        }
056    }
057
058    /**
059     * Get this entry's file size.
060     *
061     * @return This entry's file size.
062     */
063    public long getSize() {
064        return localFileHeader.originalSize;
065    }
066
067    /** True if the entry refers to a directory */
068    public boolean isDirectory() {
069        return localFileHeader.fileType == LocalFileHeader.FileTypes.DIRECTORY;
070    }
071
072    /**
073     * The last modified date of the entry.
074     *
075     * <p>Note the interpretation of time is different depending on
076     * the HostOS that has created the archive.  While an OS that is
077     * {@link #isHostOsUnix considered to be Unix} stores time in a
078     * timezone independent manner, other platforms only use the local
079     * time.  I.e. if an archive has been created at midnight UTC on a
080     * machine in timezone UTC this method will return midnight
081     * regardless of timezone if the archive has been created on a
082     * non-Unix system and a time taking the current timezone into
083     * account if the archive has beeen created on Unix.</p>
084     */
085    public Date getLastModifiedDate() {
086        long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000l
087            : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified);
088        return new Date(ts);
089    }
090
091    /**
092     * File mode of this entry.
093     *
094     * <p>The format depends on the host os that created the entry.</p>
095     */
096    public int getMode() {
097        return localFileHeader.fileAccessMode;
098    }
099
100    /**
101     * File mode of this entry as Unix stat value.
102     *
103     * <p>Will only be non-zero of the host os was UNIX.
104     */
105    public int getUnixMode() {
106        return isHostOsUnix() ? getMode() : 0;
107    }
108
109    /**
110     * The operating system the archive has been created on.
111     * @see HostOs
112     */
113    public int getHostOs() {
114        return localFileHeader.hostOS;
115    }
116
117    /**
118     * Is the operating system the archive has been created on one
119     * that is considered a UNIX OS by arj?
120     */
121    public boolean isHostOsUnix() {
122        return getHostOs() == HostOs.UNIX || getHostOs() == HostOs.NEXT;
123    }
124
125    int getMethod() {
126        return localFileHeader.method;
127    }
128
129    /**
130     * The known values for HostOs.
131     */
132    public static class HostOs {
133        public static final int DOS = 0;
134        public static final int PRIMOS = 1;
135        public static final int UNIX = 2;
136        public static final int AMIGA = 3;
137        public static final int MAC_OS = 4;
138        public static final int OS_2 = 5;
139        public static final int APPLE_GS = 6;
140        public static final int ATARI_ST = 7;
141        public static final int NEXT = 8;
142        public static final int VAX_VMS = 9;
143        public static final int WIN95 = 10;
144        public static final int WIN32 = 11;
145    }
146    
147}