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