CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Posts
    1

    Splitting a file

    I need to split a file into multiple parts. Let's say you have a 3.8 MB file and need to split it into 500 KB segments; how would I do this?

    Thanks.


  2. #2

  3. #3
    Join Date
    Jan 2000
    Posts
    20

    Re: Splitting a file

    The following code should do what you're asking. I wrote it on the fly so I don't know how efficient it really is. Regarding the code, though, maybe someone can answer this for me. When you split up the files, the sum total size of all the files is larger than the original file...and when you reconstruct the original from the smaller chunks, the new file is actually larger in size than the original, but is virtually identical in every other respect. I have a version of Jasc Paint Shop Pro I tested the code with. After reconstruction, the new executable was exactly 32767 bytes larger than the original, but it ran just the same as before.

    It wouldn't really matter except that 32767 bytes is too much of a coincidence in this case. During splitting up the file, I transferred data in exactly 32K chunks...and now my new file is 32K larger than the original. I gotta go skiing in a bit here so I don't have time to sit and pick apart the code looking for the mysterious 32K, but if someone wants a neat little puzzle to figure out then they can find out where this is coming from and why it doesn't affect the end result file and email me at [email protected] with your answer.



    public Sub Split_Up_File()
    Dim smallChunkSize as Long
    Dim position1 as Long
    Dim position2 as Long
    Dim filesize as Long
    Dim numTmpFiles as Integer
    Dim theData as string
    Dim sizeCopied as Long

    ' initialize state variables
    smallChunkSize = 32767 ' break off 32K chunks at a time to write to a temp file
    sizeCopied = 0 ' keep track of how much data has been copied from the source file
    numTmpFiles = 1 ' helps determine the name of our temp files
    position1 = 1 ' keeps track of the byte position to read from in the source file
    position2 = 1 ' keeps track of the byte position to write to in the temp file
    Open "YOUR_FILE" for binary as #1
    filesize = LOF(1)
    Open "SOME_DIRECTORY\tempfile1.tmp" for binary as #2
    Do While filesize - sizeCopied >= smallChunkSize
    If position2 >= 512000 then ' position2 starts out at 1, so we ignore this
    Close #2 ' the first run through. Later when the temp file
    position2 = 1 ' size exceeds 512000 (500K), we close it and
    numTmpFiles = numTmpFiles + 1 ' open another file to write to
    Open "SOME_DIRECTORY\tempfile" & numTmpFiles & ".tmp" for binary as #2
    End If
    theData = string(smallChunkSize, " ")
    get #1, position1, theData ' read the 32K chunk of data from the source file
    Put #2, position2, theData ' and write it to the temp file
    sizeCopied = position1 - 1
    position1 = position1 + smallChunkSize ' increment the byte position of
    position2 = position2 + smallChunkSize ' read/write in each file
    Loop
    If filesize - sizeCopied > 0 then ' if there was data left over
    theData = string(filesize - sizeCopied, " ") ' that didn't fit neatly into
    get #1, position1, theData ' our 32K chunk, read it out of the source file and
    Put #2, position2, theData ' write it to the end of our temp file
    End If
    Close #1
    Close #2
    End Sub

    public Sub Reconstruct_File()
    Dim smallChunkSize as Long
    Dim position1 as Long
    Dim position2 as Long
    Dim howManyFiles as Integer
    Dim s as string
    Dim i as Integer
    Dim filesize as Long
    Dim sizeCopied as Long
    Dim theData as string

    howManyFiles = 0
    s = Dir("SAME_DIRECTORY_YOU_USED_BEFORE\*.tmp") ' determine the number
    Do While s <> "" ' of files we will be using to reconstruct our original
    howManyFiles = howManyFiles + 1
    s = Dir
    Loop
    position1 = 1
    smallChunkSize = 32767
    Open "RECONSTRUCTION_FILE_NAME" for binary as #1 ' open the outfile
    for i = 1 to howManyFiles
    ' open the the temp file corresponding to the current index of the loop
    ' to be read into the outfile
    Open "SAME_DIRECTORY_YOU_USED_BEFORE\tempfile" & i & ".tmp" for binary as #2
    position2 = 1
    filesize = LOF(2)
    sizeCopied = 0
    Do While filesize - sizeCopied >= smallChunkSize ' begin reading data
    theData = string(smallChunkSize, " ") ' from the temp file and writing it
    get #2, position2, theData ' to the outfile in 32K chunks
    Put #1, position1, theData
    position2 = position2 + smallChunkSize
    position1 = position1 + smallChunkSize
    sizeCopied = position2 - 1
    Loop
    If filesize - sizeCopied > 0 then ' again, account for leftover
    theData = string(filesize - sizeCopied, " ") ' bytes of data that did not
    get #2, position2, theData ' neatly into our 32K chunks
    Put #1, position1, theData
    End If
    Close #2 ' close the temp file and increment the loop to start working
    next i ' with the next temp file, leaving the outfile open
    Close #1 ' close the outfile
    End Sub




    xxMariusxx


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured