【问题标题】:Zip file is getting corrupted after downloading from server in C#在 C# 中从服务器下载后 Zip 文件损坏
【发布时间】:2016-12-28 07:22:30
【问题描述】:
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);
    }
}

前者将内容写入文件,但当我尝试打开文件时,它说它已损坏。但是后者在下载 zip 文件时完美地完成了这项工作。之前的代码对 zip 文件不起作用,因为它对文本文件非常有效,是否有任何具体原因?

【问题讨论】:

    标签: c# .net ftp zip streamreader


    【解决方案1】:
    byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd());
    

    您尝试将二进制 PDF 文件解释为 UTF-8 文本。那是行不通的。

    如需正确代码,请参阅Upload and download a binary file to/from FTP server in C#/.NET

    【讨论】:

    • 我了解到您的第二个代码可以工作(“但下载 zip 文件时后一个代码可以完美地完成工作”)。不是吗?
    • 您可以一口气读取整个文件。但这不是一个通用的解决方案。您无法以这种方式读取几 GB 的大文件。
    • 如果您想要一个简单的解决方案,请使用WebClient.DownloadFile
    【解决方案2】:

    使用 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);
            }
        }
    

    【讨论】:

    • zip 仍然损坏?
    • 是的,文件仍然损坏
    • 我尝试了您的代码,并成功下载了文件(来自网络,而不是来自 ftp,因为现在我无法从 ftp 下载;但它仅在转换响应和请求方面有所不同。所以,我已经编辑我的回答。
    • 问题是,为什么第一个代码不起作用,而后者起作用。显示代码的第三个版本并不能回答问题。
    【解决方案3】:

    这是对我有用的解决方案

    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");
    }
    

    不要错过这里的ms.Position = 0;

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2018-03-20
      相关资源
      最近更新 更多