我可以在FTP中解压缩ZIP文件,并使用C#将这个提取的文件放在相同的位置?我们可以使用C#在FTP服务器上解压文件吗?
回答
这是不可能的。
FTP协议中没有API来解压缩服务器上的文件。
虽然,除FTP访问之外,还有一种方法也具有SSH访问权限并不罕见。如果是这种情况,可以使用SSH连接并在服务器上执行shell命令(或类似命令)以解压缩文件。
请参阅C# send a simple SSH command。
如果需要,您可以使用FTP协议下载解压缩的文件(虽然如果您有SSH访问权限,您也可以使用SFTP访问,然后使用SFTP而不是FTP)。
一些(很少)FTP服务器提供到执行任意壳(或其他)使用SITE EXEC
命令(或类似的)命令的API。但这真的非常罕见。你可以像上面的SSH一样使用这个API。
通过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;
}
}
}
}
}
}
}
调用执行,但是在解压缩到内存流后,我必须将其上传到ftp。其实我希望所有这一切都在一个单一的FTP请求.. – Nithin
听起来像你有三个步骤的过程? 1.通过ftp下载,2.解压缩,3.通过ftp上传?这是您要查找的订单吗?我给出了两个关于如何通过ftp第一次下载以及如何解压的例子。让我用更具体的东西来定制答案,是的? –
我知道这个例子不是'完美',但它的工作原理 –
- 1. 我们可以使用COPY命令从FTP服务器加载文件吗?
- 2. 我们可以使用c#编写NNTP服务器吗?
- 3. c#上传文件到FTP服务器
- 4. 是否可以使用C#在FTP中压缩文件夹?
- 5. 我可以使用JSP在我的服务器上移动文件吗?
- 6. 我可以在PHP文件上传中使用gzip压缩吗?
- 7. 我们可以使用WCF服务上传500MB的文件大小吗?
- 8. 我们可以使用Linq和View服务器吗?
- 9. 我可以在服务器上获取FTP上传的IP地址吗?
- 10. 无法上传文件到FTP服务器使用C++
- 11. 使用PHP从本地xampp服务器到在线免费服务器的FTP文件。可以做到吗?
- 12. 我可以在FTP上使用后台智能传输服务吗?
- 13. 我可以在Xcode的Linux服务器上使用SVN吗?
- 14. JavaScript可以获取文件上传到FTP服务器的日期吗?
- 15. 我可以上传文件到角度服务器吗?
- 16. 我可以使用我的网络服务器作为邮件服务器吗?
- 17. iPhone - 我们可以使用NSXMLParser解析.plist(xml)文件吗?
- 18. 使用iPhone上传FTP服务器上的文本文件
- 19. 我可以下载Laravel源代码并上传到我的ftp服务器吗?
- 20. 如何在lua上的nginx服务器上解压文件?
- 21. Mule Zip文件并向FTP服务器发送压缩文件
- 22. 我们可以使用Talend DI open studio将文件从FTP服务器上的文件夹移动到另一个文件夹吗?
- 23. 我可以使文件始终从服务器加载吗?
- 24. 我们可以在专用网络或服务器上部署wit.ai吗?
- 25. 我们可以使用C#比较两个JavaScript文件吗?
- 26. 我们可以在IIS上运行Metro Web服务吗?
- 27. 在apache服务器中使用php解压zip文件
- 28. 空文件上传到FTP服务器
- 29. 上传文件到FTP服务器
- 30. ASP.Net文件上传到FTP服务器
你的意思是通过FTP远程访问吗? – Ben
@BensaysNotoPoliticsonSO yup in ftp – Nithin
@DanielShillcock怎么样? –