读取tgz归档文件而不保存/解压缩到磁盘

时间:2013-10-03 17:34:56

标签: c#

我一直在寻找使用C#从.tgz或.tar.gz存档中读取一个或多个文件的解决方案,而无需将文件解压缩到磁盘。

我已经确定了许多在GNU许可下发布的第三方库,允许有人提取.tgz存档但是没有任何运气找到解决方案来读取文件而不先提取它。

如果可能的话,我想坚持使用标准库 - 有没有人使用GZipStream或任何其他方法?谢谢!

编辑:

我想实现类似于以下内容的内容:

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的支持或如何在文件加载到内存后读取文件。

1 个答案:

答案 0 :(得分:4)

刚刚使用blog post下的tar-cs library实施了New BSD License)tar.gz-archive提取示例。该示例演示了如何将tar.gz-archive的内容提取到磁盘。

/// <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 Property,因此必须使用MemoryStream作为中间存储,或者使用GZipStream属性支持实现Position包装。