2

如何使用 SharpZipLib 从 tar.gz(tar 文件和文件夹,然后压缩)中提取特定文件(或具有流式访问)?或者也许有人在 .NET cf 3.5 中有一些类似的库可以做到这一点?

4

2 回答 2

7
using ( FileStream inputStream = File.OpenRead ( aPackage ) )
{
    using ( GzipInputStream gzStream = new GzipInputStream ( inputStream ) )
    {
        using ( TarInputStream tarStream = new TarInputStream ( gzStream ) )
        {
            TarEntry entry = tarStream.GetNextEntry();
            while ( entry != null )
            {
                if ( entry == theOneIWant )
                {
                    tarStream.CopyEntryContents (outputStream );
                    break;
                }
                entry = tarStream.GetNextEntry();
            }
        }
    }
}
于 2012-07-31T13:04:42.203 回答
0

这应该为你做。

public static void Main(string[ args)
{
    TarInputStream tarIn = new TarInputStream(new FileStream(@args[0], FileMode.Open, FileAccess.Read));
    TarEntry curEntry = tarIn.GetNextEntry();
    while (curEntry != null)
    {
        if (curEntry.Name.EndsWith("foo.txt", StringComparison.CurrentCultureIgnoreCase))
        {
            byte[] outBuffer = new byte[curEntry.Size];
            FileStream fs = new FileStream(@"foo.txt", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            tarIn.Read(outBuffer, 0, (int)curEntry.Size);
            bw.Write(outBuffer,0,outBuffer.Length);
            bw.Close();
        }
        curEntry = tarIn.GetNextEntry();
    }
    tarIn.Close();
}
于 2012-07-31T13:01:38.007 回答