ARX
ARX

Reputation: 297

Zip a directory and upload to FTP server without saving the .zip file locally in C#

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?

Upvotes: 3

Views: 3613

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202594

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

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

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:

Upvotes: 3

Matthew Watson
Matthew Watson

Reputation: 109792

Something like this would work as far as getting the ZIP into memory goes:

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

Once you have the byte array, you should readily be able to send it to the server.

Upvotes: 1

Related Questions