| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using System.IO.Compression; |
| | 8 | | using System.Text; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace GDX.IO.Compression |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Provides static methods for extracting tar files and tarballs. |
| | 15 | | /// </summary> |
| | 16 | | public static class TarFile |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Extracts all the files in the specified tar/tarball to a directory on the file system. |
| | 20 | | /// </summary> |
| | 21 | | /// <example> |
| | 22 | | /// A synchronous approach to extracting the contents of a file, to a folder: |
| | 23 | | /// <code>TarFile.ExtractToDirectory("C:\Temp\DownloadCache.tar.gz", "C:\Saved");</code> |
| | 24 | | /// </example> |
| | 25 | | /// <param name="sourceArchiveFileName">The path to the archive that is to be extracted.</param> |
| | 26 | | /// <param name="destinationDirectoryName"> |
| | 27 | | /// The path to the directory in which to place the extracted files, specified as a |
| | 28 | | /// relative or absolute path. A relative path is interpreted as relative to the current working directory. |
| | 29 | | /// </param> |
| | 30 | | /// <param name="forceGZipDataFormat">Enforce inflating the file via a <see cref="GZipStream" />.</param> |
| | 31 | | public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, |
| | 32 | | bool forceGZipDataFormat = false) |
| 0 | 33 | | { |
| | 34 | | const int k_ReadBufferSize = 4096; |
| | 35 | |
|
| | 36 | | // We need to handle the gzip first before we address the archive itself |
| 0 | 37 | | if (forceGZipDataFormat || |
| | 38 | | sourceArchiveFileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase)) |
| 0 | 39 | | { |
| 0 | 40 | | using FileStream stream = File.OpenRead(sourceArchiveFileName); |
| 0 | 41 | | using GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress); |
| 0 | 42 | | using MemoryStream memoryStream = new MemoryStream(); |
| | 43 | | // Loop through the stream |
| | 44 | | int readByteCount; |
| 0 | 45 | | byte[] readBuffer = new byte[k_ReadBufferSize]; |
| | 46 | | do |
| 0 | 47 | | { |
| 0 | 48 | | readByteCount = gzip.Read(readBuffer, 0, k_ReadBufferSize); |
| 0 | 49 | | memoryStream.Write(readBuffer, 0, readByteCount); |
| 0 | 50 | | } while (readByteCount == k_ReadBufferSize); |
| | 51 | |
|
| 0 | 52 | | memoryStream.Seek(0, SeekOrigin.Begin); |
| 0 | 53 | | ExtractStream(memoryStream, destinationDirectoryName); |
| 0 | 54 | | } |
| | 55 | | else |
| 0 | 56 | | { |
| 0 | 57 | | using FileStream fileStream = File.OpenRead(sourceArchiveFileName); |
| 0 | 58 | | ExtractStream(fileStream, destinationDirectoryName); |
| 0 | 59 | | } |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Extract a tar formatted <see cref="Stream" /> to the <paramref name="destinationDirectoryName" />. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="sourceStream">The <see cref="Stream" /> which to extract from.</param> |
| | 67 | | /// <param name="destinationDirectoryName">Output directory to write the files.</param> |
| | 68 | | public static void ExtractStream(Stream sourceStream, string destinationDirectoryName) |
| 0 | 69 | | { |
| | 70 | | const int k_ReadBufferSize = 100; |
| | 71 | | const int k_ContentOffset = 512; |
| 0 | 72 | | byte[] readBuffer = new byte[k_ReadBufferSize]; |
| 0 | 73 | | while (true) |
| 0 | 74 | | { |
| | 75 | | // ReSharper disable once MustUseReturnValue |
| 0 | 76 | | sourceStream.Read(readBuffer, 0, k_ReadBufferSize); |
| 0 | 77 | | string currentName = Encoding.ASCII.GetString(readBuffer).Trim('\0'); |
| | 78 | |
|
| 0 | 79 | | if (string.IsNullOrWhiteSpace(currentName)) |
| 0 | 80 | | { |
| 0 | 81 | | break; |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | string destinationFilePath = Path.Combine(destinationDirectoryName, currentName); |
| 0 | 85 | | sourceStream.Seek(24, SeekOrigin.Current); |
| 0 | 86 | | int readByteCount = sourceStream.Read(readBuffer, 0, 12); |
| 0 | 87 | | if (readByteCount != 12) |
| 0 | 88 | | { |
| 0 | 89 | | Debug.LogError($"Unable to read filesize from header. {readByteCount.ToString()} read, expected 12." |
| 0 | 90 | | break; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | long fileSize = Convert.ToInt64(Encoding.UTF8.GetString(readBuffer, 0, 12).Trim('\0').Trim(), 8); |
| 0 | 94 | | sourceStream.Seek(376L, SeekOrigin.Current); |
| | 95 | |
|
| | 96 | | // Do we need to make a directory? |
| 0 | 97 | | string parentDirectory = Path.GetDirectoryName(destinationFilePath); |
| 0 | 98 | | if (parentDirectory != null && !Directory.Exists(parentDirectory)) |
| 0 | 99 | | { |
| 0 | 100 | | DirectoryInfo directoryInfo = Directory.CreateDirectory(parentDirectory); |
| 0 | 101 | | directoryInfo.Attributes &= ~FileAttributes.ReadOnly; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | // Don't try to make directories as files |
| 0 | 105 | | if (!currentName.Equals("./", StringComparison.InvariantCulture) && |
| | 106 | | !currentName.EndsWith("/") && |
| | 107 | | !currentName.EndsWith("\\")) |
| 0 | 108 | | { |
| 0 | 109 | | using FileStream newFileStream = |
| | 110 | | File.Open(destinationFilePath, FileMode.OpenOrCreate, FileAccess.Write); |
| 0 | 111 | | byte[] fileContentBuffer = new byte[fileSize]; |
| 0 | 112 | | int newFileContentBufferLength = fileContentBuffer.Length; |
| 0 | 113 | | readByteCount = sourceStream.Read(fileContentBuffer, 0, newFileContentBufferLength); |
| 0 | 114 | | if (readByteCount != newFileContentBufferLength) |
| 0 | 115 | | { |
| 0 | 116 | | Debug.LogWarning( |
| | 117 | | $"Read file size of {readByteCount.ToString()} does not match the expected {newFileContentBu |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | newFileStream.Write(fileContentBuffer, 0, newFileContentBufferLength); |
| 0 | 121 | | } |
| | 122 | |
|
| 0 | 123 | | long nextOffset = k_ContentOffset - sourceStream.Position % k_ContentOffset; |
| 0 | 124 | | if (nextOffset == k_ContentOffset) |
| 0 | 125 | | { |
| 0 | 126 | | nextOffset = 0; |
| 0 | 127 | | } |
| | 128 | |
|
| 0 | 129 | | sourceStream.Seek(nextOffset, SeekOrigin.Current); |
| 0 | 130 | | } |
| 0 | 131 | | } |
| | 132 | | } |
| | 133 | | } |