paradise_human
paradise_human

Reputation: 695

Changing FileStream Write encoding type

This is my code :

public static string DownloadFile(string FtpUrl, string FileNameToDownload,
                   string userName, string password, string tempDirPath)
    {
        string ResponseDescription = "";
        string PureFileName = new FileInfo(FileNameToDownload).Name;
        string DownloadedFilePath = tempDirPath + "/" + PureFileName;
        string downloadUrl = String.Format("{0}/{1}", FtpUrl, FileNameToDownload);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
        req.Method = WebRequestMethods.Ftp.DownloadFile;
        req.Credentials = new NetworkCredential(userName, password);
        req.UseBinary = true;
        req.Proxy = null;
        try
        {
            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] buffer = new byte[2048];
            FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
            int ReadCount = stream.Read(buffer, 0, buffer.Length);
            while (ReadCount > 0)
            {

              fs.Write(buffer, 0, ReadCount);
              ReadCount = stream.Read(buffer, 0, buffer.Length);
            }
            ResponseDescription = response.StatusDescription;
            fs.Close();

            stream.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return ResponseDescription;
    }

}

This code Downloads a file from a ftp server and write it to a specific path in server. but the encoding of the saved file is not UTF-8. I want to change the encoding type of the file to UTF-8. Do I must use StreamReader ? How Can I modify that code?

Upvotes: 3

Views: 24718

Answers (2)

Me.Name
Me.Name

Reputation: 12544

In theory, the below should work, but it depends or whether the responsestream can work together with the streamreader. Writing with a different encoding is easy, you can simply use a streamwriter (based on textwriter) instead of a filestream. However, you can't write the bytes directly, since you have to write the properly formatted text. For that, the bytes have to be converted to text (Char buffer) with the proper original encoding.

    char[] buffer = new char[2048]; //or 1024 if you want to keep the same block size
    using (var reader = new StreamReader(stream, Encoding.Unicode)) // <= Or whatever encoding the orignal is
    {
        using (var tw = new StreamWriter(DownloadedFilePath, false, Encoding.UTF8)) //streamwriter instead of filestream
        {                
            while (true)
            {
                int ReadCount = reader.Read(buffer, 0, buffer.Length);
                if (ReadCount == 0) break;
                tw.Write(buffer, 0, ReadCount);                    
            }
            ResponseDescription = response.StatusDescription;
            stream.Close();
            tw.Close();
        }
    }

If the streamreader gives problems, you can also download the bytes first, and use a streamreader on the already downloaded bytes.

Upvotes: 3

user3383479
user3383479

Reputation:

You can wrap it in StreaWriter:

try
    {
        FtpWebResponse response = (FtpWebResponse)req.GetResponse();
        Stream stream = response.GetResponseStream();
        byte[] buffer = new byte[2048];
        var sw = new StreamWriter( new FileStream(DownloadedFilePath, FileMode.Create),
    Encoding.UTF8);
        int ReadCount = stream.Read(buffer, 0, buffer.Length);
        while (ReadCount > 0)
        {

           sw.Write(buffer, 0, ReadCount);
          ReadCount = stream.Read(buffer, 0, buffer.Length);
        }
        ResponseDescription = response.StatusDescription;
        sw.Close();

        stream.Close();
    }

I hope it will help

You have a look here :https://stackoverflow.com/ answer

Upvotes: 0

Related Questions