| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace ICSharpCode.SharpZipLib.Tar |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// The TarInputStream reads a UNIX tar archive as an InputStream. |
| | | 9 | | /// methods are provided to position at each successive entry in |
| | | 10 | | /// the archive, and the read each entry as a normal input stream |
| | | 11 | | /// using read(). |
| | | 12 | | /// </summary> |
| | | 13 | | public class TarInputStream : Stream |
| | | 14 | | { |
| | | 15 | | #region Constructors |
| | | 16 | | /// <summary> |
| | | 17 | | /// Construct a TarInputStream with default block factor |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="inputStream">stream to source data from</param> |
| | | 20 | | public TarInputStream(Stream inputStream) |
| | 4 | 21 | | : this(inputStream, TarBuffer.DefaultBlockFactor) |
| | | 22 | | { |
| | 4 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Construct a TarInputStream with user specified block factor |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="inputStream">stream to source data from</param> |
| | | 29 | | /// <param name="blockFactor">block factor to apply to archive</param> |
| | 5 | 30 | | public TarInputStream(Stream inputStream, int blockFactor) |
| | | 31 | | { |
| | 5 | 32 | | this.inputStream = inputStream; |
| | 5 | 33 | | tarBuffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor); |
| | 5 | 34 | | } |
| | | 35 | | |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Get/set flag indicating ownership of the underlying stream. |
| | | 40 | | /// When the flag is true <see cref="Close"></see> will close the underlying stream also. |
| | | 41 | | /// </summary> |
| | | 42 | | public bool IsStreamOwner { |
| | 0 | 43 | | get { return tarBuffer.IsStreamOwner; } |
| | 2 | 44 | | set { tarBuffer.IsStreamOwner = value; } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | #region Stream Overrides |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a value indicating whether the current stream supports reading |
| | | 50 | | /// </summary> |
| | | 51 | | public override bool CanRead { |
| | | 52 | | get { |
| | 0 | 53 | | return inputStream.CanRead; |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets a value indicating whether the current stream supports seeking |
| | | 59 | | /// This property always returns false. |
| | | 60 | | /// </summary> |
| | | 61 | | public override bool CanSeek { |
| | | 62 | | get { |
| | 0 | 63 | | return false; |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets a value indicating if the stream supports writing. |
| | | 69 | | /// This property always returns false. |
| | | 70 | | /// </summary> |
| | | 71 | | public override bool CanWrite { |
| | | 72 | | get { |
| | 0 | 73 | | return false; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The length in bytes of the stream |
| | | 79 | | /// </summary> |
| | | 80 | | public override long Length { |
| | | 81 | | get { |
| | 0 | 82 | | return inputStream.Length; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets or sets the position within the stream. |
| | | 88 | | /// Setting the Position is not supported and throws a NotSupportedExceptionNotSupportedException |
| | | 89 | | /// </summary> |
| | | 90 | | /// <exception cref="NotSupportedException">Any attempt to set position</exception> |
| | | 91 | | public override long Position { |
| | | 92 | | get { |
| | 0 | 93 | | return inputStream.Position; |
| | | 94 | | } |
| | | 95 | | set { |
| | 0 | 96 | | throw new NotSupportedException("TarInputStream Seek not supported"); |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Flushes the baseInputStream |
| | | 102 | | /// </summary> |
| | | 103 | | public override void Flush() |
| | | 104 | | { |
| | 0 | 105 | | inputStream.Flush(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Set the streams position. This operation is not supported and will throw a NotSupportedException |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="offset">The offset relative to the origin to seek to.</param> |
| | | 112 | | /// <param name="origin">The <see cref="SeekOrigin"/> to start seeking from.</param> |
| | | 113 | | /// <returns>The new position in the stream.</returns> |
| | | 114 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | | 115 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 116 | | { |
| | 0 | 117 | | throw new NotSupportedException("TarInputStream Seek not supported"); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Sets the length of the stream |
| | | 122 | | /// This operation is not supported and will throw a NotSupportedException |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="value">The new stream length.</param> |
| | | 125 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | | 126 | | public override void SetLength(long value) |
| | | 127 | | { |
| | 0 | 128 | | throw new NotSupportedException("TarInputStream SetLength not supported"); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Writes a block of bytes to this stream using data from a buffer. |
| | | 133 | | /// This operation is not supported and will throw a NotSupportedException |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="buffer">The buffer containing bytes to write.</param> |
| | | 136 | | /// <param name="offset">The offset in the buffer of the frist byte to write.</param> |
| | | 137 | | /// <param name="count">The number of bytes to write.</param> |
| | | 138 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | | 139 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 140 | | { |
| | 0 | 141 | | throw new NotSupportedException("TarInputStream Write not supported"); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Writes a byte to the current position in the file stream. |
| | | 146 | | /// This operation is not supported and will throw a NotSupportedException |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="value">The byte value to write.</param> |
| | | 149 | | /// <exception cref="NotSupportedException">Any access</exception> |
| | | 150 | | public override void WriteByte(byte value) |
| | | 151 | | { |
| | 0 | 152 | | throw new NotSupportedException("TarInputStream WriteByte not supported"); |
| | | 153 | | } |
| | | 154 | | /// <summary> |
| | | 155 | | /// Reads a byte from the current tar archive entry. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <returns>A byte cast to an int; -1 if the at the end of the stream.</returns> |
| | | 158 | | public override int ReadByte() |
| | | 159 | | { |
| | 0 | 160 | | byte[] oneByteBuffer = new byte[1]; |
| | 0 | 161 | | int num = Read(oneByteBuffer, 0, 1); |
| | 0 | 162 | | if (num <= 0) { |
| | | 163 | | // return -1 to indicate that no byte was read. |
| | 0 | 164 | | return -1; |
| | | 165 | | } |
| | 0 | 166 | | return oneByteBuffer[0]; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Reads bytes from the current tar archive entry. |
| | | 171 | | /// |
| | | 172 | | /// This method is aware of the boundaries of the current |
| | | 173 | | /// entry in the archive and will deal with them appropriately |
| | | 174 | | /// </summary> |
| | | 175 | | /// <param name="buffer"> |
| | | 176 | | /// The buffer into which to place bytes read. |
| | | 177 | | /// </param> |
| | | 178 | | /// <param name="offset"> |
| | | 179 | | /// The offset at which to place bytes read. |
| | | 180 | | /// </param> |
| | | 181 | | /// <param name="count"> |
| | | 182 | | /// The number of bytes to read. |
| | | 183 | | /// </param> |
| | | 184 | | /// <returns> |
| | | 185 | | /// The number of bytes read, or 0 at end of stream/EOF. |
| | | 186 | | /// </returns> |
| | | 187 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 188 | | { |
| | 0 | 189 | | if (buffer == null) { |
| | 0 | 190 | | throw new ArgumentNullException(nameof(buffer)); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | int totalRead = 0; |
| | | 194 | | |
| | 0 | 195 | | if (entryOffset >= entrySize) { |
| | 0 | 196 | | return 0; |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | long numToRead = count; |
| | | 200 | | |
| | 0 | 201 | | if ((numToRead + entryOffset) > entrySize) { |
| | 0 | 202 | | numToRead = entrySize - entryOffset; |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | if (readBuffer != null) { |
| | 0 | 206 | | int sz = (numToRead > readBuffer.Length) ? readBuffer.Length : (int)numToRead; |
| | | 207 | | |
| | 0 | 208 | | Array.Copy(readBuffer, 0, buffer, offset, sz); |
| | | 209 | | |
| | 0 | 210 | | if (sz >= readBuffer.Length) { |
| | 0 | 211 | | readBuffer = null; |
| | 0 | 212 | | } else { |
| | 0 | 213 | | int newLen = readBuffer.Length - sz; |
| | 0 | 214 | | byte[] newBuf = new byte[newLen]; |
| | 0 | 215 | | Array.Copy(readBuffer, sz, newBuf, 0, newLen); |
| | 0 | 216 | | readBuffer = newBuf; |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | totalRead += sz; |
| | 0 | 220 | | numToRead -= sz; |
| | 0 | 221 | | offset += sz; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | while (numToRead > 0) { |
| | 0 | 225 | | byte[] rec = tarBuffer.ReadBlock(); |
| | 0 | 226 | | if (rec == null) { |
| | | 227 | | // Unexpected EOF! |
| | 0 | 228 | | throw new TarException("unexpected EOF with " + numToRead + " bytes unread"); |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | var sz = (int)numToRead; |
| | 0 | 232 | | int recLen = rec.Length; |
| | | 233 | | |
| | 0 | 234 | | if (recLen > sz) { |
| | 0 | 235 | | Array.Copy(rec, 0, buffer, offset, sz); |
| | 0 | 236 | | readBuffer = new byte[recLen - sz]; |
| | 0 | 237 | | Array.Copy(rec, sz, readBuffer, 0, recLen - sz); |
| | 0 | 238 | | } else { |
| | 0 | 239 | | sz = recLen; |
| | 0 | 240 | | Array.Copy(rec, 0, buffer, offset, recLen); |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | totalRead += sz; |
| | 0 | 244 | | numToRead -= sz; |
| | 0 | 245 | | offset += sz; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | entryOffset += totalRead; |
| | | 249 | | |
| | 0 | 250 | | return totalRead; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Closes this stream. Calls the TarBuffer's close() method. |
| | | 255 | | /// The underlying stream is closed by the TarBuffer. |
| | | 256 | | /// </summary> |
| | | 257 | | public override void Close() |
| | | 258 | | { |
| | 5 | 259 | | tarBuffer.Close(); |
| | 5 | 260 | | } |
| | | 261 | | |
| | | 262 | | #endregion |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Set the entry factory for this instance. |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="factory">The factory for creating new entries</param> |
| | | 268 | | public void SetEntryFactory(IEntryFactory factory) |
| | | 269 | | { |
| | 0 | 270 | | entryFactory = factory; |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Get the record size being used by this stream's TarBuffer. |
| | | 275 | | /// </summary> |
| | | 276 | | public int RecordSize { |
| | 0 | 277 | | get { return tarBuffer.RecordSize; } |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Get the record size being used by this stream's TarBuffer. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <returns> |
| | | 284 | | /// TarBuffer record size. |
| | | 285 | | /// </returns> |
| | | 286 | | [Obsolete("Use RecordSize property instead")] |
| | | 287 | | public int GetRecordSize() |
| | | 288 | | { |
| | 0 | 289 | | return tarBuffer.RecordSize; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Get the available data that can be read from the current |
| | | 294 | | /// entry in the archive. This does not indicate how much data |
| | | 295 | | /// is left in the entire archive, only in the current entry. |
| | | 296 | | /// This value is determined from the entry's size header field |
| | | 297 | | /// and the amount of data already read from the current entry. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <returns> |
| | | 300 | | /// The number of available bytes for the current entry. |
| | | 301 | | /// </returns> |
| | | 302 | | public long Available { |
| | | 303 | | get { |
| | 0 | 304 | | return entrySize - entryOffset; |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Skip bytes in the input buffer. This skips bytes in the |
| | | 310 | | /// current entry's data, not the entire archive, and will |
| | | 311 | | /// stop at the end of the current entry's data if the number |
| | | 312 | | /// to skip extends beyond that point. |
| | | 313 | | /// </summary> |
| | | 314 | | /// <param name="skipCount"> |
| | | 315 | | /// The number of bytes to skip. |
| | | 316 | | /// </param> |
| | | 317 | | public void Skip(long skipCount) |
| | | 318 | | { |
| | | 319 | | // TODO: REVIEW efficiency of TarInputStream.Skip |
| | | 320 | | // This is horribly inefficient, but it ensures that we |
| | | 321 | | // properly skip over bytes via the TarBuffer... |
| | | 322 | | // |
| | 0 | 323 | | byte[] skipBuf = new byte[8 * 1024]; |
| | | 324 | | |
| | 0 | 325 | | for (long num = skipCount; num > 0;) { |
| | 0 | 326 | | int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num; |
| | 0 | 327 | | int numRead = Read(skipBuf, 0, toRead); |
| | | 328 | | |
| | 0 | 329 | | if (numRead == -1) { |
| | | 330 | | break; |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | num -= numRead; |
| | | 334 | | } |
| | 0 | 335 | | } |
| | | 336 | | |
| | | 337 | | /// <summary> |
| | | 338 | | /// Return a value of true if marking is supported; false otherwise. |
| | | 339 | | /// </summary> |
| | | 340 | | /// <remarks>Currently marking is not supported, the return value is always false.</remarks> |
| | | 341 | | public bool IsMarkSupported { |
| | | 342 | | get { |
| | 0 | 343 | | return false; |
| | | 344 | | } |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// Since we do not support marking just yet, we do nothing. |
| | | 349 | | /// </summary> |
| | | 350 | | /// <param name ="markLimit"> |
| | | 351 | | /// The limit to mark. |
| | | 352 | | /// </param> |
| | | 353 | | public void Mark(int markLimit) |
| | | 354 | | { |
| | 0 | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Since we do not support marking just yet, we do nothing. |
| | | 359 | | /// </summary> |
| | | 360 | | public void Reset() |
| | | 361 | | { |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <summary> |
| | | 365 | | /// Get the next entry in this tar archive. This will skip |
| | | 366 | | /// over any remaining data in the current entry, if there |
| | | 367 | | /// is one, and place the input stream at the header of the |
| | | 368 | | /// next entry, and read the header and instantiate a new |
| | | 369 | | /// TarEntry from the header bytes and return that entry. |
| | | 370 | | /// If there are no more entries in the archive, null will |
| | | 371 | | /// be returned to indicate that the end of the archive has |
| | | 372 | | /// been reached. |
| | | 373 | | /// </summary> |
| | | 374 | | /// <returns> |
| | | 375 | | /// The next TarEntry in the archive, or null. |
| | | 376 | | /// </returns> |
| | | 377 | | public TarEntry GetNextEntry() |
| | | 378 | | { |
| | 3 | 379 | | if (hasHitEOF) { |
| | 0 | 380 | | return null; |
| | | 381 | | } |
| | | 382 | | |
| | 3 | 383 | | if (currentEntry != null) { |
| | 0 | 384 | | SkipToNextEntry(); |
| | | 385 | | } |
| | | 386 | | |
| | 3 | 387 | | byte[] headerBuf = tarBuffer.ReadBlock(); |
| | | 388 | | |
| | 3 | 389 | | if (headerBuf == null) { |
| | 0 | 390 | | hasHitEOF = true; |
| | 0 | 391 | | } else |
| | 3 | 392 | | hasHitEOF |= TarBuffer.IsEndOfArchiveBlock(headerBuf); |
| | | 393 | | |
| | 3 | 394 | | if (hasHitEOF) { |
| | 1 | 395 | | currentEntry = null; |
| | 1 | 396 | | } else { |
| | | 397 | | try { |
| | 2 | 398 | | var header = new TarHeader(); |
| | 2 | 399 | | header.ParseBuffer(headerBuf); |
| | 2 | 400 | | if (!header.IsChecksumValid) { |
| | 1 | 401 | | throw new TarException("Header checksum is invalid"); |
| | | 402 | | } |
| | 1 | 403 | | this.entryOffset = 0; |
| | 1 | 404 | | this.entrySize = header.Size; |
| | | 405 | | |
| | 1 | 406 | | StringBuilder longName = null; |
| | | 407 | | |
| | 1 | 408 | | if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) { |
| | | 409 | | |
| | 0 | 410 | | byte[] nameBuffer = new byte[TarBuffer.BlockSize]; |
| | 0 | 411 | | long numToRead = this.entrySize; |
| | | 412 | | |
| | 0 | 413 | | longName = new StringBuilder(); |
| | | 414 | | |
| | 0 | 415 | | while (numToRead > 0) { |
| | 0 | 416 | | int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead |
| | | 417 | | |
| | 0 | 418 | | if (numRead == -1) { |
| | 0 | 419 | | throw new InvalidHeaderException("Failed to read long name entry"); |
| | | 420 | | } |
| | | 421 | | |
| | 0 | 422 | | longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString()); |
| | 0 | 423 | | numToRead -= numRead; |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | SkipToNextEntry(); |
| | 0 | 427 | | headerBuf = this.tarBuffer.ReadBlock(); |
| | 1 | 428 | | } else if (header.TypeFlag == TarHeader.LF_GHDR) { // POSIX global extended header |
| | | 429 | | // Ignore things we dont understand completely for now |
| | 0 | 430 | | SkipToNextEntry(); |
| | 0 | 431 | | headerBuf = this.tarBuffer.ReadBlock(); |
| | 1 | 432 | | } else if (header.TypeFlag == TarHeader.LF_XHDR) { // POSIX extended header |
| | | 433 | | // Ignore things we dont understand completely for now |
| | 0 | 434 | | SkipToNextEntry(); |
| | 0 | 435 | | headerBuf = this.tarBuffer.ReadBlock(); |
| | 1 | 436 | | } else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) { |
| | | 437 | | // TODO: could show volume name when verbose |
| | 0 | 438 | | SkipToNextEntry(); |
| | 0 | 439 | | headerBuf = this.tarBuffer.ReadBlock(); |
| | 1 | 440 | | } else if (header.TypeFlag != TarHeader.LF_NORMAL && |
| | 1 | 441 | | header.TypeFlag != TarHeader.LF_OLDNORM && |
| | 1 | 442 | | header.TypeFlag != TarHeader.LF_LINK && |
| | 1 | 443 | | header.TypeFlag != TarHeader.LF_SYMLINK && |
| | 1 | 444 | | header.TypeFlag != TarHeader.LF_DIR) { |
| | | 445 | | // Ignore things we dont understand completely for now |
| | 0 | 446 | | SkipToNextEntry(); |
| | 0 | 447 | | headerBuf = tarBuffer.ReadBlock(); |
| | | 448 | | } |
| | | 449 | | |
| | 1 | 450 | | if (entryFactory == null) { |
| | 1 | 451 | | currentEntry = new TarEntry(headerBuf); |
| | 1 | 452 | | if (longName != null) { |
| | 0 | 453 | | currentEntry.Name = longName.ToString(); |
| | | 454 | | } |
| | 0 | 455 | | } else { |
| | 0 | 456 | | currentEntry = entryFactory.CreateEntry(headerBuf); |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | // Magic was checked here for 'ustar' but there are multiple valid possibilities |
| | | 460 | | // so this is not done anymore. |
| | | 461 | | |
| | 1 | 462 | | entryOffset = 0; |
| | | 463 | | |
| | | 464 | | // TODO: Review How do we resolve this discrepancy?! |
| | 1 | 465 | | entrySize = this.currentEntry.Size; |
| | 1 | 466 | | } catch (InvalidHeaderException ex) { |
| | 0 | 467 | | entrySize = 0; |
| | 0 | 468 | | entryOffset = 0; |
| | 0 | 469 | | currentEntry = null; |
| | 0 | 470 | | string errorText = string.Format("Bad header in record {0} block {1} {2}", |
| | 0 | 471 | | tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, ex.Message); |
| | 0 | 472 | | throw new InvalidHeaderException(errorText); |
| | | 473 | | } |
| | | 474 | | } |
| | 2 | 475 | | return currentEntry; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Copies the contents of the current tar archive entry directly into |
| | | 480 | | /// an output stream. |
| | | 481 | | /// </summary> |
| | | 482 | | /// <param name="outputStream"> |
| | | 483 | | /// The OutputStream into which to write the entry's data. |
| | | 484 | | /// </param> |
| | | 485 | | public void CopyEntryContents(Stream outputStream) |
| | | 486 | | { |
| | 0 | 487 | | byte[] tempBuffer = new byte[32 * 1024]; |
| | | 488 | | |
| | 0 | 489 | | while (true) { |
| | 0 | 490 | | int numRead = Read(tempBuffer, 0, tempBuffer.Length); |
| | 0 | 491 | | if (numRead <= 0) { |
| | | 492 | | break; |
| | | 493 | | } |
| | 0 | 494 | | outputStream.Write(tempBuffer, 0, numRead); |
| | | 495 | | } |
| | 0 | 496 | | } |
| | | 497 | | |
| | | 498 | | void SkipToNextEntry() |
| | | 499 | | { |
| | 0 | 500 | | long numToSkip = entrySize - entryOffset; |
| | | 501 | | |
| | 0 | 502 | | if (numToSkip > 0) { |
| | 0 | 503 | | Skip(numToSkip); |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | readBuffer = null; |
| | 0 | 507 | | } |
| | | 508 | | |
| | | 509 | | /// <summary> |
| | | 510 | | /// This interface is provided, along with the method <see cref="SetEntryFactory"/>, to allow |
| | | 511 | | /// the programmer to have their own <see cref="TarEntry"/> subclass instantiated for the |
| | | 512 | | /// entries return from <see cref="GetNextEntry"/>. |
| | | 513 | | /// </summary> |
| | | 514 | | public interface IEntryFactory |
| | | 515 | | { |
| | | 516 | | /// <summary> |
| | | 517 | | /// Create an entry based on name alone |
| | | 518 | | /// </summary> |
| | | 519 | | /// <param name="name"> |
| | | 520 | | /// Name of the new EntryPointNotFoundException to create |
| | | 521 | | /// </param> |
| | | 522 | | /// <returns>created TarEntry or descendant class</returns> |
| | | 523 | | TarEntry CreateEntry(string name); |
| | | 524 | | |
| | | 525 | | /// <summary> |
| | | 526 | | /// Create an instance based on an actual file |
| | | 527 | | /// </summary> |
| | | 528 | | /// <param name="fileName"> |
| | | 529 | | /// Name of file to represent in the entry |
| | | 530 | | /// </param> |
| | | 531 | | /// <returns> |
| | | 532 | | /// Created TarEntry or descendant class |
| | | 533 | | /// </returns> |
| | | 534 | | TarEntry CreateEntryFromFile(string fileName); |
| | | 535 | | |
| | | 536 | | /// <summary> |
| | | 537 | | /// Create a tar entry based on the header information passed |
| | | 538 | | /// </summary> |
| | | 539 | | /// <param name="headerBuffer"> |
| | | 540 | | /// Buffer containing header information to create an an entry from. |
| | | 541 | | /// </param> |
| | | 542 | | /// <returns> |
| | | 543 | | /// Created TarEntry or descendant class |
| | | 544 | | /// </returns> |
| | | 545 | | TarEntry CreateEntry(byte[] headerBuffer); |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Standard entry factory class creating instances of the class TarEntry |
| | | 550 | | /// </summary> |
| | | 551 | | public class EntryFactoryAdapter : IEntryFactory |
| | | 552 | | { |
| | | 553 | | /// <summary> |
| | | 554 | | /// Create a <see cref="TarEntry"/> based on named |
| | | 555 | | /// </summary> |
| | | 556 | | /// <param name="name">The name to use for the entry</param> |
| | | 557 | | /// <returns>A new <see cref="TarEntry"/></returns> |
| | | 558 | | public TarEntry CreateEntry(string name) |
| | | 559 | | { |
| | 0 | 560 | | return TarEntry.CreateTarEntry(name); |
| | | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Create a tar entry with details obtained from <paramref name="fileName">file</paramref> |
| | | 565 | | /// </summary> |
| | | 566 | | /// <param name="fileName">The name of the file to retrieve details from.</param> |
| | | 567 | | /// <returns>A new <see cref="TarEntry"/></returns> |
| | | 568 | | public TarEntry CreateEntryFromFile(string fileName) |
| | | 569 | | { |
| | 0 | 570 | | return TarEntry.CreateEntryFromFile(fileName); |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | /// <summary> |
| | | 574 | | /// Create an entry based on details in <paramref name="headerBuffer">header</paramref> |
| | | 575 | | /// </summary> |
| | | 576 | | /// <param name="headerBuffer">The buffer containing entry details.</param> |
| | | 577 | | /// <returns>A new <see cref="TarEntry"/></returns> |
| | | 578 | | public TarEntry CreateEntry(byte[] headerBuffer) |
| | | 579 | | { |
| | 0 | 580 | | return new TarEntry(headerBuffer); |
| | | 581 | | } |
| | | 582 | | } |
| | | 583 | | |
| | | 584 | | #region Instance Fields |
| | | 585 | | /// <summary> |
| | | 586 | | /// Flag set when last block has been read |
| | | 587 | | /// </summary> |
| | | 588 | | protected bool hasHitEOF; |
| | | 589 | | |
| | | 590 | | /// <summary> |
| | | 591 | | /// Size of this entry as recorded in header |
| | | 592 | | /// </summary> |
| | | 593 | | protected long entrySize; |
| | | 594 | | |
| | | 595 | | /// <summary> |
| | | 596 | | /// Number of bytes read for this entry so far |
| | | 597 | | /// </summary> |
| | | 598 | | protected long entryOffset; |
| | | 599 | | |
| | | 600 | | /// <summary> |
| | | 601 | | /// Buffer used with calls to <code>Read()</code> |
| | | 602 | | /// </summary> |
| | | 603 | | protected byte[] readBuffer; |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Working buffer |
| | | 607 | | /// </summary> |
| | | 608 | | protected TarBuffer tarBuffer; |
| | | 609 | | |
| | | 610 | | /// <summary> |
| | | 611 | | /// Current entry being read |
| | | 612 | | /// </summary> |
| | | 613 | | TarEntry currentEntry; |
| | | 614 | | |
| | | 615 | | /// <summary> |
| | | 616 | | /// Factory used to create TarEntry or descendant class instance |
| | | 617 | | /// </summary> |
| | | 618 | | protected IEntryFactory entryFactory; |
| | | 619 | | |
| | | 620 | | /// <summary> |
| | | 621 | | /// Stream used as the source of input data. |
| | | 622 | | /// </summary> |
| | | 623 | | readonly Stream inputStream; |
| | | 624 | | #endregion |
| | | 625 | | } |
| | | 626 | | } |