Solved

Split File into Multiple Byte Arrays

Posted on 2013-01-27
2
1,891 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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