| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Text; |
| | 4 | |
|
| | 5 | | namespace 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 | | { |
| 0 | 47 | | ProgressMessageHandler handler = ProgressMessageEvent; |
| 0 | 48 | | if (handler != null) { |
| 0 | 49 | | handler(this, entry, message); |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | #region Constructors |
| | 54 | | /// <summary> |
| | 55 | | /// Constructor for a default <see cref="TarArchive"/>. |
| | 56 | | /// </summary> |
| 0 | 57 | | protected TarArchive() |
| | 58 | | { |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Initalise a TarArchive for input. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="stream">The <see cref="TarInputStream"/> to use for input.</param> |
| 1 | 65 | | protected TarArchive(TarInputStream stream) |
| | 66 | | { |
| 1 | 67 | | if (stream == null) { |
| 0 | 68 | | throw new ArgumentNullException(nameof(stream)); |
| | 69 | | } |
| | 70 | |
|
| 1 | 71 | | tarIn = stream; |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Initialise a TarArchive for output. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="stream">The <see cref="TarOutputStream"/> to use for output.</param> |
| 1 | 78 | | protected TarArchive(TarOutputStream stream) |
| | 79 | | { |
| 1 | 80 | | if (stream == null) { |
| 0 | 81 | | throw new ArgumentNullException(nameof(stream)); |
| | 82 | | } |
| | 83 | |
|
| 1 | 84 | | tarOut = stream; |
| 1 | 85 | | } |
| | 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 | | { |
| 1 | 99 | | if (inputStream == null) { |
| 0 | 100 | | throw new ArgumentNullException(nameof(inputStream)); |
| | 101 | | } |
| | 102 | |
|
| 1 | 103 | | var tarStream = inputStream as TarInputStream; |
| | 104 | |
|
| | 105 | | TarArchive result; |
| 1 | 106 | | if (tarStream != null) { |
| 0 | 107 | | result = new TarArchive(tarStream); |
| 0 | 108 | | } else { |
| 1 | 109 | | result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor); |
| | 110 | | } |
| 1 | 111 | | 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 | | { |
| 1 | 122 | | if (inputStream == null) { |
| 0 | 123 | | throw new ArgumentNullException(nameof(inputStream)); |
| | 124 | | } |
| | 125 | |
|
| 1 | 126 | | if (inputStream is TarInputStream) { |
| 0 | 127 | | throw new ArgumentException("TarInputStream not valid"); |
| | 128 | | } |
| | 129 | |
|
| 1 | 130 | | 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 | | { |
| 1 | 140 | | if (outputStream == null) { |
| 0 | 141 | | throw new ArgumentNullException(nameof(outputStream)); |
| | 142 | | } |
| | 143 | |
|
| 1 | 144 | | var tarStream = outputStream as TarOutputStream; |
| | 145 | |
|
| | 146 | | TarArchive result; |
| 1 | 147 | | if (tarStream != null) { |
| 0 | 148 | | result = new TarArchive(tarStream); |
| 0 | 149 | | } else { |
| 1 | 150 | | result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor); |
| | 151 | | } |
| 1 | 152 | | 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 | | { |
| 1 | 163 | | if (outputStream == null) { |
| 0 | 164 | | throw new ArgumentNullException(nameof(outputStream)); |
| | 165 | | } |
| | 166 | |
|
| 1 | 167 | | if (outputStream is TarOutputStream) { |
| 0 | 168 | | throw new ArgumentException("TarOutputStream is not valid"); |
| | 169 | | } |
| | 170 | |
|
| 1 | 171 | | 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 | | { |
| 0 | 184 | | if (isDisposed) { |
| 0 | 185 | | throw new ObjectDisposedException("TarArchive"); |
| | 186 | | } |
| | 187 | |
|
| 0 | 188 | | keepOldFiles = keepExistingFiles; |
| 0 | 189 | | } |
| | 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 { |
| 0 | 202 | | if (isDisposed) { |
| 0 | 203 | | throw new ObjectDisposedException("TarArchive"); |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | return asciiTranslate; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | set { |
| 0 | 210 | | if (isDisposed) { |
| 0 | 211 | | throw new ObjectDisposedException("TarArchive"); |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | asciiTranslate = value; |
| 0 | 215 | | } |
| | 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 | | { |
| 0 | 228 | | if (isDisposed) { |
| 0 | 229 | | throw new ObjectDisposedException("TarArchive"); |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | asciiTranslate = translateAsciiFiles; |
| 0 | 233 | | } |
| | 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 { |
| 0 | 241 | | if (isDisposed) { |
| 0 | 242 | | throw new ObjectDisposedException("TarArchive"); |
| | 243 | | } |
| | 244 | |
|
| 0 | 245 | | return pathPrefix; |
| | 246 | | } |
| | 247 | |
|
| | 248 | | set { |
| 0 | 249 | | if (isDisposed) { |
| 0 | 250 | | throw new ObjectDisposedException("TarArchive"); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | pathPrefix = value; |
| 0 | 254 | | } |
| | 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 { |
| 0 | 264 | | if (isDisposed) { |
| 0 | 265 | | throw new ObjectDisposedException("TarArchive"); |
| | 266 | | } |
| | 267 | |
|
| 0 | 268 | | return rootPath; |
| | 269 | | } |
| | 270 | |
|
| | 271 | | set { |
| 0 | 272 | | if (isDisposed) { |
| 0 | 273 | | throw new ObjectDisposedException("TarArchive"); |
| | 274 | | } |
| | 275 | | // Convert to forward slashes for matching. Trim trailing / for correct final path |
| 0 | 276 | | rootPath = value.Replace('\\', '/').TrimEnd('/'); |
| 0 | 277 | | } |
| | 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 | | { |
| 0 | 302 | | if (isDisposed) { |
| 0 | 303 | | throw new ObjectDisposedException("TarArchive"); |
| | 304 | | } |
| | 305 | |
|
| 0 | 306 | | this.userId = userId; |
| 0 | 307 | | this.userName = userName; |
| 0 | 308 | | this.groupId = groupId; |
| 0 | 309 | | this.groupName = groupName; |
| 0 | 310 | | applyUserInfoOverrides = true; |
| 0 | 311 | | } |
| | 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 { |
| 0 | 319 | | if (isDisposed) { |
| 0 | 320 | | throw new ObjectDisposedException("TarArchive"); |
| | 321 | | } |
| | 322 | |
|
| 0 | 323 | | return applyUserInfoOverrides; |
| | 324 | | } |
| | 325 | |
|
| | 326 | | set { |
| 0 | 327 | | if (isDisposed) { |
| 0 | 328 | | throw new ObjectDisposedException("TarArchive"); |
| | 329 | | } |
| | 330 | |
|
| 0 | 331 | | applyUserInfoOverrides = value; |
| 0 | 332 | | } |
| | 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 { |
| 0 | 345 | | if (isDisposed) { |
| 0 | 346 | | throw new ObjectDisposedException("TarArchive"); |
| | 347 | | } |
| | 348 | |
|
| 0 | 349 | | 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 { |
| 0 | 363 | | if (isDisposed) { |
| 0 | 364 | | throw new ObjectDisposedException("TarArchive"); |
| | 365 | | } |
| | 366 | |
|
| 0 | 367 | | 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 { |
| 0 | 381 | | if (isDisposed) { |
| 0 | 382 | | throw new ObjectDisposedException("TarArchive"); |
| | 383 | | } |
| | 384 | |
|
| 0 | 385 | | 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 { |
| 0 | 399 | | if (isDisposed) { |
| 0 | 400 | | throw new ObjectDisposedException("TarArchive"); |
| | 401 | | } |
| | 402 | |
|
| 0 | 403 | | 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 { |
| 1 | 419 | | if (isDisposed) { |
| 0 | 420 | | throw new ObjectDisposedException("TarArchive"); |
| | 421 | | } |
| | 422 | |
|
| 1 | 423 | | if (tarIn != null) { |
| 0 | 424 | | return tarIn.RecordSize; |
| 1 | 425 | | } else if (tarOut != null) { |
| 1 | 426 | | return tarOut.RecordSize; |
| | 427 | | } |
| 0 | 428 | | 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 { |
| 0 | 438 | | if (tarIn != null) { |
| 0 | 439 | | tarIn.IsStreamOwner = value; |
| 0 | 440 | | } else { |
| 0 | 441 | | tarOut.IsStreamOwner = value; |
| | 442 | | } |
| 0 | 443 | | } |
| | 444 | | } |
| | 445 | |
|
| | 446 | | /// <summary> |
| | 447 | | /// Close the archive. |
| | 448 | | /// </summary> |
| | 449 | | [Obsolete("Use Close instead")] |
| | 450 | | public void CloseArchive() |
| | 451 | | { |
| 0 | 452 | | Close(); |
| 0 | 453 | | } |
| | 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 | | { |
| 1 | 463 | | if (isDisposed) { |
| 0 | 464 | | throw new ObjectDisposedException("TarArchive"); |
| | 465 | | } |
| | 466 | |
|
| 0 | 467 | | while (true) { |
| 1 | 468 | | TarEntry entry = tarIn.GetNextEntry(); |
| | 469 | |
|
| 1 | 470 | | if (entry == null) { |
| | 471 | | break; |
| | 472 | | } |
| 0 | 473 | | OnProgressMessageEvent(entry, null); |
| | 474 | | } |
| 1 | 475 | | } |
| | 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 | | { |
| 0 | 485 | | if (isDisposed) { |
| 0 | 486 | | throw new ObjectDisposedException("TarArchive"); |
| | 487 | | } |
| | 488 | |
|
| 0 | 489 | | while (true) { |
| 0 | 490 | | TarEntry entry = tarIn.GetNextEntry(); |
| | 491 | |
|
| 0 | 492 | | if (entry == null) { |
| | 493 | | break; |
| | 494 | | } |
| | 495 | |
|
| 0 | 496 | | if (entry.TarHeader.TypeFlag == TarHeader.LF_LINK || entry.TarHeader.TypeFlag == TarHeader.LF_SYMLINK) |
| | 497 | | continue; |
| | 498 | |
|
| 0 | 499 | | ExtractEntry(destinationDirectory, entry); |
| | 500 | | } |
| 0 | 501 | | } |
| | 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 | | { |
| 0 | 515 | | OnProgressMessageEvent(entry, null); |
| | 516 | |
|
| 0 | 517 | | string name = entry.Name; |
| | 518 | |
|
| 0 | 519 | | if (Path.IsPathRooted(name)) { |
| | 520 | | // NOTE: |
| | 521 | | // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt |
| 0 | 522 | | name = name.Substring(Path.GetPathRoot(name).Length); |
| | 523 | | } |
| | 524 | |
|
| 0 | 525 | | name = name.Replace('/', Path.DirectorySeparatorChar); |
| | 526 | |
|
| 0 | 527 | | string destFile = Path.Combine(destDir, name); |
| | 528 | |
|
| 0 | 529 | | if (entry.IsDirectory) { |
| 0 | 530 | | EnsureDirectoryExists(destFile); |
| 0 | 531 | | } else { |
| 0 | 532 | | string parentDirectory = Path.GetDirectoryName(destFile); |
| 0 | 533 | | EnsureDirectoryExists(parentDirectory); |
| | 534 | |
|
| 0 | 535 | | bool process = true; |
| 0 | 536 | | var fileInfo = new FileInfo(destFile); |
| 0 | 537 | | if (fileInfo.Exists) { |
| 0 | 538 | | if (keepOldFiles) { |
| 0 | 539 | | OnProgressMessageEvent(entry, "Destination file already exists"); |
| 0 | 540 | | process = false; |
| 0 | 541 | | } else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0) { |
| 0 | 542 | | OnProgressMessageEvent(entry, "Destination file already exists, and is read-only"); |
| 0 | 543 | | process = false; |
| | 544 | | } |
| | 545 | | } |
| | 546 | |
|
| 0 | 547 | | if (process) { |
| 0 | 548 | | bool asciiTrans = false; |
| | 549 | |
|
| 0 | 550 | | Stream outputStream = File.Create(destFile); |
| 0 | 551 | | if (this.asciiTranslate) { |
| 0 | 552 | | asciiTrans = !IsBinary(destFile); |
| | 553 | | } |
| | 554 | |
|
| 0 | 555 | | StreamWriter outw = null; |
| 0 | 556 | | if (asciiTrans) { |
| 0 | 557 | | outw = new StreamWriter(outputStream); |
| | 558 | | } |
| | 559 | |
|
| 0 | 560 | | byte[] rdbuf = new byte[32 * 1024]; |
| | 561 | |
|
| 0 | 562 | | while (true) { |
| 0 | 563 | | int numRead = tarIn.Read(rdbuf, 0, rdbuf.Length); |
| | 564 | |
|
| 0 | 565 | | if (numRead <= 0) { |
| | 566 | | break; |
| | 567 | | } |
| | 568 | |
|
| 0 | 569 | | if (asciiTrans) { |
| 0 | 570 | | for (int off = 0, b = 0; b < numRead; ++b) { |
| 0 | 571 | | if (rdbuf[b] == 10) { |
| 0 | 572 | | string s = Encoding.ASCII.GetString(rdbuf, off, (b - off)); |
| 0 | 573 | | outw.WriteLine(s); |
| 0 | 574 | | off = b + 1; |
| | 575 | | } |
| | 576 | | } |
| 0 | 577 | | } else { |
| 0 | 578 | | outputStream.Write(rdbuf, 0, numRead); |
| | 579 | | } |
| | 580 | | } |
| | 581 | |
|
| 0 | 582 | | if (asciiTrans) { |
| 0 | 583 | | outw.Close(); |
| 0 | 584 | | } else { |
| 0 | 585 | | outputStream.Close(); |
| | 586 | | } |
| | 587 | | } |
| | 588 | | } |
| 0 | 589 | | } |
| | 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 | | { |
| 0 | 606 | | if (sourceEntry == null) { |
| 0 | 607 | | throw new ArgumentNullException(nameof(sourceEntry)); |
| | 608 | | } |
| | 609 | |
|
| 0 | 610 | | if (isDisposed) { |
| 0 | 611 | | throw new ObjectDisposedException("TarArchive"); |
| | 612 | | } |
| | 613 | |
|
| | 614 | | try { |
| 0 | 615 | | if (recurse) { |
| 0 | 616 | | TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName, |
| 0 | 617 | | sourceEntry.GroupId, sourceEntry.GroupName); |
| | 618 | | } |
| 0 | 619 | | WriteEntryCore(sourceEntry, recurse); |
| 0 | 620 | | } finally { |
| 0 | 621 | | if (recurse) { |
| 0 | 622 | | TarHeader.RestoreSetValues(); |
| | 623 | | } |
| 0 | 624 | | } |
| 0 | 625 | | } |
| | 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 | | { |
| 0 | 642 | | string tempFileName = null; |
| 0 | 643 | | string entryFilename = sourceEntry.File; |
| | 644 | |
|
| 0 | 645 | | var entry = (TarEntry)sourceEntry.Clone(); |
| | 646 | |
|
| 0 | 647 | | if (applyUserInfoOverrides) { |
| 0 | 648 | | entry.GroupId = groupId; |
| 0 | 649 | | entry.GroupName = groupName; |
| 0 | 650 | | entry.UserId = userId; |
| 0 | 651 | | entry.UserName = userName; |
| | 652 | | } |
| | 653 | |
|
| 0 | 654 | | OnProgressMessageEvent(entry, null); |
| | 655 | |
|
| 0 | 656 | | if (asciiTranslate && !entry.IsDirectory) { |
| | 657 | |
|
| 0 | 658 | | if (!IsBinary(entryFilename)) { |
| 0 | 659 | | tempFileName = Path.GetTempFileName(); |
| | 660 | |
|
| 0 | 661 | | using (StreamReader inStream = File.OpenText(entryFilename)) { |
| 0 | 662 | | using (Stream outStream = File.Create(tempFileName)) { |
| | 663 | |
|
| 0 | 664 | | while (true) { |
| 0 | 665 | | string line = inStream.ReadLine(); |
| 0 | 666 | | if (line == null) { |
| | 667 | | break; |
| | 668 | | } |
| 0 | 669 | | byte[] data = Encoding.ASCII.GetBytes(line); |
| 0 | 670 | | outStream.Write(data, 0, data.Length); |
| 0 | 671 | | outStream.WriteByte((byte)'\n'); |
| | 672 | | } |
| | 673 | |
|
| 0 | 674 | | outStream.Flush(); |
| 0 | 675 | | } |
| | 676 | | } |
| | 677 | |
|
| 0 | 678 | | entry.Size = new FileInfo(tempFileName).Length; |
| 0 | 679 | | entryFilename = tempFileName; |
| | 680 | | } |
| | 681 | | } |
| | 682 | |
|
| 0 | 683 | | string newName = null; |
| | 684 | |
|
| 0 | 685 | | if (rootPath != null) { |
| 0 | 686 | | if (entry.Name.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase)) { |
| 0 | 687 | | newName = entry.Name.Substring(rootPath.Length + 1); |
| | 688 | | } |
| | 689 | | } |
| | 690 | |
|
| 0 | 691 | | if (pathPrefix != null) { |
| 0 | 692 | | newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName; |
| | 693 | | } |
| | 694 | |
|
| 0 | 695 | | if (newName != null) { |
| 0 | 696 | | entry.Name = newName; |
| | 697 | | } |
| | 698 | |
|
| 0 | 699 | | tarOut.PutNextEntry(entry); |
| | 700 | |
|
| 0 | 701 | | if (entry.IsDirectory) { |
| 0 | 702 | | if (recurse) { |
| 0 | 703 | | TarEntry[] list = entry.GetDirectoryEntries(); |
| 0 | 704 | | for (int i = 0; i < list.Length; ++i) { |
| 0 | 705 | | WriteEntryCore(list[i], recurse); |
| | 706 | | } |
| | 707 | | } |
| 0 | 708 | | } else { |
| 0 | 709 | | using (Stream inputStream = File.OpenRead(entryFilename)) { |
| 0 | 710 | | byte[] localBuffer = new byte[32 * 1024]; |
| 0 | 711 | | while (true) { |
| 0 | 712 | | int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length); |
| | 713 | |
|
| 0 | 714 | | if (numRead <= 0) { |
| 0 | 715 | | break; |
| | 716 | | } |
| | 717 | |
|
| 0 | 718 | | tarOut.Write(localBuffer, 0, numRead); |
| | 719 | | } |
| | 720 | | } |
| | 721 | |
|
| 0 | 722 | | if (!string.IsNullOrEmpty(tempFileName)) { |
| 0 | 723 | | File.Delete(tempFileName); |
| | 724 | | } |
| | 725 | |
|
| 0 | 726 | | tarOut.CloseEntry(); |
| | 727 | | } |
| 0 | 728 | | } |
| | 729 | |
|
| | 730 | | /// <summary> |
| | 731 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | 732 | | /// </summary> |
| | 733 | | public void Dispose() |
| | 734 | | { |
| 2 | 735 | | Dispose(true); |
| 2 | 736 | | GC.SuppressFinalize(this); |
| 2 | 737 | | } |
| | 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 | | { |
| 2 | 746 | | if (!isDisposed) { |
| 2 | 747 | | isDisposed = true; |
| 2 | 748 | | if (disposing) { |
| 2 | 749 | | if (tarOut != null) { |
| 1 | 750 | | tarOut.Flush(); |
| 1 | 751 | | tarOut.Close(); |
| | 752 | | } |
| | 753 | |
|
| 2 | 754 | | if (tarIn != null) { |
| 1 | 755 | | tarIn.Close(); |
| | 756 | | } |
| | 757 | | } |
| | 758 | | } |
| 2 | 759 | | } |
| | 760 | |
|
| | 761 | | /// <summary> |
| | 762 | | /// Closes the archive and releases any associated resources. |
| | 763 | | /// </summary> |
| | 764 | | public virtual void Close() |
| | 765 | | { |
| 0 | 766 | | Dispose(true); |
| 0 | 767 | | } |
| | 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 | | { |
| 0 | 775 | | Dispose(false); |
| 0 | 776 | | } |
| | 777 | |
|
| | 778 | | static void EnsureDirectoryExists(string directoryName) |
| | 779 | | { |
| 0 | 780 | | if (!Directory.Exists(directoryName)) { |
| | 781 | | try { |
| 0 | 782 | | Directory.CreateDirectory(directoryName); |
| 0 | 783 | | } catch (Exception e) { |
| 0 | 784 | | throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message, e); |
| | 785 | | } |
| | 786 | | } |
| 0 | 787 | | } |
| | 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 | | { |
| 0 | 795 | | using (FileStream fs = File.OpenRead(filename)) { |
| 0 | 796 | | int sampleSize = Math.Min(4096, (int)fs.Length); |
| 0 | 797 | | byte[] content = new byte[sampleSize]; |
| | 798 | |
|
| 0 | 799 | | int bytesRead = fs.Read(content, 0, sampleSize); |
| | 800 | |
|
| 0 | 801 | | for (int i = 0; i < bytesRead; ++i) { |
| 0 | 802 | | byte b = content[i]; |
| 0 | 803 | | if ((b < 8) || ((b > 13) && (b < 32)) || (b == 255)) { |
| 0 | 804 | | return true; |
| | 805 | | } |
| | 806 | | } |
| 0 | 807 | | } |
| 0 | 808 | | return false; |
| 0 | 809 | | } |
| | 810 | |
|
| | 811 | | #region Instance Fields |
| | 812 | | bool keepOldFiles; |
| | 813 | | bool asciiTranslate; |
| | 814 | |
|
| | 815 | | int userId; |
| 2 | 816 | | string userName = string.Empty; |
| | 817 | | int groupId; |
| 2 | 818 | | 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 | | } |