• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 package org.apache.commons.compress.archivers.arj;
19 
20 import java.io.File;
21 import java.util.Date;
22 import java.util.regex.Matcher;
23 
24 import org.apache.commons.compress.archivers.ArchiveEntry;
25 import org.apache.commons.compress.archivers.zip.ZipUtil;
26 
27 /**
28  * An entry in an ARJ archive.
29  *
30  * @NotThreadSafe
31  * @since 1.6
32  */
33 public class ArjArchiveEntry implements ArchiveEntry {
34     private final LocalFileHeader localFileHeader;
35 
ArjArchiveEntry()36     public ArjArchiveEntry() {
37         localFileHeader = new LocalFileHeader();
38     }
39 
ArjArchiveEntry(final LocalFileHeader localFileHeader)40     ArjArchiveEntry(final LocalFileHeader localFileHeader) {
41         this.localFileHeader = localFileHeader;
42     }
43 
44     /**
45      * Get this entry's name.
46      *
47      * <p>This method returns the raw name as it is stored inside of the archive.</p>
48      *
49      * @return This entry's name.
50      */
51     @Override
getName()52     public String getName() {
53         if ((localFileHeader.arjFlags & LocalFileHeader.Flags.PATHSYM) != 0) {
54             return localFileHeader.name.replaceAll("/",
55                     Matcher.quoteReplacement(File.separator));
56         }
57         return localFileHeader.name;
58     }
59 
60     /**
61      * Get this entry's file size.
62      *
63      * @return This entry's file size.
64      */
65     @Override
getSize()66     public long getSize() {
67         return localFileHeader.originalSize;
68     }
69 
70     /** True if the entry refers to a directory.
71      *
72      * @return True if the entry refers to a directory
73      */
74     @Override
isDirectory()75     public boolean isDirectory() {
76         return localFileHeader.fileType == LocalFileHeader.FileTypes.DIRECTORY;
77     }
78 
79     /**
80      * The last modified date of the entry.
81      *
82      * <p>Note the interpretation of time is different depending on
83      * the HostOS that has created the archive.  While an OS that is
84      * {@link #isHostOsUnix considered to be Unix} stores time in a
85      * timezone independent manner, other platforms only use the local
86      * time.  I.e. if an archive has been created at midnight UTC on a
87      * machine in timezone UTC this method will return midnight
88      * regardless of timezone if the archive has been created on a
89      * non-Unix system and a time taking the current timezone into
90      * account if the archive has beeen created on Unix.</p>
91      *
92      * @return the last modified date
93      */
94     @Override
getLastModifiedDate()95     public Date getLastModifiedDate() {
96         final long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000L
97             : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified);
98         return new Date(ts);
99     }
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      */
getMode()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      */
getUnixMode()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      */
getHostOs()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      */
isHostOsUnix()138     public boolean isHostOsUnix() {
139         return getHostOs() == HostOs.UNIX || getHostOs() == HostOs.NEXT;
140     }
141 
getMethod()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 }
165