PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jtar/src/main/java/org/xeustechnologies/jtar/TarHeader.java

http://jtar.googlecode.com/
Java | 197 lines | 71 code | 23 blank | 103 comment | 7 complexity | 8025cd015a461fc13edbdd3f6b3667a5 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright 2012 Kamran Zafar
  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.xeustechnologies.jtar;
  18. /**
  19. * Header
  20. *
  21. * <pre>
  22. * Offset Size Field
  23. * 0 100 File name
  24. * 100 8 File mode
  25. * 108 8 Owner's numeric user ID
  26. * 116 8 Group's numeric user ID
  27. * 124 12 File size in bytes
  28. * 136 12 Last modification time in numeric Unix time format
  29. * 148 8 Checksum for header block
  30. * 156 1 Link indicator (file type)
  31. * 157 100 Name of linked file
  32. * </pre>
  33. *
  34. *
  35. * File Types
  36. *
  37. * <pre>
  38. * Value Meaning
  39. * '0' Normal file
  40. * (ASCII NUL) Normal file (now obsolete)
  41. * '1' Hard link
  42. * '2' Symbolic link
  43. * '3' Character special
  44. * '4' Block special
  45. * '5' Directory
  46. * '6' FIFO
  47. * '7' Contigous
  48. * </pre>
  49. *
  50. *
  51. *
  52. * Ustar header
  53. *
  54. * <pre>
  55. * Offset Size Field
  56. * 257 6 UStar indicator "ustar"
  57. * 263 2 UStar version "00"
  58. * 265 32 Owner user name
  59. * 297 32 Owner group name
  60. * 329 8 Device major number
  61. * 337 8 Device minor number
  62. * 345 155 Filename prefix
  63. * </pre>
  64. */
  65. public class TarHeader {
  66. /*
  67. * Header
  68. */
  69. public static final int NAMELEN = 100;
  70. public static final int MODELEN = 8;
  71. public static final int UIDLEN = 8;
  72. public static final int GIDLEN = 8;
  73. public static final int SIZELEN = 12;
  74. public static final int MODTIMELEN = 12;
  75. public static final int CHKSUMLEN = 8;
  76. public static final byte LF_OLDNORM = 0;
  77. /*
  78. * File Types
  79. */
  80. public static final byte LF_NORMAL = (byte) '0';
  81. public static final byte LF_LINK = (byte) '1';
  82. public static final byte LF_SYMLINK = (byte) '2';
  83. public static final byte LF_CHR = (byte) '3';
  84. public static final byte LF_BLK = (byte) '4';
  85. public static final byte LF_DIR = (byte) '5';
  86. public static final byte LF_FIFO = (byte) '6';
  87. public static final byte LF_CONTIG = (byte) '7';
  88. /*
  89. * Ustar header
  90. */
  91. public static final int MAGICLEN = 8;
  92. /**
  93. * The magic tag representing a POSIX tar archive.
  94. */
  95. public static final String TMAGIC = "ustar";
  96. /**
  97. * The magic tag representing a GNU tar archive.
  98. */
  99. public static final String GNU_TMAGIC = "ustar ";
  100. public static final int UNAMELEN = 32;
  101. public static final int GNAMELEN = 32;
  102. public static final int DEVLEN = 8;
  103. // Header values
  104. public StringBuffer name;
  105. public int mode;
  106. public int userId;
  107. public int groupId;
  108. public long size;
  109. public long modTime;
  110. public int checkSum;
  111. public byte linkFlag;
  112. public StringBuffer linkName;
  113. public StringBuffer magic;
  114. public StringBuffer userName;
  115. public StringBuffer groupName;
  116. public int devMajor;
  117. public int devMinor;
  118. public TarHeader() {
  119. this.magic = new StringBuffer( TarHeader.TMAGIC );
  120. this.name = new StringBuffer();
  121. this.linkName = new StringBuffer();
  122. String user = System.getProperty( "user.name", "" );
  123. if (user.length() > 31)
  124. user = user.substring( 0, 31 );
  125. this.userId = 0;
  126. this.groupId = 0;
  127. this.userName = new StringBuffer( user );
  128. this.groupName = new StringBuffer( "" );
  129. }
  130. /**
  131. * Parse an entry name from a header buffer.
  132. *
  133. * @param name
  134. * @param header
  135. * The header buffer from which to parse.
  136. * @param offset
  137. * The offset into the buffer from which to parse.
  138. * @param length
  139. * The number of header bytes to parse.
  140. * @return The header's entry name.
  141. */
  142. public static StringBuffer parseName(byte[] header, int offset, int length) {
  143. StringBuffer result = new StringBuffer( length );
  144. int end = offset + length;
  145. for (int i = offset; i < end; ++i) {
  146. if (header[i] == 0)
  147. break;
  148. result.append( (char) header[i] );
  149. }
  150. return result;
  151. }
  152. /**
  153. * Determine the number of bytes in an entry name.
  154. *
  155. * @param name
  156. * @param header
  157. * The header buffer from which to parse.
  158. * @param offset
  159. * The offset into the buffer from which to parse.
  160. * @param length
  161. * The number of header bytes to parse.
  162. * @return The number of bytes in a header's entry name.
  163. */
  164. public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
  165. int i;
  166. for (i = 0; i < length && i < name.length(); ++i) {
  167. buf[offset + i] = (byte) name.charAt( i );
  168. }
  169. for (; i < length; ++i) {
  170. buf[offset + i] = 0;
  171. }
  172. return offset + length;
  173. }
  174. }