Source Code Cross Referenced for AsiExtraField.java in  » Build » ANT » org » apache » tools » zip » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.zip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */
018:
019:        package org.apache.tools.zip;
020:
021:        import java.util.zip.CRC32;
022:        import java.util.zip.ZipException;
023:
024:        /**
025:         * Adds Unix file permission and UID/GID fields as well as symbolic
026:         * link handling.
027:         *
028:         * <p>This class uses the ASi extra field in the format:
029:         * <pre>
030:         *         Value         Size            Description
031:         *         -----         ----            -----------
032:         * (Unix3) 0x756e        Short           tag for this extra block type
033:         *         TSize         Short           total data size for this block
034:         *         CRC           Long            CRC-32 of the remaining data
035:         *         Mode          Short           file permissions
036:         *         SizDev        Long            symlink'd size OR major/minor dev num
037:         *         UID           Short           user ID
038:         *         GID           Short           group ID
039:         *         (var.)        variable        symbolic link filename
040:         * </pre>
041:         * taken from appnote.iz (Info-ZIP note, 981119) found at <a
042:         * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
043:
044:         *
045:         * <p>Short is two bytes and Long is four bytes in big endian byte and
046:         * word order, device numbers are currently not supported.</p>
047:         *
048:         */
049:        public class AsiExtraField implements  ZipExtraField, UnixStat,
050:                Cloneable {
051:
052:            private static final ZipShort HEADER_ID = new ZipShort(0x756E);
053:
054:            /**
055:             * Standard Unix stat(2) file mode.
056:             *
057:             * @since 1.1
058:             */
059:            private int mode = 0;
060:            /**
061:             * User ID.
062:             *
063:             * @since 1.1
064:             */
065:            private int uid = 0;
066:            /**
067:             * Group ID.
068:             *
069:             * @since 1.1
070:             */
071:            private int gid = 0;
072:            /**
073:             * File this entry points to, if it is a symbolic link.
074:             *
075:             * <p>empty string - if entry is not a symbolic link.</p>
076:             *
077:             * @since 1.1
078:             */
079:            private String link = "";
080:            /**
081:             * Is this an entry for a directory?
082:             *
083:             * @since 1.1
084:             */
085:            private boolean dirFlag = false;
086:
087:            /**
088:             * Instance used to calculate checksums.
089:             *
090:             * @since 1.1
091:             */
092:            private CRC32 crc = new CRC32();
093:
094:            /** Constructor for AsiExtraField. */
095:            public AsiExtraField() {
096:            }
097:
098:            /**
099:             * The Header-ID.
100:             * @return the value for the header id for this extrafield
101:             * @since 1.1
102:             */
103:            public ZipShort getHeaderId() {
104:                return HEADER_ID;
105:            }
106:
107:            /**
108:             * Length of the extra field in the local file data - without
109:             * Header-ID or length specifier.
110:             * @return a <code>ZipShort</code> for the length of the data of this extra field
111:             * @since 1.1
112:             */
113:            public ZipShort getLocalFileDataLength() {
114:                return new ZipShort(4 // CRC
115:                        + 2 // Mode
116:                        + 4 // SizDev
117:                        + 2 // UID
118:                        + 2 // GID
119:                        + getLinkedFile().getBytes().length);
120:            }
121:
122:            /**
123:             * Delegate to local file data.
124:             * @return the centralDirectory length
125:             * @since 1.1
126:             */
127:            public ZipShort getCentralDirectoryLength() {
128:                return getLocalFileDataLength();
129:            }
130:
131:            /**
132:             * The actual data to put into local file data - without Header-ID
133:             * or length specifier.
134:             * @return get the data
135:             * @since 1.1
136:             */
137:            public byte[] getLocalFileDataData() {
138:                // CRC will be added later
139:                byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
140:                System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
141:
142:                byte[] linkArray = getLinkedFile().getBytes();
143:                System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data,
144:                        2, 4);
145:
146:                System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2);
147:                System
148:                        .arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8,
149:                                2);
150:
151:                System.arraycopy(linkArray, 0, data, 10, linkArray.length);
152:
153:                crc.reset();
154:                crc.update(data);
155:                long checksum = crc.getValue();
156:
157:                byte[] result = new byte[data.length + 4];
158:                System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, 4);
159:                System.arraycopy(data, 0, result, 4, data.length);
160:                return result;
161:            }
162:
163:            /**
164:             * Delegate to local file data.
165:             * @return the local file data
166:             * @since 1.1
167:             */
168:            public byte[] getCentralDirectoryData() {
169:                return getLocalFileDataData();
170:            }
171:
172:            /**
173:             * Set the user id.
174:             * @param uid the user id
175:             * @since 1.1
176:             */
177:            public void setUserId(int uid) {
178:                this .uid = uid;
179:            }
180:
181:            /**
182:             * Get the user id.
183:             * @return the user id
184:             * @since 1.1
185:             */
186:            public int getUserId() {
187:                return uid;
188:            }
189:
190:            /**
191:             * Set the group id.
192:             * @param gid the group id
193:             * @since 1.1
194:             */
195:            public void setGroupId(int gid) {
196:                this .gid = gid;
197:            }
198:
199:            /**
200:             * Get the group id.
201:             * @return the group id
202:             * @since 1.1
203:             */
204:            public int getGroupId() {
205:                return gid;
206:            }
207:
208:            /**
209:             * Indicate that this entry is a symbolic link to the given filename.
210:             *
211:             * @param name Name of the file this entry links to, empty String
212:             *             if it is not a symbolic link.
213:             *
214:             * @since 1.1
215:             */
216:            public void setLinkedFile(String name) {
217:                link = name;
218:                mode = getMode(mode);
219:            }
220:
221:            /**
222:             * Name of linked file
223:             *
224:             * @return name of the file this entry links to if it is a
225:             *         symbolic link, the empty string otherwise.
226:             *
227:             * @since 1.1
228:             */
229:            public String getLinkedFile() {
230:                return link;
231:            }
232:
233:            /**
234:             * Is this entry a symbolic link?
235:             * @return true if this is a symbolic link
236:             * @since 1.1
237:             */
238:            public boolean isLink() {
239:                return getLinkedFile().length() != 0;
240:            }
241:
242:            /**
243:             * File mode of this file.
244:             * @param mode the file mode
245:             * @since 1.1
246:             */
247:            public void setMode(int mode) {
248:                this .mode = getMode(mode);
249:            }
250:
251:            /**
252:             * File mode of this file.
253:             * @return the file mode
254:             * @since 1.1
255:             */
256:            public int getMode() {
257:                return mode;
258:            }
259:
260:            /**
261:             * Indicate whether this entry is a directory.
262:             * @param dirFlag if true, this entry is a directory
263:             * @since 1.1
264:             */
265:            public void setDirectory(boolean dirFlag) {
266:                this .dirFlag = dirFlag;
267:                mode = getMode(mode);
268:            }
269:
270:            /**
271:             * Is this entry a directory?
272:             * @return true if this entry is a directory
273:             * @since 1.1
274:             */
275:            public boolean isDirectory() {
276:                return dirFlag && !isLink();
277:            }
278:
279:            /**
280:             * Populate data from this array as if it was in local file data.
281:             * @param data an array of bytes
282:             * @param offset the start offset
283:             * @param length the number of bytes in the array from offset
284:             * @since 1.1
285:             * @throws ZipException on error
286:             */
287:            public void parseFromLocalFileData(byte[] data, int offset,
288:                    int length) throws ZipException {
289:
290:                long givenChecksum = ZipLong.getValue(data, offset);
291:                byte[] tmp = new byte[length - 4];
292:                System.arraycopy(data, offset + 4, tmp, 0, length - 4);
293:                crc.reset();
294:                crc.update(tmp);
295:                long realChecksum = crc.getValue();
296:                if (givenChecksum != realChecksum) {
297:                    throw new ZipException("bad CRC checksum "
298:                            + Long.toHexString(givenChecksum) + " instead of "
299:                            + Long.toHexString(realChecksum));
300:                }
301:
302:                int newMode = ZipShort.getValue(tmp, 0);
303:                byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
304:                uid = ZipShort.getValue(tmp, 6);
305:                gid = ZipShort.getValue(tmp, 8);
306:
307:                if (linkArray.length == 0) {
308:                    link = "";
309:                } else {
310:                    System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
311:                    link = new String(linkArray);
312:                }
313:                setDirectory((newMode & DIR_FLAG) != 0);
314:                setMode(newMode);
315:            }
316:
317:            /**
318:             * Get the file mode for given permissions with the correct file type.
319:             * @param mode the mode
320:             * @return the type with the mode
321:             * @since 1.1
322:             */
323:            protected int getMode(int mode) {
324:                int type = FILE_FLAG;
325:                if (isLink()) {
326:                    type = LINK_FLAG;
327:                } else if (isDirectory()) {
328:                    type = DIR_FLAG;
329:                }
330:                return type | (mode & PERM_MASK);
331:            }
332:
333:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.