Frage

I am trying to upload a compressed GZipStream to sftp server using ssh.net library. The problem is that when I create the GZipStream, it can not be read any more. Below is my code:

using (SftpClient client = new SftpClient(connectionInfo))
{
     client.Connect();
     client.ChangeDirectory("/upload");
     var uploadFileDirectory = client.WorkingDirectory + "\testXml.xml.gz";


     using (GZipStream gzs = new GZipStream(stream, CompressionLevel.Fastest))
     {
         stream.CopyTo(gzs);
         client.UploadFile(gzs, "text.xml.gz");
     }
}

The SftpClient's UploadFile takes a stream and I need to upload the GzipStream that is being compressed (without storing to a local drive and then read it again). But the GZipStream doesn't allow read when it is compressed. I tried doign the upload outside the gzipstream using clause and it says that the stream can not be accessed.

How can I approach this? Is it even possible to do it directly this way or do I need to write it to local drive and then upload it...

War es hilfreich?

Lösung

For future reference, I manage to find how to do this. You can't read the gZipStream on Compression mode but you can get create another MemoryStream of previous stream's bytes like this:

using (SftpClient client = new SftpClient(connectionInfo))
{
    client.Connect();
    client.ChangeDirectory("/upload");

    using (MemoryStream outputStream = new MemoryStream())
    {
        using (var gzip = new GZipStream(outputStream, CompressionLevel.Fastest))
        {
            stream.CopyTo(gzip);                  
        }
        using (Stream stm = new MemoryStream(outputStream.ToArray()))
        {
            client.UploadFile(stm,"txt.gz");
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top