简体   繁体   中英

How to read / write a null terminated string from stream while downloading from FTP

I'm working on a Updater that downloads a updatelist.xml file containing information (hash code, file name, and file directory) about files on a FTP server.

It then gets the information of those files on the local computer and if they are different (based on hash code) the updater proceeds to download the updated file from the ftp server.

That works great, but I'm having a little problem when it comes to txt files. Basically when I'm writing the file, it doesn't writes the null terminated string, for example:

This is the file on FTP server:

"Come Lads! How does things go?
I see my youth in you and it makes

This is the same file once it has been downloaded to the local computer:

"Come Lads! How does things go?I see my youth in you and it makes

I've been searching information on internet but I'm not understanding how to fix this.

This is a piece of the code, which I think is the important part.

FileStream file;
file = File.Open(downloadFilePath, FileMode.Create);

byte[] buffer = new byte[1024 * 30];

int read;
long downloaded = 0;
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    downloaded += read;

    string text = string.Format("Descargando archivo {0} de {1}: {2} - {3} / {4}", curFiles, totalFiles,
        xn["Name"].InnerText, BytesToString(downloaded), BytesToString(contentLength));

    label_progreso.Text = text;

    progressBar2.Value = (int)ProgressBarValue(downloaded, contentLength);

    file.Write(buffer, 0, read);
}

Clearly the problem is that when the program creates and writes the file it does not writes the null terminated string.

What I'm missing?

I've found info using webclient.download instead of FtpWebRequest, I've tried that but it doesn't work either.

You should always close FileStream objects. Try using statement

using(FileStream file = File.Open(downloadFilePath, FileMode.Create))
{
    //code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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