0

前提:使用 WinInet FtpGetFile 通过 FTP 将文件从 Linux 复制到 Windows。

目标:文件源自 ANSI,需要 Unicode。

进展:我遇到的唯一问题是我需要原始文件中的 LF 字符成为目标文件中的 CRLF 字符。
我努力了:

Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileW" (ByVal hFTP As Long, ByVal sRemoteFile As String, ByVal sNewFile As String, ByVal bFailIfExists As Boolean, ByVal lFlagsAndAttributes As Long, ByVal lFlags As Long, ByVal lContext As Long) As Boolean
Public Sub test(hConn as Long, strSrcPath as String, strDestPath as String)
  'All code works other than the file not converting to having CR chars
  ftpGetFile(hConn, StrConv(strSrcPath, vbUnicode), StrConv(strDestPath, vbUnicode), True, 0, 0, 0)
End Sub
  • (转换失败)使用FtpGetFile方法 ( Alias FtpGetFileW) 的 Unicode 版本,使用StrConv(<string>, vbUnicode). 这些文件在行尾仅显示 LF 字符。
  • (WORKS,手动)使用 WinSCP 手动复制文件。它会自动将输出文件设为 Unicode,但我找不到与此相关的方法/设置。我无法在工作中使用 WinSCP.dll,因为我无法注册它。
  • (工作,慢慢地)使用解决方法。使用任一版本的FtpGetFile. 打开文件,读取变量,关闭文件,然后打开文件进行写入,写入Replace(variable,Chr(10),Chr(13)&Chr(10))。此外,文件的大小似乎增加了一倍。

如何使用 WinAPI 函数获取文件并一次性转换(如果可能)?

相关文章:
FTP 传输后 Unicode 变为 ANSI 通过 FTP 将
ANSI 字符串写入 Unicode 文件

源信息:
如何
为 WinInet 创建 FTP 组件 CodeGuru MSDN

4

2 回答 2

1

以下似乎是即时工作。如果有人对如何自动执行此操作有任何更好的建议(最好没有此解决方法或使我的解决方法更好),请提供。否则,我可能会在几天后选择这个作为答案。ftpReadFile是一个自定义函数,它使用InternetReadFile并将整个文件作为字符串输出。

Public Function ftpGetFileToUnicode(hConn As Long, strFromPath As String, strDestPath As String) As Boolean
  Dim hFile As Long
  Dim objFS As New FileSystemObject, objFile As TextStream

  If Not objFS.FileExists(strDestPath) Then
      Set objFile = objFS.CreateTextFile(strDestPath, ForWriting)
      objFile.Write Replace(ftpReadFile(hConn, strFromPath), Chr(10), Chr(13) & Chr(10))
      objFile.Close

      If objFS.GetFile(strDestPath).Size > 0 Then
          ftpGetFileToUnicode = True
          Exit Function
      End If
  End If

  ftpGetFileToUnicode = False
End Function

注意:如果文件不存在,则创建一个 0 字节的文件。可以很容易地更改为不这样做。

于 2013-10-02T18:23:13.970 回答
0

免责声明:我对VB一无所知。但是FtpGetFile 说它支持 ASCII 模式传输,它具有隐式行结束转换:

ftpGetFile(hConn, StrConv(strSrcPath, vbUnicode), StrConv(strDestPath, vbUnicode),
           True, 0, FTP_TRANSFER_TYPE_ASCII, 0)
于 2013-10-02T21:20:31.897 回答