Concatenate BINARY files
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Concatenate BINARY files

  1. #1
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Concatenate BINARY files

    Does anybody have any code that can concatenate BINARY files?

    enigmaos@yahoo.com

  2. #2
    Join Date
    Mar 2002
    Posts
    92

    Re: Concatenate BINARY files

    Why would you want to concatenate binary files?


  3. #3
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Concatenate BINARY files

    to preserve PCL format.

    enigmaos@yahoo.com

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Concatenate BINARY files

    Here is a simple program I wrote not to long ago to Pack the C:\Windows directory files into one big file and then unpack them again.
    Have not checked out the output files very closely, but I did some cursory look at the output and did not find any gross errors.

    The "Pack" Command button willl pack the files into a file called App.Path & "\OutFile.bin"
    The Unpack Button will create a Temp folder and unpack the App.Path & "\OutFile.bin" to it.
    1. Start a new project
    2. Add two command buttons
    3. Paste this code into the general declarations section of the form
    4. Look at the code to make sure it does not wipe out your system
    5. Run in
    6. Click "Pack"
    7. Click "Unpack"
    8. Review files created in App.path \Temp

    option Explicit
    Dim inf as string
    Dim outf as string
    Dim pth as string
    Dim iData as string
    Dim strFiles(100, 2)
    Dim ctr as Long
    Dim str as string
    Dim ffileI as Long
    Dim ffileO as Long
    Dim X as Long
    private Sub Command1_Click()
    If Dir(outf) <> "" then Kill outf ' get rid of left over file
    ffileO = FreeFile() ' get a file number for the output file
    Open outf for binary as ffileO ' open the output file
    ffileI = FreeFile() ' get file number for the input files
    str = Dir(pth & "*.txt", vbNormal) ' get appropriate files
    Do
    ' If Right(str, 4) = ".txt" then
    Debug.print pth & str
    Open pth & str for binary as ffileI ' open the input file
    iData = string(LOF(ffileI), Chr(0)) ' set length of read
    get ffileI, 1, iData ' get the data
    Debug.print seek(ffileO) ' next available byte in output file
    strFiles(ctr, 0) = str
    strFiles(ctr, 1) = seek(ffileO)
    strFiles(ctr, 2) = LOF(ffileI)
    ctr = ctr + 1
    Put ffileO, , iData ' append to the output file
    ' End If
    Close ffileI ' finished with this file
    str = Dir ' get next file
    Loop Until str = "" ' loop until no more
    Close ffileO ' Close all files
    ' save control information for Command2
    Open App.Path & "\CTL.TXT" for Output as ffileO
    for X = 0 to ctr
    print #ffileO, Trim(strFiles(X, 0)); ","; Trim(strFiles(X, 1)); ","; Trim(strFiles(X, 2))
    next X
    Close ffileO
    End Sub

    private Sub Command2_Click()
    ' This sub unpacks the created file.
    ' Ctr is number of files + 1
    ' Note. Command1 must be run just prior to this command or it will not workl
    ' The control information is stored in an array. This sub does not
    ' use the file created by command1 for control purposes. I just got lazy
    ' and did not finish it up.
    Dim X as Long
    ffileI = FreeFile()
    Open outf for binary as ffileI ' compacted file
    ffileO = FreeFile() ' the output files
    for X = 0 to ctr - 1
    Debug.print strFiles(X, 0); " "; strFiles(X, 1); " "; strFiles(X, 2)
    Open App.Path & "\Temp\" & strFiles(X, 0) for binary as ffileO
    iData = string(strFiles(X, 2), Chr(0)) ' length to read
    get ffileI, strFiles(X, 1), iData ' read bytes
    Put ffileO, 1, iData
    Close ffileO
    next X
    End Sub

    private Sub Form_Load()
    ctr = 0
    pth = "C:\Windows\"
    outf = App.Path & "\OutFile.bin"
    Command1.Caption = "Pack"
    Command2.Caption = "UNpack"
    End Sub




    Please rate it if it answers the question
    or is useful.
    '
    John G

  5. #5
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Concatenate BINARY files

    Here is a revision to that program
    1. Original program was limited to 100 files
    1.5. Made control array ReDimensionable
    2. Command2 depended on command1 being run
    3.Added some debugging info

    option Explicit
    Dim inf as string
    Dim outf as string
    Dim pth as string
    Dim iData as string
    Dim strFiles()
    Dim ctr as Long
    Dim str as string
    Dim ffileI as Long
    Dim ffileO as Long
    Dim X as Long
    private Sub Command1_Click()
    ReDim strFiles(2, 100)
    If Dir(outf) <> "" then Kill outf ' get rid of left over file
    ffileO = FreeFile() ' get a file number for the output file
    Open outf for binary as ffileO ' open the output file
    ffileI = FreeFile() ' get file number for the input files
    str = Dir(pth & "*.txt", vbNormal) ' get appropriate files
    Do
    Debug.print pth & str
    Open pth & str for binary as ffileI ' open the input file
    iData = string(LOF(ffileI), Chr(0)) ' set length of read
    get ffileI, 1, iData ' get the data
    Debug.print seek(ffileO) ' next available byte in output file
    strFiles(0, ctr) = str
    strFiles(1, ctr) = seek(ffileO)
    strFiles(2, ctr) = LOF(ffileI)
    ctr = ctr + 1
    Put ffileO, , iData ' append to the output file
    Close ffileI ' finished with this file
    str = Dir ' get next file
    Loop Until str = "" ' loop until no more
    Close ffileO ' Close all files
    ' save control information for Command2
    Open App.Path & "\CTL.TXT" for Output as ffileO
    for X = 0 to ctr - 1
    Debug.print "Writing "; Trim(strFiles(0, X)); ","; Trim(strFiles(1, X)); ","; Trim(strFiles(2, X))
    print #ffileO, Trim(strFiles(0, X)); ","; Trim(strFiles(1, X)); ","; Trim(strFiles(2, X))
    next X
    Close ffileO
    End Sub

    private Sub Command2_Click()
    ' This sub unpacks the created file.
    ' Ctr is number of files + 1
    Dim X as Long, Temp as string
    Dim TempArr
    ReDim strFiles(2, 0)
    ffileI = FreeFile()
    Open App.Path & "\CTL.TXT" for input as ffileI
    ctr = 0
    Do Until EOF(ffileI)
    If ctr Mod 100 = 0 then ReDim Preserve strFiles(2, UBound(strFiles, 2) + 100)
    Line input #ffileI, Temp
    TempArr = Split(Temp, ",", , vbTextCompare)
    strFiles(0, ctr) = TempArr(0)
    strFiles(1, ctr) = TempArr(1)
    strFiles(2, ctr) = TempArr(2)
    ctr = ctr + 1
    Loop
    Close ffileI

    Open outf for binary as ffileI ' the compacted file
    ffileO = FreeFile() ' the output file(s)
    for X = 0 to ctr - 1
    Debug.print strFiles(0, X); " "; strFiles(1, X); " "; strFiles(2, X)
    Open App.Path & "\Temp\" & strFiles(0, X) for binary as ffileO
    iData = string(strFiles(2, X), Chr(0)) ' Create input buffer of binary zeroes
    get ffileI, strFiles(1, X), iData ' read bytes
    Put ffileO, 1, iData
    Close ffileO
    next X
    End Sub

    private Sub Form_Load()
    ctr = 0
    pth = "C:\Windows\"
    outf = App.Path & "\OutFile.bin"
    Command1.Caption = "Pack"
    Command2.Caption = "UNpack"
    End Sub




    Please rate it if it answers the question
    or is useful.
    '
    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center