c#上传不了文件到ftp服务器,压缩目录并上传到FTP服务器而不在C#中本地保存.zip文件...

在MemoryStream中创建ZIP存档并上传。

using (Stream memoryStream = new MemoryStream())

{

using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))

{

foreach (string path in Directory.EnumerateFiles(@"C:\source\directory"))

{

ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path));

using (Stream entryStream = entry.Open())

using (Stream fileStream = File.OpenRead(path))

{

fileStream.CopyTo(entryStream);

}

}

}

memoryStream.Seek(0, SeekOrigin.Begin);

FtpWebRequest request =

(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/archive.zip");

request.Credentials = new NetworkCredential("username", "password");

request.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream ftpStream = request.GetRequestStream())

{

memoryStream.CopyTo(ftpStream);

}

}

不幸的是,ZipArchive需要可搜索的流。如果不是,您将能够直接写入FTP请求流,而不需要将整个ZIP文件保存在内存中。

基于:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值