【问题标题】:Zip a directory and upload to FTP server without saving the .zip file locally in C#压缩目录并上传到 FTP 服务器,而无需在 C# 中本地保存 .zip 文件
【发布时间】:2018-03-22 19:40:21
【问题描述】:

我的问题是标题。我试过这个:

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);
}

错误:

System.NotSupportedException:"不支持路径格式。"

我不知道在这种情况下我应该如何连接到 FTP 服务器并将目录压缩到 ram 中并将其直接发送到服务器。

...有人可以提供帮助或提供指向类似或相同问题的链接吗?解决了什么问题?

【问题讨论】:

    标签: c# .net ftp upload zip


    【解决方案1】:

    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);
    
        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 文件保存在内存中。


    基于:

    【讨论】:

    • 谢谢,这正是我搜索到的并且运行良好!
    【解决方案2】:

    只要将 ZIP 放入内存,这样的方法就可以工作:

    public static byte[] ZipFolderToMemory(string folder)
    {
        using (var stream = new MemoryStream())
        {
            using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
            {
                foreach (var filePath in Directory.EnumerateFiles(folder))
                {
                    var entry = archive.CreateEntry(Path.GetFileName(filePath));
    
                    using (var zipEntry = entry.Open())
                    using (var file = new FileStream(filePath, FileMode.Open))
                    {
                        file.CopyTo(zipEntry);
                    }
                }
            }
    
            return stream.ToArray();
        }
    }
    

    一旦你有了字节数组,你应该可以很容易地将它发送到服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-10
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      相关资源
      最近更新 更多