CSV in stream to ZIP in Memory to SFTP in memory

0 votes
asked Dec 19, 2019 by Heavyg (180 points)

Is it possible to take a File (CSV) from Stream , ZIp it with a password, then send it to a SFTP site without a file being actually being created?

1 Answer

+1 vote
answered Dec 19, 2019 by Lukas Matyska (56,670 points)

Yes, it is very easy using Rebex components:

// prepare input data
var inputStream = new MemoryStream(Encoding.ASCII.GetBytes("This is test data."));

// prepare ZIP stream
var zipStream = new MemoryStream();
using (var zip = new ZipArchive(zipStream, ArchiveStreamCloseMode.LeaveOpen))
    // set password
    zip.Password = "top-secret";

    // add input data
    zip.AddFile(inputStream, "data.txt");

// connect to SFTP and log in 
var client = new Sftp();
client.Connect(sftpHost, sftpPort);
client.Login(sftpUser, sftpPassword);

// upload ZIP stream (from the beginning)
zipStream.Position = 0;
client.PutFile(zipStream, "/data/secret.zip");

All is done in memory, without need to create any temporary file on disk.

commented Dec 19, 2019 by Heavyg (180 points)
looks great, thanks for the fast response.