read/write bytes in chunks

Posted on 2009-02-11
Last Modified: 2012-05-06
I have this code zipping files, directories, subdir/files, etc successfully - almost.
When I get to a large file, I get the error "Arithmetic operation resulted in an overflow". Based on what I've read, my zFileBuffer is larger than As Byte can handle and I need to read and write the bytes in chunks, say 1024. I'm very much a beginner, can someone please help adjust my code to read/write bytes so I don't overload the byte buffer?
Many, many, many thanks.
Private Sub Zip(ByVal SourceFolder As String, ByVal DestFile As String)

        Dim DirFileNames() As String = Directory.GetFiles(SourceFolder, "*", SearchOption.AllDirectories)

        Dim objCrc32 As New Crc32()

        Dim zipStream As ZipOutputStream

        zipStream = New ZipOutputStream(File.Create(DestFile))


        Dim zFile As String

        For Each zFile In DirFileNames

            Dim zFileStream As FileStream = File.OpenRead(zFile)

            Dim zFileBuffer(zFileStream.Length - 1) As Byte

            Dim strSource As Integer = SourceFolder.Length + 1

            zFileStream.Read(zFileBuffer, 0, zFileBuffer.Length)

            Dim objZipEntry As ZipEntry = New ZipEntry(zFile.Substring(strSource))

            objZipEntry.DateTime = DateTime.Now

            objZipEntry.Size = zFileStream.Length




            objZipEntry.Crc = objCrc32.Value


            zipStream.Write(zFileBuffer, 0, zFileBuffer.Length)




    End Sub

Open in new window

Question by:bucfanjeff
    LVL 41

    Assisted Solution

    I'm not 100% certain, but I think there is a 4 Gb limit to the size of the file...     How big is the file that's causing the problem?

    Author Comment

    3.41GB - Which for most practical situations would be abnormal. I know it's an overload of the Byte, which is why I have to read/write the file in smaller chunks. I just don't know how...
    LVL 41

    Assisted Solution

    Try that one large file by itself.... that way we'll know if it's the size of the zip file or the size of that one entry that causing the problem.
    BTW:  I don't recognize that Zip class... it's somewhat resembles the J++ class, but not quite

    Author Comment

    Ok, I tried the one file by itself and got the same error/issue. The zip class is from It works well except in this case where I'm overflowing the Byte.

    Accepted Solution

    I was able to do what I wanted to do MUCH easier using DotNetZip.
    Thanks for your input.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    758 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

    8 Experts available now in Live!

    Get 1:1 Help Now