Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Text;
  5.  
  6. namespace TarExample
  7. {
  8.  
  9. public class Tar
  10. {
  11. /// <summary>
  12. /// Extracts a <i>.tar.gz</i> archive to the specified directory.
  13. /// </summary>
  14. /// <param name="filename">The <i>.tar.gz</i> to decompress and extract.</param>
  15. /// <param name="outputDir">Output directory to write the files.</param>
  16. public static void ExtractTarGz(string filename, string outputDir)
  17. {
  18. using (var stream = File.OpenRead(filename))
  19. ExtractTarGz(stream, outputDir);
  20. }
  21.  
  22. /// <summary>
  23. /// Extracts a <i>.tar.gz</i> archive stream to the specified directory.
  24. /// </summary>
  25. /// <param name="stream">The <i>.tar.gz</i> to decompress and extract.</param>
  26. /// <param name="outputDir">Output directory to write the files.</param>
  27. public static void ExtractTarGz(Stream stream, string outputDir)
  28. {
  29. // A GZipStream is not seekable, so copy it first to a MemoryStream
  30. using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
  31. {
  32. const int chunk = 4096;
  33. using (var memStr = new MemoryStream())
  34. {
  35. int read;
  36. var buffer = new byte[chunk];
  37. do
  38. {
  39. read = gzip.Read(buffer, 0, chunk);
  40. memStr.Write(buffer, 0, read);
  41. } while (read == chunk);
  42.  
  43. memStr.Seek(0, SeekOrigin.Begin);
  44. ExtractTar(memStr, outputDir);
  45. }
  46. }
  47. }
  48.  
  49. /// <summary>
  50. /// Extractes a <c>tar</c> archive to the specified directory.
  51. /// </summary>
  52. /// <param name="filename">The <i>.tar</i> to extract.</param>
  53. /// <param name="outputDir">Output directory to write the files.</param>
  54. public static void ExtractTar(string filename, string outputDir)
  55. {
  56. using (var stream = File.OpenRead(filename))
  57. ExtractTar(stream, outputDir);
  58. }
  59.  
  60. /// <summary>
  61. /// Extractes a <c>tar</c> archive to the specified directory.
  62. /// </summary>
  63. /// <param name="stream">The <i>.tar</i> to extract.</param>
  64. /// <param name="outputDir">Output directory to write the files.</param>
  65. public static void ExtractTar(Stream stream, string outputDir)
  66. {
  67. var buffer = new byte[100];
  68. while (true)
  69. {
  70. stream.Read(buffer, 0, 100);
  71. var name = Encoding.ASCII.GetString(buffer).Trim('\0');
  72. if (name == null || name == "")
  73. break;
  74. stream.Seek(24, SeekOrigin.Current);
  75. stream.Read(buffer, 0, 12);
  76. var size = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, 12).Trim('\0').Trim(), 8);
  77.  
  78.  
  79. stream.Seek(376L, SeekOrigin.Current);
  80.  
  81. var output = Path.Combine(outputDir, name);
  82. if (!Directory.Exists(Path.GetDirectoryName(output)))
  83. Directory.CreateDirectory(Path.GetDirectoryName(output));
  84. if (!name.EndsWith("/"))
  85. {
  86. using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.Write))
  87. {
  88. var buf = new byte[size];
  89. stream.Read(buf, 0, buf.Length);
  90. str.Write(buf, 0, buf.Length);
  91. }
  92. }
  93.  
  94. var pos = stream.Position;
  95.  
  96. var offset = 512 - (pos % 512);
  97. if (offset == 512)
  98. offset = 0;
  99.  
  100. stream.Seek(offset, SeekOrigin.Current);
  101. }
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement