2012-02-24 44 views
0

我想要一些帮助一些代码请。这里是我的代码:读取/写入二进制文件溢出

Public Function ReadBinaryFileLarge(strFilename As String) As Byte() 
    Dim position As Integer = 0 
    Dim bufferSize As Integer = 4096 
    Dim bytes() As Byte 

    Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open) 
     ReDim bytes((fsOpen.Length) - 1) 
     Do 
      If (position + bufferSize) > fsOpen.Length Then 
       fsOpen.Read(bytes, position, fsOpen.Length - position) 
       Exit Do 
      Else 
       fsOpen.Read(bytes, position, bufferSize) 
      End If 

      position += bufferSize 

      Application.DoEvents() 
     Loop 
    End Using 

    Return bytes 

End Function 

Public Sub SaveBinaryFileLarge(strFilename As String, bytesToWrite() As Byte) 
    Dim position As Integer = 0 

    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write) 
     Do 
      Dim intToCopy As Integer = Math.Min(4096, bytesToWrite.Length - position) 
      Dim buffer(intToCopy - 1) As Byte 
      Array.Copy(bytesToWrite, position, buffer, 0, intToCopy) 
      fsNew.Write(buffer, 0, buffer.Length) 

      position += intToCopy 

      Application.DoEvents() 

     Loop While position < bytesToWrite.Length 

    End Using 

End Sub 

我的问题是,与大文件,字节数组不能宣称大。我需要将它加载到一个字节数组中以对字节数组执行一些加密工作。我用这个代码玩弄:

Public Function ReadBinaryFileTest(strFilename As String) As Byte()() 
    Const SIZE As Integer = &H1000 '4096 <-experiment with this value 
    Dim bytes()() As Byte 

    Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read, SIZE) 
     Dim ubound = (fsOpen.Length/SIZE) - 1 
     bytes = new byte(ubound)() 
     Dim read As Integer = 0 

     For index As Integer = 0 To ubound 
      bytes(index) = New Byte(SIZE - 1) 
      read = fsOpen.Read(bytes(index), 0, SIZE) 
      If read <> SIZE Then 
       Array.Resize(bytes(index), read) 'this should only happen once if at all 
      End If 
     Next 
    End Using 

    Return bytes 

End Function 

但是我得到这些错误:

字节=新的字节(UBOUND)()

'{' 预计

bytes(index )=新字节(SIZE - 1)

类型'字节'没有构造函数。

这是正确的方式去解决这个问题吗?如果不是,我应该如何攻击它?

回答

0

你必须将ReDim,

ReDim bytes(ubound-1) 

或者

bytes = New Byte(ubound)() {} 
+0

如何:字节(指数)=新的字节(SIZE - 1) – Garry 2012-02-24 03:28:12

+0

认沽{} - 字节(指数)=新字节(SIZE - 1){} – adatapost 2012-02-24 03:28:57