如何使用c#提取存在于ftp服务器上的zip文件
本文关键字:服务器 zip 文件 ftp 何使用 提取 存在 | 更新日期: 2023-09-27 18:08:30
我正在使用ftplib在ftp服务器上上传文件,使用以下代码
FtpConnection ftp = new FtpConnection(serverip, ftpuser, ftppassword);
ftp.Open();
ftp.Login();
ftp.SetCurrentDirectory("domain/wwwroot");
void CreateDirOnFtp(string sDir, FtpConnection ftp)
{
try
{
foreach (string f in Directory.GetFiles(sDir))
{
Uri uri = new Uri(f);
ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
}
foreach (string d in Directory.GetDirectories(sDir))
{
string dirname = new DirectoryInfo(d).Name;
if (!ftp.DirectoryExists(dirname))
{
ftp.CreateDirectory(dirname);
}
ftp.SetCurrentDirectory(dirname);
CreateDirOnFtp(d, ftp);
}
}
catch (System.Exception e)
{
}
}
但是这段代码没有遍历所有目录,所以丢失了ftp服务器上的一些目录和文件。
所以我决定在FTP上上传zip文件并在FTP服务器上提取它,但是我找不到解压ftp服务器上存在的文件的方法。
我该怎么做?或者其他更好的方式来上传多个目录和文件到ftp服务器
您不能告诉服务器使用FTP协议提取文件。要做到这一点,您需要创建一些web服务来在上传后提取它,或者一些计划任务来提取它找到的所有zip。
如果你要创建一个web服务,那么FTP上传可以完全放弃。Service方法可以接受压缩后的内容,并在服务器端提取。
尝试递归地遍历目录并通过ftp上传。我认为这比在没有webservice的情况下在服务器上解压缩文件要简单得多。