locked
Arithmetic operation resulted in an overflow RRS feed

  • Question

  • User556867334 posted

    I have this zip program that works nicely until I get to large files. What am I doing wrong?

    I get a "Arithmetic operation resulted in an overflow" error on:

    Dim zFileBuffer(zFileStream.Length - 1) As Byte

    -----------------------------------

    Code: 

     

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

    Dim objCrc32 As New Crc32()

    Dim zipStream As ZipOutputStream

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

    zipStream.SetLevel(9)

    Dim zFile As String

    For Each zFile In azFileNames

    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

    zFileStream.Close()

    objCrc32.Reset()

    objCrc32.Update(zFileBuffer)

    objZipEntry.Crc = objCrc32.Value

    zipStream.PutNextEntry(objZipEntry)

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

    Next

    zipStream.Finish()

    zipStream.Close()

    Tuesday, February 10, 2009 4:26 PM

Answers

  • User556867334 posted

    I found a better solution and I was able to create what I wanted MUCH easier with DotNetZip.

    Thanks for all the input.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 12, 2009 3:46 PM

All replies

  • User-754076202 posted

    Bucfanjeff,

    That specific error, "Arithmetic operation result is an over flow," means that the buffer size your creating is not large enough for the data you are storing into it.  Taking your line of code that gave you the error:

    Dim zFileBuffer(zFileStream.Length - 1) As Byte 

     You are declaring the zfilebuffer() as a byte. Looking at these data type definitions below, we see that a byte can store value from 0 to 255. So, your zfilestream.length is larger than a byte. You should try to declare it as a integer or double.

    ********Data Types**********

    • Byte
    the Byte data type is an 8-bit variable which can store value from 0 to 255. This data type is very useful for storing binary data. It can also be very useful when sending/receiving byte values to/from a Basic Stamp or PIC.

    • Double
    The Double data type is a 64-bit floating point number used when high accuracy is needed. These variables can range from -1.79769313486232e308 to -4.94065645841247e-324 for negative values and from 4.94065645841247e-324 to 1.79769313486232e308 for positive values.

    • Integer
    The Integer data type is a 16-bit number which can range from -32768 to 32767. Integers should be used when you are working with values that can not contain fractional numbers.

     

    Try changing this: Dim zFileBuffer(zFileStream.Length - 1) As Byte 

    to: Dim zFileBuffer(zFileStream.Length - 1) As Integer

     

    Tuesday, February 10, 2009 5:50 PM
  • User556867334 posted

    Thanks for the answer. Based on my searches, you're right, too much data for the buffer size. However, since I'm using a stream, I assume it has to be bytes. If I change it to - say - Integer, I get Value of type '1-dimensional array of Integer' cannot be converted to '1-dimensional array of Byte' because 'Integer' is not derived from 'Byte' on the read of zfilebuffer in zFileStream.Read(zFileBuffer, 0, zFileBuffer.Length).

    Based on what I have found so far, I'm going to have to read/write the file in byte chunks. Since I am a rookie, I'm not sure how to do that. Can you (or anyone) show me how to do that?

    Thanks in advance.

    Wednesday, February 11, 2009 8:43 AM
  • User556867334 posted

    I found a better solution and I was able to create what I wanted MUCH easier with DotNetZip.

    Thanks for all the input.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 12, 2009 3:46 PM