2
如何使用SharpZipLib從tar.gz(tar文件和文件夾,然後gzipped)提取特定文件(或有流式訪問)?或者也許任何人有一些類似的庫在.NET cf 3.5中做到這一點?如何使用SharpZipLib提取或訪問gzip中特定文件的流?
如何使用SharpZipLib從tar.gz(tar文件和文件夾,然後gzipped)提取特定文件(或有流式訪問)?或者也許任何人有一些類似的庫在.NET cf 3.5中做到這一點?如何使用SharpZipLib提取或訪問gzip中特定文件的流?
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();
}
}
}
}
這應該爲你做。
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();
}
你怎麼能識別 「theOneIWant」 項?我以爲你必須找到它的名字,有沒有另一種方式? :) – Faraday 2012-07-31 13:24:58
@Vijay通過使用TarEntry的任何屬性,這很可能是Name(或File)。現在不能讓它變得太簡單,我可以嗎? :D – Thomas 2012-07-31 13:49:16
哈哈,夠公平的。也許我給了他太多的幫助! :) – Faraday 2012-07-31 14:35:36