SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

Renci.SshNet.Sftp : SftpClient AppendAllText does not work for complex file types such as .docx or .xlsx

Open mcaupybugs opened this issue 1 year ago • 4 comments

I have two scenarios to consider.

  1. I have been trying to use the appendAllText functionality of the SftpClient to append two .docx files. The steps I perform in sequence are -
  • I create a .docx file with some content "First docx".
  • I create another .docx file with the content "Second docx".
  • I read the second .docx file in bytes and then tried to append the content to the first file by providing the path of the first file. The issue is when I perform the append using the AppendAllText functionality, I end up corrupting the first docx file. It works well for file types such as .txt or .log but for complex file types such as .docx etc it fails.
  1. The second alternative I am trying is to open the file in a SftpFileStream using the sftpClient.Open method.
  • I open the file in the FileMode.Append mode and provide the file access as FileAccess.Write, which returns a SftpFileStream.
  • I open the second Docx file in a memoryStream using using (var memoryStream = new MemoryStream(byteArrContent)) and use await memoryStream.CopyToAsync(sftpFileStream) to copy the bytes of the second docx to the first one. It also ends up corrupting the docx file.

I wanted to know if append functionality is not supported for such file types, or If I am missing something.

mcaupybugs avatar Feb 10 '24 12:02 mcaupybugs

AppendAllText is not aware about file types. It is just appending the bytes of the input to the bytes of the file. I imagine you would get the same result with System.IO.File.AppendAllText.

If you need to manipulate .docx files, I recommend you play with Open XML SDK https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk

Rob-Hague avatar Feb 10 '24 13:02 Rob-Hague

@mcaupybugs can you prepare a failing integration test for the .docx file?

WojciechNagorski avatar Feb 22 '24 12:02 WojciechNagorski

Docx is a archived zip file with xml files inside. Would this not be expected behavior

darkoperator avatar Feb 22 '24 12:02 darkoperator

As @darkoperator said, both of those file formats are compressed files (binary). To append data to the end of either of those files, you need to decompress the file, read and parse the xml, add the new data to the appropriate xml data structures, write out the xml, recompress. That is outside the scope of adding text to the end of a text file. I'd mark this as "will not fix".

lymem avatar Jul 24 '24 16:07 lymem