2017-03-06 21 views
0

1つの.tar.gzアーカイブにいくつかのファイルがあります。これらのファイルはLinuxサーバー上にあります。名前がわかっていれば、このアーカイブ内の特定のファイルからどのように読み取ることができますか?上記のコードのようなAsp .NET tar.gzアーカイブからファイルを読む

Uri urlFile = new Uri("ftp://" + ServerName + "/%2f" + FilePath + "/" + fileName); 
WebClient req = new WebClient() { Credentials=new NetworkCredential("user","psw")}; 
string result = req.DownloadString(urlFile); 

それは、ローカルマシン上でアーカイブをコピーせずに、このファイルを読み取ることが可能ですが、何か: は、txtファイルから直接読み込むために、私は次のコードを使用しましたか?

+0

タールボールはzipファイルのように動作するとは思わないが、ファイルをダウンロードしてから、それを展開してtxtファイルを取得しなければならないと思うだろう。 – War

+0

私はこの解決策を試しました。次のコードを使用して、サーバーからローカルにアーカイブ(gz)をダウンロードし、tarファイルを抽出しますが、このアーカイブから特定のファイルを抽出する方法はわかりません。 –

+0

'WebClient wc = new WebClient(){Credentials = cred}; wc.DownloadFile(path、gzFile.FullName); (GZipStream decompStreem =新しいGZipStream(INFILE、CompressionMode.Decompress))を使用して(のFileStream tarFileStream = File.Create(tarFile.FullName))を使用して(FileStreamをINFILE = gzFile.OpenRead()){ {を用い { decompStreem.CopyTo(tarFileStream); } } } ' –

答えて

0

解決策が見つかりました。多分、これはあなた達を助けることができます。

// archivePath="ftp://myLinuxServer.com/%2f/move/files/archive/20170225.tar.gz"; 
    public static string ExtractFileFromArchive(string archivePath, string fileName) 
    { 
     string stringFromFile="File not found"; 
     WebClient wc = new WebClient() { Credentials = cred, Proxy= webProxy };  //Create webClient with all necessary settings 

     using (Stream source = new GZipInputStream(wc.OpenRead(archivePath))) //wc.OpenRead() create one stream with archive tar.gz from our server 
     { 
      using (TarInputStream tarStr =new TarInputStream(source)) //TarInputStream is a stream from ICSharpCode.SharpZipLib.Tar library(need install SharpZipLib in nutgets) 
      { 
       TarEntry te; 
       while ((te = tarStr.GetNextEntry())!=null) // Go through all files from archive 
       { 
        if (te.Name == fileName) 
        { 
         using (Stream fs = new MemoryStream()) //Create a empty stream that we will be fill with file contents. 
         { 
          tarStr.CopyEntryContents(fs); 
          fs.Position = 0;     //Move stream position to 0, in order to read from beginning 
          stringFromFile = new StreamReader(fs).ReadToEnd(); //Convert stream to string 
         } 
         break; 
        } 
       } 
      } 
     } 

     return stringFromFile; 
    } 
関連する問題