Reading archived tgz file without saving / extracting to disk

I was looking for a solution to read one or more files from a .tgz or .tar.gz archive using C # without having to extract the files to disk.

I have identified several third-party libraries published under the GNU licenses that allow someone to extract the .tgz archive, but did not manage to find a solution to read the file without first extracting it.

If possible, I would like to stick with the standard libraries - does anyone have a solution using GZipStream or any other / s method? Thanks!

EDIT:

I would like to implement something similar to the following:

public static void Decompress2(FileInfo fileToDecompress)
{
    using (FileStream fileStream = fileToDecompress.OpenRead())
    {
        using (var memStream = new MemoryStream())
        {
            string currentFileName = fileToDecompress.FullName;
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (GZipStream decompressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
                {
                    byte[] bytes = new byte[4096];
                    int n;
                    while ((n = decompressionStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        memStream.Write(bytes, 0, n);
                    }
                }
            }
        }
    }
}

.tgz .tar.gz . . .gz, , .tar .

+4
1

( ) tar.gz-, tar-cs library, BSD. , tar.gz- .

/// <summary>
/// Example of tar-cs library usage to extract tar.gz-archives.
/// Please use the latest version (from trunk) of the library.
/// </summary>
public static class TarGZip
{
    public static void Extract(string inputFile, string outputDirectory)
    {
        using (FileStream inputStream = File.OpenRead(inputFile))
        using (Stream tarStream = UnGZipSteam(inputStream))
        {
            var tarReader = new TarReader(tarStream);
            while (tarReader.MoveNext(false)) // Moves pointer to the next file in the tar archive.
            {
                ExtractTarEntry(tarReader, outputDirectory);
            }
        }
    }

    /// <summary>
    /// Since GZipStream.Position Property is not implemented,
    /// it is necessary to use MemoryStream as intermediate storage.
    /// </summary>
    /// <param name="inputStream">The input stream.</param>
    /// <returns>Un-gzipped stream.</returns>
    private static Stream UnGZipSteam(Stream inputStream)
    {
        using (GZipStream gZipStream = new GZipStream(inputStream, CompressionMode.Decompress))
        {
            MemoryStream memoryStream = new MemoryStream();
            gZipStream.CopyTo(memoryStream);
            memoryStream.Position = 0;
            return memoryStream;
        }
    }

    private static void ExtractTarEntry(TarReader tarReader, string outputDirectory)
    {
        string relativePath = tarReader.FileInfo.FileName;

        // Relative path can contain slash, not backslash.
        // Use Path.GetFullPath() method to convert path.
        string fullPath = Path.GetFullPath(Path.Combine(outputDirectory, relativePath));

        switch (tarReader.FileInfo.EntryType)
        {
            case EntryType.File:
            case EntryType.FileObsolete:
                using (FileStream outputStream = File.Create(fullPath))
                {
                    // Read data from a current file to a Stream.
                    tarReader.Read(outputStream);
                }
                break;
            case EntryType.Directory:
                Directory.CreateDirectory(fullPath);
                break;
            default:
                throw new NotSupportedException("Not supported entry type: " + tarReader.FileInfo.EntryType);
        }
    }
}

, GZipStream.Position , MemoryStream GZipStream Position .

+4

All Articles