0
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. Is there any specific reason why the former code doesn't work for zip files as it works perfectly for text files?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Praburaj
  • 613
  • 8
  • 21

3 Answers3

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

You try to interpret a binary PDF file as an UTF-8 text. That just cannot work.

For a correct code, see Upload and download a binary file to/from FTP server in C#/.NET.

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

Use BinaryWriter and pass it 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);
        }
    }
Nino
  • 6,931
  • 2
  • 27
  • 42
  • zip is still corrupt? – Nino Dec 28 '16 at 07:32
  • Yeah the file is still corrupted – Praburaj Dec 28 '16 at 07:36
  • I tried your code, and sucessfully downloaded file (from web, not from ftp because right now i can not download from ftp; but it only differs in casting response and request. So, I've edited my answer. – Nino Dec 28 '16 at 08:52
  • 1
    The question was, why the first code does not work, while the latter works. Showing a third version of a code does not answer the question. – Martin Prikryl Dec 28 '16 at 08:56
0

here is my solution that worked for me

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; here

Fitch
  • 1,084
  • 1
  • 11
  • 17