問題描述
我的問題是標題.我試過這個:
My question is the title. I have tried this:
public void UploadToFtp(List<strucProduktdaten> ProductData)
{
ProductData.ForEach(delegate( strucProduktdaten data )
{
ZipFile.CreateFromDirectory(data.Quellpfad, data.Zielpfad, CompressionLevel.Fastest, true);
});
}
static void Main(string[] args)
{
List<strucProduktdaten> ProductDataList = new List<strucProduktdaten>();
strucProduktdaten ProduktData = new strucProduktdaten();
ProduktData.Quellpfad = @"Path ozip";
ProduktData.Zielpfad = @"Link to the ftp"; // <- i know the link makes no sense without a connect to the ftp with uname and password
ProductDataList.Add(ProduktData);
ftpClient.UploadToFtp(ProductDataList);
}
錯誤:
System.NotSupportedException:不支持路徑格式."
System.NotSupportedException:"The Path format is not supported."
我不知道在這種情況下我應該如何連接到 FTP 服務器并將目錄壓縮到 ram 中并將其直接發送到服務器.
I have no idea how I should connect in this case to the FTP server and zipping the directory in ram and send it directly to the server.
...有人可以提供幫助或提供指向類似或相同問題的鏈接嗎?解決了什么?
... can someone help or have a link to a similar or equal problem what was solved?
推薦答案
在MemoryStream
中創建ZIP壓縮包并上傳.
Create the ZIP archive in MemoryStream
and upload it.
using (Stream memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (string path in Directory.EnumerateFiles(@"C:sourcedirectory"))
{
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);
var request =
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 文件保存在內存中.
Unfortunately the ZipArchive
requires a seekable stream. Were it not, you would be able to write directly to the FTP request stream and won't need to keep a whole ZIP file in a memory.
基于:
- 使用 System.IO.Compression 在內存中創建 ZIP 存檔
- 將文件從字符串或流上傳到 FTP 服務器
這篇關于壓縮目錄并上傳到 FTP 服務器,而無需在 C# 中本地保存 .zip 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!