"Not enough memory to complete this operation" when base64-encoded zip file

Below is the code to convert the zip file to base64 format.

Dim inByteArray, base64Encoded,
Const TypeBinary = 1
inByteArray = readBytes("F:path/file.zip")
base64Encoded = encodeBase64(inByteArray)

Private Function readBytes(file)
    Dim inStream
    ' ADODB stream object used
    Set inStream = CreateObject("ADODB.Stream")
    ' open with no arguments makes the stream an empty container 
    inStream.Open
    inStream.Type = TypeBinary
    inStream.LoadFromFile(file)
    readBytes = inStream.Read()
End Function

Private Function encodeBase64(bytes)
    Dim DM, EL
    Set DM = CreateObject("Microsoft.XMLDOM")
    ' Create temporary node with Base64 data type
    Set EL = DM.CreateElement("tmp")
    EL.DataType = "bin.base64"
    ' Set bytes, get encoded String
    EL.NodeTypedValue = bytes
    encodeBase64 = EL.Text
End Function

I tried first with a 3 MB zip file. It worked fine. But when I try with a 34 MB zip file, it says

There is not enough memory to complete this operation.

in line

encodeBase64 = EL.Text

Is there a way that I can process zip files of all sizes because my files are basically 30 MB or more in size.

+4
source share
2 answers

edited 2017/01/10 - (the original answer is kept below)

2017/01/10 - () - ( ) .

. : ( 10MB) , (, 10MB) (. ).

Option Explicit

Dim buffer
    buffer = encodeFileBase64( "file.zip" ) 

    WScript.StdOut.WriteLine( CStr(Len(buffer)) )


Private Function encodeFileBase64( file )
    ' Declare ADODB used constants
    Const adTypeBinary = 1
    Const adTypeText = 2

    ' Declare FSO constants
    Const TEMP_FOLDER = 2

    ' Initialize output
    encodeFileBase64 = ""

    ' Instantiate FileSystemObject
    Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

    ' Check input file exists
    If Not fso.FileExists( file ) Then 
        Exit Function
    End If 

    ' Determine how we will handle data buffering.
    ' Use a temporary file for large files 
    Dim useTemporaryFile
    useTemporaryFile = fso.GetFile( file ).Size > 10 * 1048576

    ' Instantiate the B64 conversion component
    Dim b64 
    Set b64 = WScript.CreateObject("Microsoft.XMLDOM").CreateElement("tmp")
        b64.DataType = "bin.base64"

    Dim outputBuffer, outputBufferName
    If useTemporaryFile Then 
        ' Create a temporary file to be used as a buffer
        outputBufferName = fso.BuildPath( _ 
            fso.GetSpecialFolder( TEMP_FOLDER ), _ 
            fso.GetTempName() _ 
        )
        Set outputBuffer = fso.CreateTextFile( outputBufferName, True )
    Else 
        ' Instantiate a text stream to be used as a buffer to avoid string 
        ' concatenation operations that were generating out of memory problems
        Set outputBuffer = WScript.CreateObject("ADODB.Stream")
        With outputBuffer
            ' Two bytes per character, BOM prefixed buffer
            .Type = adTypeText
            .Charset = "Unicode"
            .Open
        End With 
    End If 

    ' Instantiate a binary stream object to read input file 
    With WScript.CreateObject("ADODB.Stream")
        .Open
        .Type = adTypeBinary
        .LoadFromFile(file)

        ' Iterate over input file converting the file, converting each readed
        ' block to base64 and appending the converted text into the output buffer
        Dim inputBuffer
        Do
            inputBuffer = .Read(3145716)
            If IsNull( inputBuffer ) Then Exit Do

            b64.NodeTypedValue = inputBuffer
            If useTemporaryFile Then 
                Call outputBuffer.Write( b64.Text )
            Else 
                Call outputBuffer.WriteText( b64.Text )
            End If 
        Loop 

        ' Input file has been readed, close its associated stream
        Call .Close()
    End With

    ' It is time to retrieve the contents of the text output buffer into a 
    ' string. 

    If useTemporaryFile Then 
        ' Close output file 
        Call outputBuffer.Close()
        ' Read all the data from the buffer file
        encodeFileBase64 = fso.OpenTextFile( outputBufferName ).ReadAll()
        ' Remove temporary file
        Call fso.DeleteFile( outputBufferName )

    Else 

        ' So, as we already have a Unicode string inside the stream, we will
        ' convert it into binary and directly retrieve the data with the .Read() 
        ' method.
        With outputBuffer
            ' Type conversion is only possible while at the start of the stream
            .Position = 0
            ' Change stream type from text to binary
            .Type = adTypeBinary
            ' Skip BOM
            .Position = 2
            ' Retrieve buffered data
            encodeFileBase64 = CStr(.Read())
            ' Ensure we clear the stream contents
            .Position = 0
            Call .SetEOS()
            ' All done, close the stream
            Call .Close()
        End With 
    End If 

End Function

?

. . , cscript.exe, 32- 90 64- 500 .

?

  • stream ( ), , : ,

  • , , , .

10MB, , , - 32- . 90 32- , , .

stream Unicode, .Read()?

stream.ReadText() . / (, ), .


. , .


/

Option Explicit
Const TypeBinary = 1

Dim buffer
    buffer = encodeFileBase64( "file.zip" ) 
    WScript.StdOut.WriteLine( buffer )

Private Function encodeFileBase64( file )

    Dim b64 
    Set b64 = WScript.CreateObject("Microsoft.XMLDOM").CreateElement("tmp")
        b64.DataType = "bin.base64"

    Dim outputBuffer
    Set outputBuffer = WScript.CreateObject("Scripting.Dictionary")

    With WScript.CreateObject("ADODB.Stream")
        .Open
        .Type = TypeBinary
        .LoadFromFile(file)

        Dim inputBuffer
        Do
            inputBuffer = .Read(3145716)
            If IsNull( inputBuffer ) Then Exit Do

            b64.NodeTypedValue = inputBuffer
            outputBuffer.Add outputBuffer.Count + 1, b64.Text 
        Loop 

        .Close
    End With

    encodeFileBase64 = Join(outputBuffer.Items(), vbCrLf)
End Function

:

  • , . , . , , .

  • 3145716 - 54 ( base64) 3145728 (3 ).

+2

22/02/2017 - 34 - Run Error -out . . enter image description here

encodeBase64 , , . 1074- ,

The encodeBase64 has array structure like below which fails at some point.If you can see 1074th position it says incorrect expression

0

Source: https://habr.com/ru/post/1664266/


All Articles