I am attempting to read a binary file and then write the contents to a new file. However for some reason some extra characters are placed at the top of the destination file apart from that the files are identical.
Here is the code:
Code:
Option Explicit
Private Sub Form_Load()
Call writeBinFile(App.Path & "\copy.zip", readBinFile(App.Path & "\original.zip"))
End Sub
Function writeBinFile(ByVal bfilename As String, data As Variant)
Dim FileNum As Long
FileNum = FreeFile
Open bfilename For Binary Access Write As #FileNum
Put #FileNum, , data
Close #FileNum
End Function
Function readBinFile(ByVal bfilename As String) As Variant
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
Dim binfilestr As String
On Error GoTo errHandler
FileNum = FreeFile
Open bfilename For Binary Access Read As #FileNum
fl = FileLen(bfilename)
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
readBinFile = binbyte
Exit Function
errHandler:
Exit Function
End Function
Sub writeBinFile(ByVal bfilename As String, Data() As Byte)
Dim FileNum As Long
FileNum = FreeFile
Open bfilename For Binary Access Write As #FileNum
Put #FileNum, , Data
Close #FileNum
End Sub
You may also want to add code to delete the destination file if it already exists.