2017-08-29 67 views

回答

18

這是不可能的。

FTP協議中沒有API來解壓縮服務器上的文件。


雖然,除FTP訪問之外,還有一種方法也具有SSH訪問權限並不罕見。如果是這種情況,可以使用SSH連接並在服務器上執行shell命令(或類似命令)以解壓縮文件。
請參閱C# send a simple SSH command

如果需要,您可以使用FTP協議下載解壓縮的文件(雖然如果您有SSH訪問權限,您也可以使用SFTP訪問,然後使用SFTP而不是FTP)。


一些(很少)FTP服務器提供到執行任意殼(或其他)使用SITE EXEC命令(或類似的)命令的API。但這真的非常罕見。你可以像上面的SSH一樣使用這個API。

2

通過FTP下載到MemoryStream,然後你可以解壓縮,示例顯示如何獲取流,只需更改爲MemoryStream並解壓。示例不使用MemoryStream,但如果您熟悉流,則修改這兩個示例以便爲您工作應該是微不足道的。

例如來自:https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-download-files-with-ftp

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace Examples.System.Net 
{ 
    public class WebRequestGetExample 
    { 
     public static void Main() 
     { 
      // Get the object used to communicate with the server. 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); 
      request.Method = WebRequestMethods.Ftp.DownloadFile; 

      // This example assumes the FTP site uses anonymous logon. 
      request.Credentials = new NetworkCredential ("anonymous","[email protected]"); 

      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 
      Console.WriteLine(reader.ReadToEnd()); 

      Console.WriteLine("Download Complete, status {0}", response.StatusDescription); 

      reader.Close(); 
      response.Close();  
     } 
    } 
} 

解壓縮流,例如來自:https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files

using System; 
using System.IO; 
using System.IO.Compression; 

namespace ConsoleApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)) 
      { 
       using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) 
       { 
        ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt"); 
        using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) 
        { 
          writer.WriteLine("Information about this package."); 
          writer.WriteLine("========================"); 
        } 
       } 
      } 
     } 
    } 
} 

這裏是從下載的FTP zip文件,解壓縮該ZIP文件,然後上傳壓縮的工作示例文件回到同一個ftp目錄

using System.IO; 
using System.IO.Compression; 
using System.Net; 
using System.Text; 

namespace ConsoleApp1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string location = @"ftp://localhost"; 
      byte[] buffer = null; 

      using (MemoryStream ms = new MemoryStream()) 
      { 
       FtpWebRequest fwrDownload = (FtpWebRequest)WebRequest.Create($"{location}/test.zip"); 
       fwrDownload.Method = WebRequestMethods.Ftp.DownloadFile; 
       fwrDownload.Credentials = new NetworkCredential("anonymous", "[email protected]"); 

       using (FtpWebResponse response = (FtpWebResponse)fwrDownload.GetResponse()) 
       using (Stream stream = response.GetResponseStream()) 
       { 
        //zipped data stream 
        //https://stackoverflow.com/a/4924357 
        byte[] buf = new byte[1024]; 
        int byteCount; 
        do 
        { 
         byteCount = stream.Read(buf, 0, buf.Length); 
         ms.Write(buf, 0, byteCount); 
        } while (byteCount > 0); 
        //ms.Seek(0, SeekOrigin.Begin); 
        buffer = ms.ToArray(); 
       } 
      } 

      //include System.IO.Compression AND System.IO.Compression.FileSystem assemblies 
      using (MemoryStream ms = new MemoryStream(buffer)) 
      using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Update)) 
      { 
       foreach (ZipArchiveEntry entry in archive.Entries) 
       { 
        FtpWebRequest fwrUpload = (FtpWebRequest)WebRequest.Create($"{location}/{entry.FullName}"); 
        fwrUpload.Method = WebRequestMethods.Ftp.UploadFile; 
        fwrUpload.Credentials = new NetworkCredential("anonymous", "[email protected]"); 

        byte[] fileContents = null; 
        using (StreamReader sr = new StreamReader(entry.Open())) 
        { 
         fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd()); 
        } 

        if (fileContents != null) 
        { 
         fwrUpload.ContentLength = fileContents.Length; 

         try 
         { 
          using (Stream requestStream = fwrUpload.GetRequestStream()) 
          { 
           requestStream.Write(fileContents, 0, fileContents.Length); 
          } 
         } 
         catch(WebException e) 
         { 
          string status = ((FtpWebResponse)e.Response).StatusDescription; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+1

調用執行,但是在解壓縮到內存流後,我必須將其上傳到ftp。其實我希望所有這一切都在一個單一的FTP請求.. – Nithin

+0

聽起來像你有三個步驟的過程? 1.通過ftp下載,2.解壓縮,3.通過ftp上傳?這是您要查找的訂單嗎?我給出了兩個關於如何通過ftp第一次下載以及如何解壓的例子。讓我用更具體的東西來定製答案,是的? –

+0

我知道這個例子不是'完美',但它的工作原理 –

相關問題