2012-12-14 17 views
5

Sto utilizzando l'ultima versione di DotNetZip e ho un file zip con 5 XML.
Voglio aprire lo zip, leggere i file XML e impostare una stringa con il valore dell'XML.
Come posso fare questo?Come utilizzare DotNetZip per estrarre il file XML da zip

Codice:

//thats my old way of doing it.But I needed the path, now I want to read from the memory 
string xfile = System.IO.File.ReadAllText(strNewFilePath, System.Text.Encoding.Default); 

using (ZipFile zip = ZipFile.Read(this.uplZip.PostedFile.InputStream)) 
{ 
    foreach (ZipEntry theEntry in zip) 
    { 
     //What should I use here, Extract ? 
    } 
} 

Grazie

risposta

14

ZipEntry ha un Extract() sovraccarico che estrae ad un ruscello. (1)

miscelazione in questa risposta a How do you get a string from a MemoryStream?, si otterrebbe qualcosa di simile (completamente non testato):

string xfile = System.IO.File.ReadAllText(strNewFilePath, System.Text.Encoding.Default); 
List<string> xmlContents; 

using (ZipFile zip = ZipFile.Read(this.uplZip.PostedFile.InputStream)) 
{ 
    foreach (ZipEntry theEntry in zip) 
    { 
     using (var ms = new MemoryStream()) 
     { 
      theEntry.Extract(ms); 

      // The StreamReader will read from the current 
      // position of the MemoryStream which is currently 
      // set at the end of the string we just wrote to it. 
      // We need to set the position to 0 in order to read 
      // from the beginning. 
      ms.Position = 0; 
      var sr = new StreamReader(ms); 
      var myStr = sr.ReadToEnd(); 
      xmlContents.Add(myStr); 
     } 
    } 
} 
+0

Grazie Carson, che funziona per me. Stavo usando l'Estratto ma non con il flusso, ancora. – Bruno

+1

Ho saltato ms.Position = 0; Funziona benissimo grazie :) –

Problemi correlati