Link to home
Start Free TrialLog in
Avatar of bucfanjeff
bucfanjeffFlag for United States of America

asked on

read/write bytes in chunks

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))
        zipStream.SetLevel(9) 
 
        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
            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()
 
    End Sub

Open in new window

SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bucfanjeff

ASKER

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...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial