2017-08-29 84 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

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

相关问题