1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
Option Compare Database
Option Explicit
' ---------------------------------------------------------
' ------------== Déclaration des API ==--------------------
' ---------------------------------------------------------
' -----------------------
' 1. Les Constantes
' -----------------------
Const MAX_PATH = 260
Const INVALID_HANDLE_VALUE = -1
' File Attributes
Const FILE_ATTRIBUTE_READONLY = &H1 ' 1
Const FILE_ATTRIBUTE_HIDDEN = &H2 ' 2
Const FILE_ATTRIBUTE_SYSTEM = &H4 ' 4
Const FILE_ATTRIBUTE_DIRECTORY = &H10 ' 16
Const FILE_ATTRIBUTE_ARCHIVE = &H20 ' 32
Const FILE_ATTRIBUTE_ENCRYPTED = &H40 ' 64
Const FILE_ATTRIBUTE_NORMAL = &H80 ' 128
Const FILE_ATTRIBUTE_TEMPORARY = &H100 ' 256
Const FILE_ATTRIBUTE_SPARSE_FILE = &H200 ' 512
Const FILE_ATTRIBUTE_REPARSE_POINT = &H400 ' 1024
Const FILE_ATTRIBUTE_COMPRESSED = &H800 ' 2048
Const FILE_ATTRIBUTE_OFFLINE = &H1000 ' 4096
' File Creation flags
Const CREATE_NEW = 1
Const CREATE_ALWAYS = 2
Const OPEN_EXISTING = 3
Const OPEN_ALWAYS = 4
Const TRUNCATE_EXISTING = 5
' ----------------------------------------------------------------------
' 2. Les Structures
' ----------------------------------------------------------------------
' ---------------------
' DATE/TIME Structures
' ---------------------
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
' ---------------------
' FINDFILE Structure
' ---------------------
Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
' cFileName As String * MAX_PATH
cFileName(MAX_PATH - 1) As Byte
cAlternateFileName(14 - 1) As Byte
End Type
' ----------------------------------------------------------------------
' 3.(a) Déclaration Functions de l'API Windows
' ----------------------------------------------------------------------
Private Declare Function GetLastError Lib "Kernel32.dll" () As Long
Private Declare Function OpenFile Lib "Kernel32.dll" Alias "OpenFileA" _
(ByVal lpFileName As String, ByVal lpReOpenBuff As Long, _
ByVal uStyle) As Long
Private Declare Function CreateFile Lib "Kernel32.dll" Alias "CreateFileA" ( _
ByVal lpFileName As String, ByVal dwAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttr As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagAndAttr As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hHandle As Long) As Long
Private Declare Function WriteFile Lib "Kernel32.dll" ( _
ByVal hFile As Long, ByVal Buffer As String, _
ByVal dwBytesToWrite As Long, ByRef lpdwBytesWritten As Long, _
ByVal lpOverlapped As Long) As Long
' ----------------------------------------------------------------------
' 3.(b) Déclaration Functions de l'API Windows Internet/ftp
' ----------------------------------------------------------------------
Const INTERNET_FLAG_TRANSFER_ASCII = &H1 ' 0x00000001
Const FTP_TRANSFER_TYPE_ASCII = &H1 ' 0x00000001
Const INTERNET_FLAG_TRANSFER_BINARY = &H2 ' 0x00000002
Const FTP_TRANSFER_TYPE_BINARY = &H2 ' 0x00000002
Const GENERIC_READ = &H80000000 ' (0x80000000L)
Const GENERIC_WRITE = &H40000000 ' (0x40000000L)
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUserName As String, _
ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, _
ByVal sProxyName As String, ByVal sProxyBypass As String, _
ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hConnect As Long, ByVal lpszLocalFile As String, _
ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
(ByVal hConnect As Long, ByVal lpszSearchFile As String, _
ByVal lpFindFileData As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Private Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" _
(ByVal hConnect As Long, ByVal lpszFileName As String, ByVal dwAccess As Long, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetReadFileVBA Lib "wininet.dll" Alias "InternetReadFile" _
(ByVal hFile As Long, ByVal lpBuffer As String, _
ByVal dwNbBytesToRead As Long, ByRef lpdwNbBytesRead As Long) As Long
Sub Lecture_fichier2()
' Internet Handles for Internet Session, Internet Connection, Internet File
Dim hInternetSess As Long, hIConnect As Long, hIFile As Long
' Internet handle for FtpFindFirstFile, and Find File Structure
Dim hIFF As Long, ffF As WIN32_FIND_DATA
' File Handle (Windows)
Dim hFile As Long
' Read/Writte buffer
Const BUFSIZE = 1024
Dim Buffer As String
'
Dim BytesToRead As Long, BytesRead As Long
Dim BytesToWrite As Long, BytesWritten As Long
Dim strSceFile As String, strScePath As String
Dim strDestFile As String, strDestTarget As String
Dim RemoteFileSize As Long, localFileSize As Long
'
Dim RetVal As Long
Dim pswd As String
'Ouvre internet
hInternetSess = InternetOpen("MonAppli", 0, vbNullString, vbNullString, 0)
'Connection au site ftp
pswd = vbNullString
hIConnect = InternetConnect(hInternetSess, "ftp.mozilla.org", 21, "anonymous", pswd, 1, 0, 0)
' Définit le répertoire Source
strScePath = "pub/mozilla.org/firefox/releases/1.5/"
RetVal = FtpSetCurrentDirectory(hIConnect, strScePath)
strSceFile = "KEY" ' Fichier Source
strDestFile = "Key.txt" ' Fichier destination
strDestTarget = "C:\" ' Répertoire destination (local)
Buffer = String(BUFSIZE + 1, vbNullChar)
' Obtenir infos dur fichier distant
hIFF = FtpFindFirstFile(hIConnect, strSceFile & vbNullChar, VarPtr(ffF), 0, 0)
InternetCloseHandle (hIFF)
RemoteFileSize = ffF.nFileSizeLow + ffF.nFileSizeHigh * &H10000
' Initialise barre de progression
SysCmd acSysCmdInitMeter, "Chrgt ftp:", RemoteFileSize
' Ouvre fichier distant
hIFile = FtpOpenFile(hIConnect, strSceFile, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0)
' Ouvre fichier local
hFile = CreateFile(strDestTarget & strDestFile & vbNullChar, GENERIC_WRITE, 0, 0, _
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
localFileSize = 0
BytesToRead = BUFSIZE
' Boucle sur fichier distant
Do
RetVal = InternetReadFileVBA(hIFile, Buffer, BytesToRead, BytesRead) 'VarPtr(BytesRead)
BytesToWrite = BytesRead
RetVal = WriteFile(hFile, Buffer, BytesToWrite, BytesWritten, 0)
localFileSize = localFileSize + BytesWritten
' Met à jour la barre de progression
SysCmd acSysCmdUpdateMeter, localFileSize
Loop While BytesRead = BytesToRead
' Restaure la barre de statut (efface la barre de progresion)
SysCmd acSysCmdClearStatus
CloseHandle (hFile) 'Ferme Fichier Local
InternetCloseHandle hIFile 'Ferme Fichier distant
InternetCloseHandle hIConnect 'Ferme la connection
InternetCloseHandle hInternetSess 'Ferme internet
End Sub |
Partager