更改FileStream编码类型 [英] Changing FileStream Write encoding type

查看:193
本文介绍了更改FileStream编码类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

}

此代码从ftp服务器并将其写入服务器中的特定路径。
,但保存文件的编码不是UTF-8。
我想将文件的编码类型更改为UTF-8。
我必须使用StreamReader吗?
如何修改该代码?

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?

推荐答案

理论上来说,下面的内容应该是有效的, Responsestream可以与Streamreader一起工作。
使用不同的编码进行编写很简单,您可以简单地使用流程(基于文本编写器)而不是文件流。但是,您不能直接写入字节,因为您必须编写正确格式的文本。为此,必须将字节转换为具有正确原始编码的文本(Char缓冲区)。

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

如果播放器出现问题,您还可以下载首先使用字节,并在已经下载的字节上使用streamreader。

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

这篇关于更改FileStream编码类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆