blob: 871faaa339c99774990a5e9fb843c30299dd7275 [file] [log] [blame]
Torsten Curdtca165392008-07-10 10:17:44 +00001/*
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 * http://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 */
19package org.apache.commons.compress.archivers.ar;
20
Sebastian Bazleyfec51a12009-03-31 00:35:56 +000021import java.io.File;
Stefan Bodewig17ffd7f2009-08-01 14:52:15 +000022import java.util.Date;
Sebastian Bazleyfec51a12009-03-31 00:35:56 +000023
Torsten Curdtca165392008-07-10 10:17:44 +000024import org.apache.commons.compress.archivers.ArchiveEntry;
25
Sebastian Bazley1d3d6ea2009-03-14 02:24:54 +000026/**
27 * Represents an archive entry in the "ar" format.
Sebastian Bazleya91e7c72009-03-27 21:13:51 +000028 *
Christian Grobmeier272aafd2009-04-07 16:56:00 +000029 * Each AR archive starts with "!<arch>" followed by a LF. After these 8 bytes
30 * the archive entries are listed. The format of an entry header is as it follows:
31 *
Sebastian Bazleyb7bcdc22009-04-14 15:49:33 +000032 * <pre>
Sebastian Bazley5dd3a972009-04-14 15:37:46 +000033 * START BYTE END BYTE NAME FORMAT LENGTH
Christian Grobmeier272aafd2009-04-07 16:56:00 +000034 * 0 15 File name ASCII 16
35 * 16 27 Modification timestamp Decimal 12
36 * 28 33 Owner ID Decimal 6
37 * 34 39 Group ID Decimal 6
38 * 40 47 File mode Octal 8
39 * 48 57 File size (bytes) Decimal 10
40 * 58 59 File magic \140\012 2
Sebastian Bazleyb7bcdc22009-04-14 15:49:33 +000041 * </pre>
Christian Grobmeier272aafd2009-04-07 16:56:00 +000042 *
43 * This specifies that an ar archive entry header contains 60 bytes.
44 *
45 * Due to the limitation of the file name length to 16 bytes GNU and BSD has
46 * their own variants of this format. This formats are currently not supported
47 * and file names with a bigger size than 16 bytes are not possible at the
48 * moment.
49 *
Sebastian Bazleya91e7c72009-03-27 21:13:51 +000050 * @Immutable
Sebastian Bazley1d3d6ea2009-03-14 02:24:54 +000051 */
Torsten Curdtca165392008-07-10 10:17:44 +000052public class ArArchiveEntry implements ArchiveEntry {
53
Sebastian Bazley6140bf02009-03-28 15:48:15 +000054 /** The header for each entry */
55 public static final String HEADER = "!<arch>\n";
56
57 /** The trailer for each entry */
58 public static final String TRAILER = "`\012";
59
Sebastian Bazley7ef91072009-04-01 10:09:01 +000060 /**
61 * SVR4/GNU adds a trailing / to names; BSD does not.
62 * They also vary in how names longer than 16 characters are represented.
63 * (Not yet supported by this implementation)
64 */
Sebastian Bazleyfd648e22009-03-28 15:05:30 +000065 private final String name;
Sebastian Bazley5402e0d2009-02-27 01:17:12 +000066 private final int userId;
67 private final int groupId;
68 private final int mode;
Sebastian Bazleyfd648e22009-03-28 15:05:30 +000069 private static final int DEFAULT_MODE = 33188; // = (octal) 0100644
Sebastian Bazley5402e0d2009-02-27 01:17:12 +000070 private final long lastModified;
71 private final long length;
Torsten Curdtca165392008-07-10 10:17:44 +000072
73 public ArArchiveEntry(String name, long length) {
Sebastian Bazleyfd648e22009-03-28 15:05:30 +000074 this(name, length, 0, 0, DEFAULT_MODE, System.currentTimeMillis());
Torsten Curdtca165392008-07-10 10:17:44 +000075 }
76
77 public ArArchiveEntry(String name, long length, int userId, int groupId, int mode, long lastModified) {
78 this.name = name;
79 this.length = length;
80 this.userId = userId;
81 this.groupId = groupId;
82 this.mode = mode;
83 this.lastModified = lastModified;
84 }
85
Sebastian Bazleyfec51a12009-03-31 00:35:56 +000086 public ArArchiveEntry(File inputFile, String entryName) {
87 // TODO sort out mode
Sebastian Bazley2de1b502009-03-31 13:01:25 +000088 this(entryName, inputFile.isFile() ? inputFile.length() : 0, 0, 0, 0, inputFile.lastModified());
Sebastian Bazleyfec51a12009-03-31 00:35:56 +000089 }
90
91 public long getSize() {
Torsten Curdtca165392008-07-10 10:17:44 +000092 return this.getLength();
93 }
94
95 public String getName() {
96 return name;
97 }
98
99 public int getUserId() {
100 return userId;
101 }
102
103 public int getGroupId() {
104 return groupId;
105 }
106
107 public int getMode() {
108 return mode;
109 }
110
111 public long getLastModified() {
112 return lastModified;
113 }
114
Stefan Bodewig17ffd7f2009-08-01 14:52:15 +0000115 /** {@inheritDocs} */
116 public Date getLastModifiedDate() {
117 return new Date(1000 * getLastModified());
118 }
119
Torsten Curdtca165392008-07-10 10:17:44 +0000120 public long getLength() {
121 return length;
122 }
Torsten Curdtd6612b62009-01-07 13:48:42 +0000123
124 public boolean isDirectory() {
125 return false;
126 }
Christian Grobmeier28c23fb2009-04-17 05:14:49 +0000127
128 /* (non-Javadoc)
129 * @see java.lang.Object#hashCode()
130 */
131 public int hashCode() {
132 final int prime = 31;
133 int result = 1;
134 result = prime * result + ((name == null) ? 0 : name.hashCode());
135 return result;
136 }
137
138 /* (non-Javadoc)
139 * @see java.lang.Object#equals(java.lang.Object)
140 */
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
145 if (obj == null || getClass() != obj.getClass()) {
146 return false;
147 }
148 ArArchiveEntry other = (ArArchiveEntry) obj;
149 if (name == null) {
150 if (other.name != null) {
151 return false;
152 }
153 } else if (!name.equals(other.name)) {
154 return false;
155 }
156 return true;
157 }
Torsten Curdtca165392008-07-10 10:17:44 +0000158}