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