You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsiExtraField.java 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2001-2002,2004-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.zip;
  18. import java.util.zip.CRC32;
  19. import java.util.zip.ZipException;
  20. /**
  21. * Adds Unix file permission and UID/GID fields as well as symbolic
  22. * link handling.
  23. *
  24. * <p>This class uses the ASi extra field in the format:
  25. * <pre>
  26. * Value Size Description
  27. * ----- ---- -----------
  28. * (Unix3) 0x756e Short tag for this extra block type
  29. * TSize Short total data size for this block
  30. * CRC Long CRC-32 of the remaining data
  31. * Mode Short file permissions
  32. * SizDev Long symlink'd size OR major/minor dev num
  33. * UID Short user ID
  34. * GID Short group ID
  35. * (var.) variable symbolic link filename
  36. * </pre>
  37. * taken from appnote.iz (Info-ZIP note, 981119) found at <a
  38. * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
  39. *
  40. * <p>Short is two bytes and Long is four bytes in big endian byte and
  41. * word order, device numbers are currently not supported.</p>
  42. *
  43. */
  44. public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
  45. private static final ZipShort HEADER_ID = new ZipShort(0x756E);
  46. /**
  47. * Standard Unix stat(2) file mode.
  48. *
  49. * @since 1.1
  50. */
  51. private int mode = 0;
  52. /**
  53. * User ID.
  54. *
  55. * @since 1.1
  56. */
  57. private int uid = 0;
  58. /**
  59. * Group ID.
  60. *
  61. * @since 1.1
  62. */
  63. private int gid = 0;
  64. /**
  65. * File this entry points to, if it is a symbolic link.
  66. *
  67. * <p>empty string - if entry is not a symbolic link.</p>
  68. *
  69. * @since 1.1
  70. */
  71. private String link = "";
  72. /**
  73. * Is this an entry for a directory?
  74. *
  75. * @since 1.1
  76. */
  77. private boolean dirFlag = false;
  78. /**
  79. * Instance used to calculate checksums.
  80. *
  81. * @since 1.1
  82. */
  83. private CRC32 crc = new CRC32();
  84. /** Constructor for AsiExtraField. */
  85. public AsiExtraField() {
  86. }
  87. /**
  88. * The Header-ID.
  89. * @return the value for the header id for this extrafield
  90. * @since 1.1
  91. */
  92. public ZipShort getHeaderId() {
  93. return HEADER_ID;
  94. }
  95. /**
  96. * Length of the extra field in the local file data - without
  97. * Header-ID or length specifier.
  98. * @return a <code>ZipShort</code> for the length of the data of this extra field
  99. * @since 1.1
  100. */
  101. public ZipShort getLocalFileDataLength() {
  102. return new ZipShort(4 // CRC
  103. + 2 // Mode
  104. + 4 // SizDev
  105. + 2 // UID
  106. + 2 // GID
  107. + getLinkedFile().getBytes().length);
  108. }
  109. /**
  110. * Delegate to local file data.
  111. * @return the centralDirectory length
  112. * @since 1.1
  113. */
  114. public ZipShort getCentralDirectoryLength() {
  115. return getLocalFileDataLength();
  116. }
  117. /**
  118. * The actual data to put into local file data - without Header-ID
  119. * or length specifier.
  120. * @return get the data
  121. * @since 1.1
  122. */
  123. public byte[] getLocalFileDataData() {
  124. // CRC will be added later
  125. byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
  126. System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
  127. byte[] linkArray = getLinkedFile().getBytes();
  128. System.arraycopy(ZipLong.getBytes(linkArray.length),
  129. 0, data, 2, 4);
  130. System.arraycopy(ZipShort.getBytes(getUserId()),
  131. 0, data, 6, 2);
  132. System.arraycopy(ZipShort.getBytes(getGroupId()),
  133. 0, data, 8, 2);
  134. System.arraycopy(linkArray, 0, data, 10, linkArray.length);
  135. crc.reset();
  136. crc.update(data);
  137. long checksum = crc.getValue();
  138. byte[] result = new byte[data.length + 4];
  139. System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, 4);
  140. System.arraycopy(data, 0, result, 4, data.length);
  141. return result;
  142. }
  143. /**
  144. * Delegate to local file data.
  145. * @return the local file data
  146. * @since 1.1
  147. */
  148. public byte[] getCentralDirectoryData() {
  149. return getLocalFileDataData();
  150. }
  151. /**
  152. * Set the user id.
  153. * @param uid the user id
  154. * @since 1.1
  155. */
  156. public void setUserId(int uid) {
  157. this.uid = uid;
  158. }
  159. /**
  160. * Get the user id.
  161. * @return the user id
  162. * @since 1.1
  163. */
  164. public int getUserId() {
  165. return uid;
  166. }
  167. /**
  168. * Set the group id.
  169. * @param gid the group id
  170. * @since 1.1
  171. */
  172. public void setGroupId(int gid) {
  173. this.gid = gid;
  174. }
  175. /**
  176. * Get the group id.
  177. * @return the group id
  178. * @since 1.1
  179. */
  180. public int getGroupId() {
  181. return gid;
  182. }
  183. /**
  184. * Indicate that this entry is a symbolic link to the given filename.
  185. *
  186. * @param name Name of the file this entry links to, empty String
  187. * if it is not a symbolic link.
  188. *
  189. * @since 1.1
  190. */
  191. public void setLinkedFile(String name) {
  192. link = name;
  193. mode = getMode(mode);
  194. }
  195. /**
  196. * Name of linked file
  197. *
  198. * @return name of the file this entry links to if it is a
  199. * symbolic link, the empty string otherwise.
  200. *
  201. * @since 1.1
  202. */
  203. public String getLinkedFile() {
  204. return link;
  205. }
  206. /**
  207. * Is this entry a symbolic link?
  208. * @return true if this is a symbolic link
  209. * @since 1.1
  210. */
  211. public boolean isLink() {
  212. return getLinkedFile().length() != 0;
  213. }
  214. /**
  215. * File mode of this file.
  216. * @param mode the file mode
  217. * @since 1.1
  218. */
  219. public void setMode(int mode) {
  220. this.mode = getMode(mode);
  221. }
  222. /**
  223. * File mode of this file.
  224. * @return the file mode
  225. * @since 1.1
  226. */
  227. public int getMode() {
  228. return mode;
  229. }
  230. /**
  231. * Indicate whether this entry is a directory.
  232. * @param dirFlag if true, this entry is a directory
  233. * @since 1.1
  234. */
  235. public void setDirectory(boolean dirFlag) {
  236. this.dirFlag = dirFlag;
  237. mode = getMode(mode);
  238. }
  239. /**
  240. * Is this entry a directory?
  241. * @return true if this entry is a directory
  242. * @since 1.1
  243. */
  244. public boolean isDirectory() {
  245. return dirFlag && !isLink();
  246. }
  247. /**
  248. * Populate data from this array as if it was in local file data.
  249. * @param data an array of bytes
  250. * @param offset the start offset
  251. * @param length the number of bytes in the array from offset
  252. * @since 1.1
  253. * @throws ZipException on error
  254. */
  255. public void parseFromLocalFileData(byte[] data, int offset, int length)
  256. throws ZipException {
  257. long givenChecksum = ZipLong.getValue(data, offset);
  258. byte[] tmp = new byte[length - 4];
  259. System.arraycopy(data, offset + 4, tmp, 0, length - 4);
  260. crc.reset();
  261. crc.update(tmp);
  262. long realChecksum = crc.getValue();
  263. if (givenChecksum != realChecksum) {
  264. throw new ZipException("bad CRC checksum "
  265. + Long.toHexString(givenChecksum)
  266. + " instead of "
  267. + Long.toHexString(realChecksum));
  268. }
  269. int newMode = ZipShort.getValue(tmp, 0);
  270. byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
  271. uid = ZipShort.getValue(tmp, 6);
  272. gid = ZipShort.getValue(tmp, 8);
  273. if (linkArray.length == 0) {
  274. link = "";
  275. } else {
  276. System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
  277. link = new String(linkArray);
  278. }
  279. setDirectory((newMode & DIR_FLAG) != 0);
  280. setMode(newMode);
  281. }
  282. /**
  283. * Get the file mode for given permissions with the correct file type.
  284. * @param mode the mode
  285. * @return the type with the mode
  286. * @since 1.1
  287. */
  288. protected int getMode(int mode) {
  289. int type = FILE_FLAG;
  290. if (isLink()) {
  291. type = LINK_FLAG;
  292. } else if (isDirectory()) {
  293. type = DIR_FLAG;
  294. }
  295. return type | (mode & PERM_MASK);
  296. }
  297. }