我需要这样做,因为我从azure Webjob运行。 这是我的代码:
public static void ExtractTGZ(Stream inStream)
{
using (Stream gzipStream = new GZipInputStream(inStream))
{
using (var tarIn = new TarInputStream(gzipStream))
{
TarEntry tarEntry;
tarEntry = tarIn.GetNextEntry();
while (tarEntry != null)
{
Console.WriteLine(tarEntry.Name);
tarEntry = tarIn.GetNextEntry();
}
}
}
}
调用ExtractTGZ时,我正在使用MemoryStream 当到达“GetNextEntry”时,“tarEntry”为空,但是当使用FileStream而不是MemoryStream时,我得到值
答案 0 :(得分:2)
您的MemoryStream
最有可能不在Position
的正确位置。例如,如果您的代码是这样的:
using (var ms = new MemoryStream())
{
otherStream.CopyTo(ms);
//ms.Position is at the end, so when you try to read, there's nothing to read
ExtractTGZ(ms);
}
您需要使用Seek
方法或Position
属性将其移至开头:
using (var ms = new MemoryStream())
{
otherStream.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin); // now it's ready to be read
ExtractTGZ(ms);
}
另外,你的循环会更简洁,如果这样写的话,我会更清楚地说:
TarEntry tarEntry;
while ((tarEntry = tarIn.GetNextEntry()) != null)
{
Console.WriteLine(tarEntry.Name);
}