2

I am trying to download a zip file from SFTP and unzip in the memory to process the file

I am using SSH.Net to download the file.

private static void processfilesfromftp(List<TSOracleMicrosDownLoadSetUp> list)
    {
        SftpClient sftp = HelperFunctions.GetClientConnection();
        if(sftp.IsConnected)
        {
            var files = sftp.ListDirectory("/");
            ZipFile zips = new ZipFile();
            string path = string.Empty;
            foreach(var file in files)
            {
                Stream unzippedEntryStream = new MemoryStream();
                path = string.Format("/{0}", file.Name);
                //byte[] arr = sftp.ReadAllBytes(file.FullName);
                var stream = new BufferedStream(sftp.OpenRead(file.FullName));
                //System.IO.TextReader textReader = new System.IO.StreamReader(stream);
                //System.IO.MemoryStream mStream = new MemoryStream();

                using (ZipFile zip = ZipFile.Read(stream))
                {
                    ZipEntry e = zip[0];
                    e.Extract(unzippedEntryStream);
                    System.IO.TextReader textReader = new System.IO.StreamReader(unzippedEntryStream);
                    string data = textReader.ReadToEnd();
                }
            }
        }
    }

memorystream throw error System.InvalidOperationException exception at

var stream = new BufferedStream(sftp.OpenRead(file.FullName));

Update

It is not throwing any error, but the final output of the unzip file is empty.

enter image description here

enter image description here

Using Framework 4.5.2 and Visual studio 2017

  • 1
    Please provide more details on that exception: stacktrace, ... – ADreNaLiNe-DJ Jun 25 '18 at 06:49
  • 1
    Exceptions in debug watches don't mean anything. The debugger is not nearly smart enough to know that it shouldn't try to display Read/WriteTimeout when a stream's CanTimeout property is false. You know that. The "timed out" message are a similar debugger problem, it has to give up when the watch expression takes too much time to produce a result. Not uncommon when the data needs to come from another machine or needs to be produced by another thread that is in a debug break-state. So this doesn't tell you anything interesting. – Hans Passant Jun 25 '18 at 09:39
  • How to increase the timeout period. I have gone through different thread and did not get any solution for it? – Muralidharan Ramakrishnan Jun 25 '18 at 09:52
  • It would be helpful if you shared the stack trace. – markoan Mar 16 '20 at 21:19
  • You can also look into 'keep alive' properties. – Hugues Beauséjour Oct 03 '22 at 15:47

2 Answers2

0

This is more a SSH.Net question and not specific Acumatica. It seems the problem is related to the SSH connection.

To change the timeout you can use SshClient.ConnectionInfo.Timeout. But you need to catch the exception and handle it gracefully. Here is a post with a similar issue.

BTW, you could use the included Acumatica library to read the zip file.

markoan
  • 498
  • 5
  • 14
0

I think you are not writing the file from FTP to the memory stream so it's empty.

Try using the DownloadFile method from SSH.Net to write file content in the stream.

Reference: https://stackoverflow.com/a/46907346/7376238

SftpClient _sftpClient;
_sftpClient = new SftpClient("sftp.server.domain", "MyLoginHere", "MyPasswordHere");
Stream fileBody = new MemoryStream();
_sftpClient.DownloadFile(ftpFile.FullName, fileBody);
fileBody.Position = 0; 
Hugues Beauséjour
  • 8,067
  • 1
  • 9
  • 22