Summary

Class:ICSharpCode.SharpZipLib.Tar.TarArchive
Assembly:ICSharpCode.SharpZipLib
File(s):C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Tar\TarArchive.cs
Covered lines:46
Uncovered lines:223
Coverable lines:269
Total lines:830
Line coverage:17.1%
Branch coverage:13.6%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
OnProgressMessageEvent(...)200
.ctor()100
.ctor(...)285.7166.67
.ctor(...)285.7166.67
CreateInputTarArchive(...)362.560
CreateInputTarArchive(...)36060
CreateOutputTarArchive(...)362.560
CreateOutputTarArchive(...)36060
SetKeepOldFiles(...)200
SetAsciiTranslation(...)200
SetUserInfo(...)200
CloseArchive()100
ListContents()357.1460
ExtractContents(...)500
ExtractEntry(...)1400
WriteEntry(...)500
WriteEntryCore(...)1900
Dispose()1100100
Dispose(...)510077.78
Close()100
Finalize()100
EnsureDirectoryExists(...)200
IsBinary(...)700

File(s)

C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Tar\TarArchive.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Text;
 4
 5namespace ICSharpCode.SharpZipLib.Tar
 6{
 7  /// <summary>
 8  /// Used to advise clients of 'events' while processing archives
 9  /// </summary>
 10  public delegate void ProgressMessageHandler(TarArchive archive, TarEntry entry, string message);
 11
 12  /// <summary>
 13  /// The TarArchive class implements the concept of a
 14  /// 'Tape Archive'. A tar archive is a series of entries, each of
 15  /// which represents a file system object. Each entry in
 16  /// the archive consists of a header block followed by 0 or more data blocks.
 17  /// Directory entries consist only of the header block, and are followed by entries
 18  /// for the directory's contents. File entries consist of a
 19  /// header followed by the number of blocks needed to
 20  /// contain the file's contents. All entries are written on
 21  /// block boundaries. Blocks are 512 bytes long.
 22  ///
 23  /// TarArchives are instantiated in either read or write mode,
 24  /// based upon whether they are instantiated with an InputStream
 25  /// or an OutputStream. Once instantiated TarArchives read/write
 26  /// mode can not be changed.
 27  ///
 28  /// There is currently no support for random access to tar archives.
 29  /// However, it seems that subclassing TarArchive, and using the
 30  /// TarBuffer.CurrentRecord and TarBuffer.CurrentBlock
 31  /// properties, this would be rather trivial.
 32  /// </summary>
 33  public class TarArchive : IDisposable
 34  {
 35    /// <summary>
 36    /// Client hook allowing detailed information to be reported during processing
 37    /// </summary>
 38    public event ProgressMessageHandler ProgressMessageEvent;
 39
 40    /// <summary>
 41    /// Raises the ProgressMessage event
 42    /// </summary>
 43    /// <param name="entry">The <see cref="TarEntry">TarEntry</see> for this event</param>
 44    /// <param name="message">message for this event.  Null is no message</param>
 45    protected virtual void OnProgressMessageEvent(TarEntry entry, string message)
 46    {
 047      ProgressMessageHandler handler = ProgressMessageEvent;
 048       if (handler != null) {
 049        handler(this, entry, message);
 50      }
 051    }
 52
 53    #region Constructors
 54    /// <summary>
 55    /// Constructor for a default <see cref="TarArchive"/>.
 56    /// </summary>
 057    protected TarArchive()
 58    {
 059    }
 60
 61    /// <summary>
 62    /// Initalise a TarArchive for input.
 63    /// </summary>
 64    /// <param name="stream">The <see cref="TarInputStream"/> to use for input.</param>
 165    protected TarArchive(TarInputStream stream)
 66    {
 167       if (stream == null) {
 068        throw new ArgumentNullException(nameof(stream));
 69      }
 70
 171      tarIn = stream;
 172    }
 73
 74    /// <summary>
 75    /// Initialise a TarArchive for output.
 76    /// </summary>
 77    /// <param name="stream">The <see cref="TarOutputStream"/> to use for output.</param>
 178    protected TarArchive(TarOutputStream stream)
 79    {
 180       if (stream == null) {
 081        throw new ArgumentNullException(nameof(stream));
 82      }
 83
 184      tarOut = stream;
 185    }
 86    #endregion
 87
 88    #region Static factory methods
 89    /// <summary>
 90    /// The InputStream based constructors create a TarArchive for the
 91    /// purposes of extracting or listing a tar archive. Thus, use
 92    /// these constructors when you wish to extract files from or list
 93    /// the contents of an existing tar archive.
 94    /// </summary>
 95    /// <param name="inputStream">The stream to retrieve archive data from.</param>
 96    /// <returns>Returns a new <see cref="TarArchive"/> suitable for reading from.</returns>
 97    public static TarArchive CreateInputTarArchive(Stream inputStream)
 98    {
 199       if (inputStream == null) {
 0100        throw new ArgumentNullException(nameof(inputStream));
 101      }
 102
 1103      var tarStream = inputStream as TarInputStream;
 104
 105      TarArchive result;
 1106       if (tarStream != null) {
 0107        result = new TarArchive(tarStream);
 0108      } else {
 1109        result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor);
 110      }
 1111      return result;
 112    }
 113
 114    /// <summary>
 115    /// Create TarArchive for reading setting block factor
 116    /// </summary>
 117    /// <param name="inputStream">A stream containing the tar archive contents</param>
 118    /// <param name="blockFactor">The blocking factor to apply</param>
 119    /// <returns>Returns a <see cref="TarArchive"/> suitable for reading.</returns>
 120    public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor)
 121    {
 1122       if (inputStream == null) {
 0123        throw new ArgumentNullException(nameof(inputStream));
 124      }
 125
 1126       if (inputStream is TarInputStream) {
 0127        throw new ArgumentException("TarInputStream not valid");
 128      }
 129
 1130      return new TarArchive(new TarInputStream(inputStream, blockFactor));
 131    }
 132
 133    /// <summary>
 134    /// Create a TarArchive for writing to, using the default blocking factor
 135    /// </summary>
 136    /// <param name="outputStream">The <see cref="Stream"/> to write to</param>
 137    /// <returns>Returns a <see cref="TarArchive"/> suitable for writing.</returns>
 138    public static TarArchive CreateOutputTarArchive(Stream outputStream)
 139    {
 1140       if (outputStream == null) {
 0141        throw new ArgumentNullException(nameof(outputStream));
 142      }
 143
 1144      var tarStream = outputStream as TarOutputStream;
 145
 146      TarArchive result;
 1147       if (tarStream != null) {
 0148        result = new TarArchive(tarStream);
 0149      } else {
 1150        result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor);
 151      }
 1152      return result;
 153    }
 154
 155    /// <summary>
 156    /// Create a <see cref="TarArchive">tar archive</see> for writing.
 157    /// </summary>
 158    /// <param name="outputStream">The stream to write to</param>
 159    /// <param name="blockFactor">The blocking factor to use for buffering.</param>
 160    /// <returns>Returns a <see cref="TarArchive"/> suitable for writing.</returns>
 161    public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor)
 162    {
 1163       if (outputStream == null) {
 0164        throw new ArgumentNullException(nameof(outputStream));
 165      }
 166
 1167       if (outputStream is TarOutputStream) {
 0168        throw new ArgumentException("TarOutputStream is not valid");
 169      }
 170
 1171      return new TarArchive(new TarOutputStream(outputStream, blockFactor));
 172    }
 173    #endregion
 174
 175    /// <summary>
 176    /// Set the flag that determines whether existing files are
 177    /// kept, or overwritten during extraction.
 178    /// </summary>
 179    /// <param name="keepExistingFiles">
 180    /// If true, do not overwrite existing files.
 181    /// </param>
 182    public void SetKeepOldFiles(bool keepExistingFiles)
 183    {
 0184       if (isDisposed) {
 0185        throw new ObjectDisposedException("TarArchive");
 186      }
 187
 0188      keepOldFiles = keepExistingFiles;
 0189    }
 190
 191    /// <summary>
 192    /// Get/set the ascii file translation flag. If ascii file translation
 193    /// is true, then the file is checked to see if it a binary file or not.
 194    /// If the flag is true and the test indicates it is ascii text
 195    /// file, it will be translated. The translation converts the local
 196    /// operating system's concept of line ends into the UNIX line end,
 197    /// '\n', which is the defacto standard for a TAR archive. This makes
 198    /// text files compatible with UNIX.
 199    /// </summary>
 200    public bool AsciiTranslate {
 201      get {
 0202         if (isDisposed) {
 0203          throw new ObjectDisposedException("TarArchive");
 204        }
 205
 0206        return asciiTranslate;
 207      }
 208
 209      set {
 0210         if (isDisposed) {
 0211          throw new ObjectDisposedException("TarArchive");
 212        }
 213
 0214        asciiTranslate = value;
 0215      }
 216
 217    }
 218
 219    /// <summary>
 220    /// Set the ascii file translation flag.
 221    /// </summary>
 222    /// <param name= "translateAsciiFiles">
 223    /// If true, translate ascii text files.
 224    /// </param>
 225    [Obsolete("Use the AsciiTranslate property")]
 226    public void SetAsciiTranslation(bool translateAsciiFiles)
 227    {
 0228       if (isDisposed) {
 0229        throw new ObjectDisposedException("TarArchive");
 230      }
 231
 0232      asciiTranslate = translateAsciiFiles;
 0233    }
 234
 235    /// <summary>
 236    /// PathPrefix is added to entry names as they are written if the value is not null.
 237    /// A slash character is appended after PathPrefix
 238    /// </summary>
 239    public string PathPrefix {
 240      get {
 0241         if (isDisposed) {
 0242          throw new ObjectDisposedException("TarArchive");
 243        }
 244
 0245        return pathPrefix;
 246      }
 247
 248      set {
 0249         if (isDisposed) {
 0250          throw new ObjectDisposedException("TarArchive");
 251        }
 252
 0253        pathPrefix = value;
 0254      }
 255
 256    }
 257
 258    /// <summary>
 259    /// RootPath is removed from entry names if it is found at the
 260    /// beginning of the name.
 261    /// </summary>
 262    public string RootPath {
 263      get {
 0264         if (isDisposed) {
 0265          throw new ObjectDisposedException("TarArchive");
 266        }
 267
 0268        return rootPath;
 269      }
 270
 271      set {
 0272         if (isDisposed) {
 0273          throw new ObjectDisposedException("TarArchive");
 274        }
 275        // Convert to forward slashes for matching. Trim trailing / for correct final path
 0276        rootPath = value.Replace('\\', '/').TrimEnd('/');
 0277      }
 278    }
 279
 280    /// <summary>
 281    /// Set user and group information that will be used to fill in the
 282    /// tar archive's entry headers. This information is based on that available
 283    /// for the linux operating system, which is not always available on other
 284    /// operating systems.  TarArchive allows the programmer to specify values
 285    /// to be used in their place.
 286    /// <see cref="ApplyUserInfoOverrides"/> is set to true by this call.
 287    /// </summary>
 288    /// <param name="userId">
 289    /// The user id to use in the headers.
 290    /// </param>
 291    /// <param name="userName">
 292    /// The user name to use in the headers.
 293    /// </param>
 294    /// <param name="groupId">
 295    /// The group id to use in the headers.
 296    /// </param>
 297    /// <param name="groupName">
 298    /// The group name to use in the headers.
 299    /// </param>
 300    public void SetUserInfo(int userId, string userName, int groupId, string groupName)
 301    {
 0302       if (isDisposed) {
 0303        throw new ObjectDisposedException("TarArchive");
 304      }
 305
 0306      this.userId = userId;
 0307      this.userName = userName;
 0308      this.groupId = groupId;
 0309      this.groupName = groupName;
 0310      applyUserInfoOverrides = true;
 0311    }
 312
 313    /// <summary>
 314    /// Get or set a value indicating if overrides defined by <see cref="SetUserInfo">SetUserInfo</see> should be applie
 315    /// </summary>
 316    /// <remarks>If overrides are not applied then the values as set in each header will be used.</remarks>
 317    public bool ApplyUserInfoOverrides {
 318      get {
 0319         if (isDisposed) {
 0320          throw new ObjectDisposedException("TarArchive");
 321        }
 322
 0323        return applyUserInfoOverrides;
 324      }
 325
 326      set {
 0327         if (isDisposed) {
 0328          throw new ObjectDisposedException("TarArchive");
 329        }
 330
 0331        applyUserInfoOverrides = value;
 0332      }
 333    }
 334
 335    /// <summary>
 336    /// Get the archive user id.
 337    /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
 338    /// on how to allow setting values on a per entry basis.
 339    /// </summary>
 340    /// <returns>
 341    /// The current user id.
 342    /// </returns>
 343    public int UserId {
 344      get {
 0345         if (isDisposed) {
 0346          throw new ObjectDisposedException("TarArchive");
 347        }
 348
 0349        return userId;
 350      }
 351    }
 352
 353    /// <summary>
 354    /// Get the archive user name.
 355    /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
 356    /// on how to allow setting values on a per entry basis.
 357    /// </summary>
 358    /// <returns>
 359    /// The current user name.
 360    /// </returns>
 361    public string UserName {
 362      get {
 0363         if (isDisposed) {
 0364          throw new ObjectDisposedException("TarArchive");
 365        }
 366
 0367        return userName;
 368      }
 369    }
 370
 371    /// <summary>
 372    /// Get the archive group id.
 373    /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
 374    /// on how to allow setting values on a per entry basis.
 375    /// </summary>
 376    /// <returns>
 377    /// The current group id.
 378    /// </returns>
 379    public int GroupId {
 380      get {
 0381         if (isDisposed) {
 0382          throw new ObjectDisposedException("TarArchive");
 383        }
 384
 0385        return groupId;
 386      }
 387    }
 388
 389    /// <summary>
 390    /// Get the archive group name.
 391    /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
 392    /// on how to allow setting values on a per entry basis.
 393    /// </summary>
 394    /// <returns>
 395    /// The current group name.
 396    /// </returns>
 397    public string GroupName {
 398      get {
 0399         if (isDisposed) {
 0400          throw new ObjectDisposedException("TarArchive");
 401        }
 402
 0403        return groupName;
 404      }
 405    }
 406
 407    /// <summary>
 408    /// Get the archive's record size. Tar archives are composed of
 409    /// a series of RECORDS each containing a number of BLOCKS.
 410    /// This allowed tar archives to match the IO characteristics of
 411    /// the physical device being used. Archives are expected
 412    /// to be properly "blocked".
 413    /// </summary>
 414    /// <returns>
 415    /// The record size this archive is using.
 416    /// </returns>
 417    public int RecordSize {
 418      get {
 1419         if (isDisposed) {
 0420          throw new ObjectDisposedException("TarArchive");
 421        }
 422
 1423         if (tarIn != null) {
 0424          return tarIn.RecordSize;
 1425         } else if (tarOut != null) {
 1426          return tarOut.RecordSize;
 427        }
 0428        return TarBuffer.DefaultRecordSize;
 429      }
 430    }
 431
 432    /// <summary>
 433    /// Sets the IsStreamOwner property on the underlying stream.
 434    /// Set this to false to prevent the Close of the TarArchive from closing the stream.
 435    /// </summary>
 436    public bool IsStreamOwner {
 437      set {
 0438         if (tarIn != null) {
 0439          tarIn.IsStreamOwner = value;
 0440        } else {
 0441          tarOut.IsStreamOwner = value;
 442        }
 0443      }
 444    }
 445
 446    /// <summary>
 447    /// Close the archive.
 448    /// </summary>
 449    [Obsolete("Use Close instead")]
 450    public void CloseArchive()
 451    {
 0452      Close();
 0453    }
 454
 455    /// <summary>
 456    /// Perform the "list" command for the archive contents.
 457    ///
 458    /// NOTE That this method uses the <see cref="ProgressMessageEvent"> progress event</see> to actually list
 459    /// the contents. If the progress display event is not set, nothing will be listed!
 460    /// </summary>
 461    public void ListContents()
 462    {
 1463       if (isDisposed) {
 0464        throw new ObjectDisposedException("TarArchive");
 465      }
 466
 0467      while (true) {
 1468        TarEntry entry = tarIn.GetNextEntry();
 469
 1470         if (entry == null) {
 471          break;
 472        }
 0473        OnProgressMessageEvent(entry, null);
 474      }
 1475    }
 476
 477    /// <summary>
 478    /// Perform the "extract" command and extract the contents of the archive.
 479    /// </summary>
 480    /// <param name="destinationDirectory">
 481    /// The destination directory into which to extract.
 482    /// </param>
 483    public void ExtractContents(string destinationDirectory)
 484    {
 0485       if (isDisposed) {
 0486        throw new ObjectDisposedException("TarArchive");
 487      }
 488
 0489      while (true) {
 0490        TarEntry entry = tarIn.GetNextEntry();
 491
 0492         if (entry == null) {
 493          break;
 494        }
 495
 0496         if (entry.TarHeader.TypeFlag == TarHeader.LF_LINK || entry.TarHeader.TypeFlag == TarHeader.LF_SYMLINK)
 497          continue;
 498
 0499        ExtractEntry(destinationDirectory, entry);
 500      }
 0501    }
 502
 503    /// <summary>
 504    /// Extract an entry from the archive. This method assumes that the
 505    /// tarIn stream has been properly set with a call to GetNextEntry().
 506    /// </summary>
 507    /// <param name="destDir">
 508    /// The destination directory into which to extract.
 509    /// </param>
 510    /// <param name="entry">
 511    /// The TarEntry returned by tarIn.GetNextEntry().
 512    /// </param>
 513    void ExtractEntry(string destDir, TarEntry entry)
 514    {
 0515      OnProgressMessageEvent(entry, null);
 516
 0517      string name = entry.Name;
 518
 0519       if (Path.IsPathRooted(name)) {
 520        // NOTE:
 521        // for UNC names...  \\machine\share\zoom\beet.txt gives \zoom\beet.txt
 0522        name = name.Substring(Path.GetPathRoot(name).Length);
 523      }
 524
 0525      name = name.Replace('/', Path.DirectorySeparatorChar);
 526
 0527      string destFile = Path.Combine(destDir, name);
 528
 0529       if (entry.IsDirectory) {
 0530        EnsureDirectoryExists(destFile);
 0531      } else {
 0532        string parentDirectory = Path.GetDirectoryName(destFile);
 0533        EnsureDirectoryExists(parentDirectory);
 534
 0535        bool process = true;
 0536        var fileInfo = new FileInfo(destFile);
 0537         if (fileInfo.Exists) {
 0538           if (keepOldFiles) {
 0539            OnProgressMessageEvent(entry, "Destination file already exists");
 0540            process = false;
 0541           } else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0) {
 0542            OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
 0543            process = false;
 544          }
 545        }
 546
 0547         if (process) {
 0548          bool asciiTrans = false;
 549
 0550          Stream outputStream = File.Create(destFile);
 0551           if (this.asciiTranslate) {
 0552            asciiTrans = !IsBinary(destFile);
 553          }
 554
 0555          StreamWriter outw = null;
 0556           if (asciiTrans) {
 0557            outw = new StreamWriter(outputStream);
 558          }
 559
 0560          byte[] rdbuf = new byte[32 * 1024];
 561
 0562          while (true) {
 0563            int numRead = tarIn.Read(rdbuf, 0, rdbuf.Length);
 564
 0565             if (numRead <= 0) {
 566              break;
 567            }
 568
 0569             if (asciiTrans) {
 0570               for (int off = 0, b = 0; b < numRead; ++b) {
 0571                 if (rdbuf[b] == 10) {
 0572                  string s = Encoding.ASCII.GetString(rdbuf, off, (b - off));
 0573                  outw.WriteLine(s);
 0574                  off = b + 1;
 575                }
 576              }
 0577            } else {
 0578              outputStream.Write(rdbuf, 0, numRead);
 579            }
 580          }
 581
 0582           if (asciiTrans) {
 0583            outw.Close();
 0584          } else {
 0585            outputStream.Close();
 586          }
 587        }
 588      }
 0589    }
 590
 591    /// <summary>
 592    /// Write an entry to the archive. This method will call the putNextEntry
 593    /// and then write the contents of the entry, and finally call closeEntry()
 594    /// for entries that are files. For directories, it will call putNextEntry(),
 595    /// and then, if the recurse flag is true, process each entry that is a
 596    /// child of the directory.
 597    /// </summary>
 598    /// <param name="sourceEntry">
 599    /// The TarEntry representing the entry to write to the archive.
 600    /// </param>
 601    /// <param name="recurse">
 602    /// If true, process the children of directory entries.
 603    /// </param>
 604    public void WriteEntry(TarEntry sourceEntry, bool recurse)
 605    {
 0606       if (sourceEntry == null) {
 0607        throw new ArgumentNullException(nameof(sourceEntry));
 608      }
 609
 0610       if (isDisposed) {
 0611        throw new ObjectDisposedException("TarArchive");
 612      }
 613
 614      try {
 0615         if (recurse) {
 0616          TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName,
 0617                         sourceEntry.GroupId, sourceEntry.GroupName);
 618        }
 0619        WriteEntryCore(sourceEntry, recurse);
 0620      } finally {
 0621         if (recurse) {
 0622          TarHeader.RestoreSetValues();
 623        }
 0624      }
 0625    }
 626
 627    /// <summary>
 628    /// Write an entry to the archive. This method will call the putNextEntry
 629    /// and then write the contents of the entry, and finally call closeEntry()
 630    /// for entries that are files. For directories, it will call putNextEntry(),
 631    /// and then, if the recurse flag is true, process each entry that is a
 632    /// child of the directory.
 633    /// </summary>
 634    /// <param name="sourceEntry">
 635    /// The TarEntry representing the entry to write to the archive.
 636    /// </param>
 637    /// <param name="recurse">
 638    /// If true, process the children of directory entries.
 639    /// </param>
 640    void WriteEntryCore(TarEntry sourceEntry, bool recurse)
 641    {
 0642      string tempFileName = null;
 0643      string entryFilename = sourceEntry.File;
 644
 0645      var entry = (TarEntry)sourceEntry.Clone();
 646
 0647       if (applyUserInfoOverrides) {
 0648        entry.GroupId = groupId;
 0649        entry.GroupName = groupName;
 0650        entry.UserId = userId;
 0651        entry.UserName = userName;
 652      }
 653
 0654      OnProgressMessageEvent(entry, null);
 655
 0656       if (asciiTranslate && !entry.IsDirectory) {
 657
 0658         if (!IsBinary(entryFilename)) {
 0659          tempFileName = Path.GetTempFileName();
 660
 0661          using (StreamReader inStream = File.OpenText(entryFilename)) {
 0662            using (Stream outStream = File.Create(tempFileName)) {
 663
 0664              while (true) {
 0665                string line = inStream.ReadLine();
 0666                 if (line == null) {
 667                  break;
 668                }
 0669                byte[] data = Encoding.ASCII.GetBytes(line);
 0670                outStream.Write(data, 0, data.Length);
 0671                outStream.WriteByte((byte)'\n');
 672              }
 673
 0674              outStream.Flush();
 0675            }
 676          }
 677
 0678          entry.Size = new FileInfo(tempFileName).Length;
 0679          entryFilename = tempFileName;
 680        }
 681      }
 682
 0683      string newName = null;
 684
 0685       if (rootPath != null) {
 0686         if (entry.Name.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase)) {
 0687          newName = entry.Name.Substring(rootPath.Length + 1);
 688        }
 689      }
 690
 0691       if (pathPrefix != null) {
 0692        newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName;
 693      }
 694
 0695       if (newName != null) {
 0696        entry.Name = newName;
 697      }
 698
 0699      tarOut.PutNextEntry(entry);
 700
 0701       if (entry.IsDirectory) {
 0702         if (recurse) {
 0703          TarEntry[] list = entry.GetDirectoryEntries();
 0704           for (int i = 0; i < list.Length; ++i) {
 0705            WriteEntryCore(list[i], recurse);
 706          }
 707        }
 0708      } else {
 0709        using (Stream inputStream = File.OpenRead(entryFilename)) {
 0710          byte[] localBuffer = new byte[32 * 1024];
 0711          while (true) {
 0712            int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);
 713
 0714             if (numRead <= 0) {
 0715              break;
 716            }
 717
 0718            tarOut.Write(localBuffer, 0, numRead);
 719          }
 720        }
 721
 0722         if (!string.IsNullOrEmpty(tempFileName)) {
 0723          File.Delete(tempFileName);
 724        }
 725
 0726        tarOut.CloseEntry();
 727      }
 0728    }
 729
 730    /// <summary>
 731    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 732    /// </summary>
 733    public void Dispose()
 734    {
 2735      Dispose(true);
 2736      GC.SuppressFinalize(this);
 2737    }
 738
 739    /// <summary>
 740    /// Releases the unmanaged resources used by the FileStream and optionally releases the managed resources.
 741    /// </summary>
 742    /// <param name="disposing">true to release both managed and unmanaged resources;
 743    /// false to release only unmanaged resources.</param>
 744    protected virtual void Dispose(bool disposing)
 745    {
 2746       if (!isDisposed) {
 2747        isDisposed = true;
 2748         if (disposing) {
 2749           if (tarOut != null) {
 1750            tarOut.Flush();
 1751            tarOut.Close();
 752          }
 753
 2754           if (tarIn != null) {
 1755            tarIn.Close();
 756          }
 757        }
 758      }
 2759    }
 760
 761    /// <summary>
 762    /// Closes the archive and releases any associated resources.
 763    /// </summary>
 764    public virtual void Close()
 765    {
 0766      Dispose(true);
 0767    }
 768
 769    /// <summary>
 770    /// Ensures that resources are freed and other cleanup operations are performed
 771    /// when the garbage collector reclaims the <see cref="TarArchive"/>.
 772    /// </summary>
 773    ~TarArchive()
 774    {
 0775      Dispose(false);
 0776    }
 777
 778    static void EnsureDirectoryExists(string directoryName)
 779    {
 0780       if (!Directory.Exists(directoryName)) {
 781        try {
 0782          Directory.CreateDirectory(directoryName);
 0783        } catch (Exception e) {
 0784          throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message, e);
 785        }
 786      }
 0787    }
 788
 789    // TODO: TarArchive - Is there a better way to test for a text file?
 790    // It no longer reads entire files into memory but is still a weak test!
 791    // This assumes that byte values 0-7, 14-31 or 255 are binary
 792    // and that all non text files contain one of these values
 793    static bool IsBinary(string filename)
 794    {
 0795      using (FileStream fs = File.OpenRead(filename)) {
 0796        int sampleSize = Math.Min(4096, (int)fs.Length);
 0797        byte[] content = new byte[sampleSize];
 798
 0799        int bytesRead = fs.Read(content, 0, sampleSize);
 800
 0801         for (int i = 0; i < bytesRead; ++i) {
 0802          byte b = content[i];
 0803           if ((b < 8) || ((b > 13) && (b < 32)) || (b == 255)) {
 0804            return true;
 805          }
 806        }
 0807      }
 0808      return false;
 0809    }
 810
 811    #region Instance Fields
 812    bool keepOldFiles;
 813    bool asciiTranslate;
 814
 815    int userId;
 2816    string userName = string.Empty;
 817    int groupId;
 2818    string groupName = string.Empty;
 819
 820    string rootPath;
 821    string pathPrefix;
 822
 823    bool applyUserInfoOverrides;
 824
 825    TarInputStream tarIn;
 826    TarOutputStream tarOut;
 827    bool isDisposed;
 828    #endregion
 829  }
 830}