0

I'm using the below code(WinScp nuget) to connect to Sftp server, and it works well. I was able to list the files in present in Sftp server.

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "tst.tst.tt",
    UserName = "test",
    SshHostKeyFingerprint = "ssh-ed25519 255 xxxxxxxxxxxxxxxxxxxxx=",
    SshPrivateKeyPath = @"D:\tst\key.ppk",
    PrivateKeyPassphrase = "asdfghwetrtert",
};

sessionOptions.AddRawSettings("FSProtocol", "2");

using (Session session = new Session())
{
    session.Open(sessionOptions);

    RemoteDirectoryInfo directory = session.ListDirectory("/home/prod/Input");
    foreach (RemoteFileInfo fileInfo in directory.Files)
    {
        Console.WriteLine(
            "{0} with size {1}, permissions {2} and last modification at {3}",
            fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
            fileInfo.LastWriteTime);
    }
}

I have a zip file present in Sftp, is it possible to download only a particular file from zip instead of downloading the entire zip file ?

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172

1 Answers1

1

In general, it's technically possible to download only specific file from a ZIP archive using the SFTP protocol. But not with WinSCP, as it does not allow partial downloads.

If you use another SFTP library, that does allow partial downloads (like SSH.NET), you can use it to first download ZIP central directory, use it to locate specific file in the archive, then download that part of the archive (in a compressed form) and uncompress locally. But it's not easy, as you will have to dig low level into ZIP archive format.

As a proof of the concept, here a similar question for Python and FTP:
Get files names inside a zip file on FTP server without downloading whole archive
With SFTP it would be bit easier, as SFTP allows a random access to the remote file, what FTP does not.


A way easier, if you have a shell access to the server, is to unzip the file on the server using a shell command and then download the extracted file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992