简体   繁体   English

从C#服务器下载后,Zip文件已损坏

[英]Zip file is getting corrupted after downloading from server in C#

request = MakeConnection(uri, WebRequestMethods.Ftp.DownloadFile, username, password);
response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

//This part of the code is  used to write the read content from the server
using (StreamReader responseReader = new StreamReader(responseStream))
{
    using (var destinationStream = new FileStream(toFilenameToWrite, FileMode.Create))
    {
        byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd());
        destinationStream.Write(fileContents, 0, fileContents.Length);
    }
}
//This part of the code is  used to write the read content from the server
using (var destinationStream = new FileStream(toFilenameToWrite, FileMode.Create))
{
    long length = response.ContentLength;
    int bufferSize = 2048;
    int readCount;
    byte[] buffer = new byte[2048];
    readCount = responseStream.Read(buffer, 0, bufferSize);
    while (readCount > 0)
    {
        destinationStream.Write(buffer, 0, readCount);
        readCount = responseStream.Read(buffer, 0, bufferSize);
    }
}

The former ones writes the content to the file but when I try to open the file it says it is corrupted. 前者将内容写入文件,但是当我尝试打开文件时,它说文件已损坏。 But the later one does the job perfectly when downloading zip files. 但是后面的一个下载zip文件时做得很好。 Is there any specific reason why the former code doesn't work for zip files as it works perfectly for text files? 有什么特定的原因为什么以前的代码不能完美地适用于文本文件,而无法用于zip文件?

byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd());

You try to interpret a binary PDF file as an UTF-8 text. 您尝试将二进制PDF文件解释为UTF-8文本。 That just cannot work. 那根本行不通。

For a correct code, see Upload and download a binary file to/from FTP server in C#/.NET . 有关正确的代码,请参见在C#/。NET中将二进制文件上传到FTP服务器或从FTP服务器下载二进制文件

Use BinaryWriter and pass it FileStream. 使用BinaryWriter并将其传递给FileStream。

    //This part of the code is  used to write the read content from the server
    using (var destinationStream = new BinaryWriter(new FileStream(toFilenameToWrite, FileMode.Create)))
    {
        long length = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];
        readCount = responseStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            destinationStream.Write(buffer, 0, readCount);
            readCount = responseStream.Read(buffer, 0, bufferSize);
        }
    }

here is my solution that worked for me 这是对我有用的解决方案

C# C#

public IActionResult GetZip([FromBody] List<DocumentAndSourceDto> documents)
{
    List<Document> listOfDocuments = new List<Document>();

    foreach (DocumentAndSourceDto doc in documents)
        listOfDocuments.Add(_documentService.GetDocumentWithServerPath(doc.Id));

    using (var ms = new MemoryStream())
    {
        using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
        {
            foreach (var attachment in listOfDocuments)
            {
                var entry = zipArchive.CreateEntry(attachment.FileName);

                using (var fileStream = new FileStream(attachment.FilePath, FileMode.Open))
                using (var entryStream = entry.Open())
                {
                    fileStream.CopyTo(entryStream);
                }
            }

        }
        ms.Position = 0;
        return File(ms.ToArray(), "application/zip");
    }

    throw new ErrorException("Can't zip files");
}

don't miss the ms.Position = 0; 不要错过ms.Position = 0; here 这里

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

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