We help IT Professionals succeed at work.

read/write bytes in chunks

Medium Priority
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

Watch Question

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?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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...
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


Ok, I tried the one file by itself and got the same error/issue. The zip class is from http://www.icsharpcode.net/OpenSource/SD/Download/. It works well except in this case where I'm overflowing the Byte.
I was able to do what I wanted to do MUCH easier using DotNetZip.
Thanks for your input.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.