简体   繁体   English

C# SFTP - 下载文件损坏并在比较文件 SFTP 服务器时显示不同的大小

[英]C# SFTP - download file corrupted and showing different size when compare the file SFTP server

I am trying to download.zip and.xlsx files from SFTP server,after downloading when I tried to open the zip file, it saying the compressed zip files is invalid and also the files size is high compared with the SFTP file size(remote file size).我正在尝试从 SFTP 服务器下载.zip 和 .xlsx 文件,下载后当我尝试打开 zip 文件时,它说压缩的 zip 文件与 FTP 文件相比文件大小无效并且文件大小也比较高尺寸)。 I am using the below code:我正在使用以下代码:

    string sFTPHost = "sftphost";
    string sFTPDirectory = "file.zip";
    string sFTPUser = "username";
    string sFTPPassword = "pwd";
    string sFTPPort = "22";

    ConnectionInfo ConnNfo = new ConnectionInfo(@sFTPHost, Convert.ToInt32(sFTPPort), @sFTPUser,
        new AuthenticationMethod[]{
            new PasswordAuthenticationMethod(@sFTPUser,@sFTPPassword),
        }
    );

    using (var sftp = new SftpClient(ConnNfo))
    {
        sftp.Connect();

        MemoryStream ms = new MemoryStream();
        sftp.DownloadFile(@sFTPDirectory, ms);
        byte[] feedData = ms.GetBuffer();

        var response = HttpContext.Current.Response;
        response.AddHeader("Content-Disposition", "attachment; filename="filename.zip");
        response.AddHeader("Content-Length", feedData.Length.ToString());
        response.ContentType = "application/octet-stream";
        response.BinaryWrite(feedData);
        sftp.Disconnect();
    }
}

What could be the issue?可能是什么问题?

MemoryStream.GetBuffer returns the underlying array of the stream which can/will contain allocated, unused bytes . MemoryStream.GetBuffer返回 stream 的底层数组,该数组可以/将包含已分配的未使用字节 For example the length of the returned buffer will match the stream's current Capacity , but will most likely be larger than the stream's current Length .例如,返回缓冲区的长度将匹配流的当前Capacity ,但很可能大于流的当前Length

From the documentation :文档中:

Note that the buffer contains allocated bytes which might be unused.请注意,缓冲区包含可能未使用的已分配字节。 For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused.例如,如果将字符串“test”写入 MemoryStream 对象,则从 GetBuffer 返回的缓冲区长度为 256,而不是 4,其中 252 个字节未使用。

You will need to use ToArray instead.您将需要改用ToArray Note however that this creates a new array and copies the data into it.但是请注意,这会创建一个新数组并将数据复制到其中。

byte[] feedData = ms.ToArray();
var response = HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment; filename=filename.zip");
response.AddHeader("Content-Length", feedData.Length.ToString());
response.ContentType = "application/octet-stream";
response.BinaryWrite(feedData);

Alternatively you should be able to copy from one stream to the other:或者,您应该能够从一个 stream 复制到另一个:

var response = HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment; filename=filename.zip");
response.AddHeader("Content-Length", ms.Length.ToString());
response.ContentType = "application/octet-stream";

// rewind stream and copy to response
ms.Position = 0;
ms.CopyTo(response.OutputStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM