View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.arj;
20  
21  import java.io.File;
22  import java.util.Date;
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  
35      /**
36       * The known values for HostOs.
37       */
38      public static class HostOs {
39  
40          /**
41           * Constant value {@value}.
42           */
43          public static final int DOS = 0;
44  
45          /**
46           * Constant value {@value}.
47           */
48          public static final int PRIMOS = 1;
49  
50          /**
51           * Constant value {@value}.
52           */
53          public static final int UNIX = 2;
54  
55          /**
56           * Constant value {@value}.
57           */
58          public static final int AMIGA = 3;
59  
60          /**
61           * Constant value {@value}.
62           */
63          public static final int MAC_OS = 4;
64  
65          /**
66           * Constant value {@value}.
67           */
68          public static final int OS_2 = 5;
69  
70          /**
71           * Constant value {@value}.
72           */
73          public static final int APPLE_GS = 6;
74  
75          /**
76           * Constant value {@value}.
77           */
78          public static final int ATARI_ST = 7;
79  
80          /**
81           * Constant value {@value}.
82           */
83          public static final int NEXT = 8;
84  
85          /**
86           * Constant value {@value}.
87           */
88          public static final int VAX_VMS = 9;
89  
90          /**
91           * Constant value {@value}.
92           */
93          public static final int WIN95 = 10;
94  
95          /**
96           * Constant value {@value}.
97           */
98          public static final int WIN32 = 11;
99  
100         /**
101          * Constructs a new instance.
102          *
103          * @deprecated Will be private in the next major release.
104          */
105         @Deprecated
106         public HostOs() {
107             // empty
108         }
109     }
110 
111     private final LocalFileHeader localFileHeader;
112 
113     /**
114      * Constructs a new instance.
115      */
116     public ArjArchiveEntry() {
117         localFileHeader = new LocalFileHeader();
118     }
119 
120     /**
121      * Constructs a new instance.
122      */
123     ArjArchiveEntry(final LocalFileHeader localFileHeader) {
124         this.localFileHeader = localFileHeader;
125     }
126 
127     @Override
128     public boolean equals(final Object obj) {
129         if (this == obj) {
130             return true;
131         }
132         if (obj == null || getClass() != obj.getClass()) {
133             return false;
134         }
135         final ArjArchiveEntry other = (ArjArchiveEntry) obj;
136         return localFileHeader.equals(other.localFileHeader);
137     }
138 
139     /**
140      * The operating system the archive has been created on.
141      *
142      * @see HostOs
143      * @return the host OS code
144      */
145     public int getHostOs() {
146         return localFileHeader.hostOS;
147     }
148 
149     /**
150      * The last modified date of the entry.
151      *
152      * <p>
153      * Note the interpretation of time is different depending on the HostOS that has created the archive. While an OS that is {@link #isHostOsUnix considered to
154      * be Unix} stores time in a time zone independent manner, other platforms only use the local time. I.e. if an archive has been created at midnight UTC on a
155      * machine in time zone UTC this method will return midnight regardless of time zone if the archive has been created on a non-Unix system and a time taking
156      * the current time zone into account if the archive has been created on Unix.
157      * </p>
158      *
159      * @return the last modified date
160      */
161     @Override
162     public Date getLastModifiedDate() {
163         final long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000L : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified);
164         return new Date(ts);
165     }
166 
167     int getMethod() {
168         return localFileHeader.method;
169     }
170 
171     /**
172      * File mode of this entry.
173      *
174      * <p>
175      * The format depends on the host os that created the entry.
176      * </p>
177      *
178      * @return the file mode
179      */
180     public int getMode() {
181         return localFileHeader.fileAccessMode;
182     }
183 
184     /**
185      * Gets this entry's name.
186      *
187      * <p>
188      * This method returns the raw name as it is stored inside of the archive.
189      * </p>
190      *
191      * @return This entry's name.
192      */
193     @Override
194     public String getName() {
195         if ((localFileHeader.arjFlags & LocalFileHeader.Flags.PATHSYM) != 0) {
196             return localFileHeader.name.replace("/", File.separator);
197         }
198         return localFileHeader.name;
199     }
200 
201     /**
202      * Gets this entry's file size.
203      *
204      * @return This entry's file size.
205      */
206     @Override
207     public long getSize() {
208         return localFileHeader.originalSize;
209     }
210 
211     /**
212      * File mode of this entry as Unix stat value.
213      *
214      * <p>
215      * Will only be non-zero of the host os was Unix.
216      *
217      * @return the Unix mode
218      */
219     public int getUnixMode() {
220         return isHostOsUnix() ? getMode() : 0;
221     }
222 
223     @Override
224     public int hashCode() {
225         final String name = getName();
226         return name == null ? 0 : name.hashCode();
227     }
228 
229     /**
230      * True if the entry refers to a directory.
231      *
232      * @return True if the entry refers to a directory
233      */
234     @Override
235     public boolean isDirectory() {
236         return localFileHeader.fileType == LocalFileHeader.FileTypes.DIRECTORY;
237     }
238 
239     /**
240      * Is the operating system the archive has been created on one that is considered a Unix OS by arj?
241      *
242      * @return whether the operating system the archive has been created on is considered a Unix OS by arj
243      */
244     public boolean isHostOsUnix() {
245         return getHostOs() == HostOs.UNIX || getHostOs() == HostOs.NEXT;
246     }
247 
248 }