Reputation: 559
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.
Using Framework 4.5.2 and Visual studio 2017
Upvotes: 1
Views: 12336
Reputation: 8288
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;
Upvotes: 0
Reputation: 498
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.
Upvotes: 0