Solved

Split File into Multiple Byte Arrays

Posted on 2013-01-27
2
1,767 Views
Last Modified: 2013-01-27
I was able to read a file into one byte array and write it back in another file without any issue.  However I'm trying to figure out how to read one file and split it into multiple arrays.  Then write back all the arrays into one file.  I'm not sure exactly what is going on but it seems I keep getting one extra byte in my arrays than I should have.

Working Simple Array
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim bteRead() As Byte

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim bteRead(myFileStream.Length)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then bteRead(lngLoop) = CByte(intByte)
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(bteRead, 0, bteRead.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window


Code for attempting to split the file into byte arrays.
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim BinBlock1 As Byte()
        Dim BinBlock2 As Byte()

        BinBlock1 = Nothing
        BinBlock2 = Nothing

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim BinBlock1(50000) 'Total Size = 73,478
            ReDim BinBlock2(23478)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then
                    If (lngLoop >= 0 And lngLoop <= 50000) Then
                        BinBlock1(lngLoop) = CByte(intByte)
                    End If

                    If (lngLoop >= 50001 And lngLoop < 99000) Then
                        If (lngLoop = 50001) Then lngLoop = 0
                        BinBlock2(lngLoop) = CByte(intByte)
                    End If
                End If
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(BinBlock1, 0, BinBlock1.Length)
            oFileStream.Write(BinBlock2, 0, BinBlock2.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window

0
Comment
Question by:thaburner
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38825441
Try something like this:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim chunkSize As Integer = 4096
        Dim chunks As New List(Of Byte())

        ' Read in the input file into byte arrays and store them in the "chunks" List above:
        Dim inputFilename As String = "C:\Users\Guest\Desktop\Adobe.pdf"
        Dim fsInput As New FileStream(inputFilename, IO.FileMode.Open)
        While fsInput.Position < fsInput.Length
            Dim bytes(chunkSize) As Byte
            ' the last "leftover" bit of the file may be smaller than our byte array...
            If fsInput.Length - fsInput.Position < bytes.Length Then
                ReDim bytes(fsInput.Length - fsInput.Position - 1)
            End If

            fsInput.Read(bytes, 0, bytes.Length)
            chunks.Add(bytes)
        End While
        fsInput.Close()

        ' Write the output file from the byte arrays in "chunks":
        Dim outputFilename As String = "C:\Users\Guest\Desktop\Adobe Test.pdf"
        Dim fsOutput As New FileStream(outputFilename, FileMode.Create)
        For Each chunk As Byte() In chunks
            fsOutput.Write(chunk, 0, chunk.Length)
        Next
        fsOutput.Close()

        MessageBox.Show("Done!")
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:thaburner
ID: 38825498
This will work perfectly!!! Thanks Idle
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now