Last active
June 25, 2023 08:49
-
-
Save Ricky-G/5562922ca29ab8f8a349dc07917d65af to your computer and use it in GitHub Desktop.
Extracting GZip Tar Files Natively in .NET without external libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Formats.Tar; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string sourceTarGzFilePath = @"C:\_Temp\test.tar.gz"; | |
string targetDirectory = @"C:\_Temp\ExtractedFiles\"; | |
string tarFilePath = Path.ChangeExtension(sourceTarGzFilePath, ".tar"); | |
// Create a directory to store the extracted files if it doesn't exist | |
Directory.CreateDirectory(targetDirectory); | |
// Step 1: Decompress the .gz file | |
using (FileStream originalFileStream = File.OpenRead(sourceTarGzFilePath)) | |
{ | |
using (FileStream decompressedFileStream = File.Create(tarFilePath)) | |
{ | |
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress)) | |
{ | |
decompressionStream.CopyTo(decompressedFileStream); | |
} | |
} | |
} | |
// Step 2: Extract the .tar file | |
using (FileStream tarStream = File.OpenRead(tarFilePath)) | |
{ | |
using (TarReader tarReader = new TarReader(tarStream)) | |
{ | |
TarEntry entry; | |
while ((entry = tarReader.GetNextEntryAsync().Result) != null) | |
{ | |
/* | |
Skp symbolic and hard links , and global extended attributes (which is metat data) | |
Symbolic Links: A symbolic link is a file that points to another file or directory by its path. When you access a symbolic link, you are automatically directed to the file or directory that the symbolic link points to. | |
Hard Links: A hard link is a directory entry or file that shares the same inode as another directory entry or file. This means that they effectively are the same file, including having the same permissions and ownership. | |
*/ | |
if (entry.EntryType is TarEntryType.SymbolicLink or TarEntryType.HardLink or TarEntryType.GlobalExtendedAttributes) | |
{ | |
continue; | |
} | |
Console.WriteLine($"Extracting {entry.Name}"); | |
entry.ExtractToFileAsync(Path.Combine(targetDirectory, entry.Name), true).Wait(); | |
} | |
} | |
} | |
// Delete the temporary .tar file | |
File.Delete(tarFilePath); | |
Console.WriteLine("Extraction Completed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In .NET 7 Tar file support has been added natively and GZip is supported via System.IO.Compression. So we should be able to decompress a .tar.gz file natively in .NET 7 without external libraries
System.Formats.Tar.TarFile to pack a directory into TAR file or extract a TAR file to a directory;
System.Formats.Tar.TarReader to read a TAR file; and
System.Formats.Tar.TarWriter to write a TAR file.