压缩目录并上传到FTP服务器,而不用在C#中本地保存.zip文件 [英] Zip a directory and upload to FTP server without saving the .zip file locally in C#
问题描述
pre $
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\to\zip;
ProduktData.Zielpfad = @链接到ftp; //< - 我知道如果没有用uname和密码连接到ftp,链接就没有意义了
ProductDataList.Add(ProduktData);
ftpClient.UploadToFtp(ProductDataList);
错误:
System.NotSupportedException:路径格式不受支持。
我不知道我如何应该在这种情况下连接到FTP服务器,并在ram中压缩目录,并直接发送到服务器。
...有人可以帮忙或有链接到一个相似或相同的问题解决了什么?
在 MemoryStream中创建ZIP压缩文件$ c $使用(流memoryStream =新的MemoryStream())
{
使用(流memoryStream =新的MemoryStream())
(var archive = new ZipArchive(memoryStream,ZipArchiveMode.Create,true))
{
foreach(在Directory.EnumerateFiles(@C:\source\directory)中的字符串路径)
{
ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path));使用(Stream entryStream = entry.Open())
using(Stream fileStream = File.OpenRead(path))
fileStream.CopyTo(entryStream) ;
}
}
}
memoryStream.Seek(0,SeekOrigin.Begin);
FtpWebRequest请求=
(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 code>需要可搜索的流。如果没有,您可以直接写入FTP请求流,而不需要将整个ZIP文件保存在内存中。
基于:
Hey guys 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\to\zip";
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);
}
Error:
System.NotSupportedException:"The Path format is not supported."
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?
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:\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);
}
}
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.
Based on:
- Creating a ZIP Archive in Memory Using System.IO.Compression
- Upload a file to an FTP server from a string or stream
这篇关于压缩目录并上传到FTP服务器,而不用在C#中本地保存.zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!