将字节数组写入文件的问题 [英] Issue with writing byte array to a file
问题描述
在Windows下,当安装 Evernote 时,还将安装一个api,可以通过以下方式访问该api: vba(例如).
Under windows, when Evernote is installed, an api is also installed, which can be accessed through vba (for example).
每个注释都可以显示其资源"(附加的文件和图像),而实际的资源可以作为字节数组进行检索.
Each notes can show its "Resources" (attached files and images), and the actual Resources can be retrieved as Byte Arrays.
我无法将字节数组写入实际文件.
变量声明:
Dim fileByte() As Byte
Dim nt As enapiLib.Note
获取数据:
fileByte = nt.Resources.Item(i).Data
将字节数组写入文件:
Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
Dim iFileNum As Integer, lWritePos As Long
Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
On Error GoTo ErrFailed
If bAppendToFile = False Then
If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
'Delete the existing file
VBA.Kill sFileName
End If
End If
iFileNum = FreeFile
Debug.Print "iFileNum = " & iFileNum
'Open sFileName For Binary Access Write As #iFileNum
Open sFileName For Binary Lock Read Write As #iFileNum
If bAppendToFile = False Then
'Write to first byte
lWritePos = 1
Else
'Write to last byte + 1
lWritePos = LOF(iFileNum) + 1
End If
Put #iFileNum, lWritePos, vData
Close #iFileNum
WriteByteArray = True
Exit Function
ErrFailed:
Debug.Print "################################"
Debug.Print "Error handling of WriteByteArray"
Debug.Print "################################"
FileWriteBinary = False
Close iFileNum
Debug.Print Err.Description & "(" & Err.Number & ")"
End Function
我尝试使用exe文件
通过debug.print每个字节值,我知道我的字节数组与其他所有exe文件一样都以4D 5A开头
By debug.printing each byte value, I know that my byte array starts with 4D 5A as every other exe file
Resource (1) :
ClickToSetup.0.9.8.1416.exe
application/x-msdownload
Le fichier C:\Dropbox\TestEvernote\ClickToSetup.0.9.8.1416.exe doit être créé.
Lbound(fileByte) = 0
Ubound(fileByte) = 5551919
i = 0
filebyte(i) = 4D
i = 1
filebyte(i) = 5A
通过将创建的exe文件读回字节数组,我知道新创建的数组按需要从字节4D 5A开始
By reading back the exe file created to a byte array, I know that the newly created array starts with byte 4D 5A as wished
但是硬盘驱动器上存在的exe文件已_corrupted_,并且_不能以正确的字节开始_:
But the exe file present on the hard drive is _corrupted_, and _does not start_ with the correct bytes_ :
这是硬盘驱动器上存储的文件的第一个二进制值:(来自VBinDiff工具)(我无法发布图像,我是这里的新手...):
Here are the first binary values of the stored file on the harddrive : (got from VBinDiff tool ) (I cannot post image, I am a newbie here... ) : VBinDiff output of exe
为什么在实际数据前面有这12个字节?
Why is there these 12 bytes in front of the actual data ??
推荐答案
我遇到了同样的问题-每个写入的文件的顶部都抛出了12个字节的标头.事实证明,PUT命令不太了解如何处理Variant类型的数据.我不确定确切的原因,但我的解决方法是简单地替换PUT行:
I had the same problem - some 12 byte header thrown at the top of every file written. It turns out that the PUT command doesn't quite know how to handle data of type Variant. I'm not sure of the exact cause, but my work around was to simply replace the PUT line:
Put #iFileNum, lWritePos, vData
与此:
Dim buffer() As Byte
buffer = vData
Put #iFileNum, lWritePos, buffer
问题解决了.
这篇关于将字节数组写入文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!