2012-07-31 1 views

Répondre

6
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(); 
      } 
     } 
    } 
} 
+0

Comment pourriez-vous identifier l'entrée "theOneIWant"? Je pensais que vous deviez le trouver par son nom, y a-t-il un autre moyen? :) – Faraday

+0

@Vijay en utilisant l'une des propriétés de TarEntry, qui serait probablement Nom (ou Fichier.) Impossible de le rendre trop facile maintenant, puis-je? : D – Thomas

+0

lol, assez juste. Peut-être que je lui ai donné trop d'aide! :) – Faraday

0

Cela devrait le faire pour vous.

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(); 
} 
Questions connexes