Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters in FTP files

I am facing problems to manage files in a FTP when they have special characters. For example filenames with ó or similars.

I give you an example. First I want to list and process each file of a folder in the FTP:

ftp = CType(FtpWebRequest.Create(sFtpPath), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.ListDirectory
reader = New StreamReader(ftp.GetResponse().GetResponseStream())

files = reader.ReadToEnd.Split(New String() {NewLine}, StringSplitOptions.RemoveEmptyEntries)
reader.Close()

But this is giving me problems when the file contains special characters because the string I have for the file doesn't "exists" on the FTP, for example:

For Each sFich As String In files
    ftp = CType(FtpWebRequest.Create(sFtpPath & "/" & sFich), FtpWebRequest)
    ftp.Method = WebRequestMethods.Ftp.DownloadFile
    reader = New StreamReader(ftp.GetResponse().GetResponseStream())

    '...
Next

For example, the file EXAMPLE_aró on the FTP, here is retrieved as EXAMPLE_ar□, so when I try to download the file it says that doesnt exist.

How can I deal with that?

like image 288
SysDragon Avatar asked Nov 03 '22 02:11

SysDragon


1 Answers

Judging from the answers to this question, you may need to specify the encoding of the StreamReader you're using to get the file listing. The default is UTF8, you can use other encodings like so:

reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.Unicode)

When I run into encoding issues, I usually copy and paste the garbled string into an editor that can show the hex values for the characters, and then figure out the encoding from there.

The string you have above contains the character 00F3. Your profile says you are in Spain. Putting those two together, with the help of Google I am guessing the FTP server is using the Spanish flavor of EBCDIC, which is code page 1145. So try this:

reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding(1145))

Edit: Turns out F3 is the acute o in most encodings, including extended ASCII, the EBCDIC one just came up in my search.

like image 149
Matt Avatar answered Nov 15 '22 09:11

Matt