dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] RC5 Stream

  1. #1

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Resolved [RESOLVED] RC5 Stream

    I have a task where I used RC5 library due to some functions that suits my needs. The main file is coming in the MIME standard and does contain an attachment as tar.gz format. The processes of file reading, writing, decoding, decompressing are as follow:
    Code:
    Dim Stream As vbRichClient5.cStream
    Dim BSrc() As Byte, BDst() As Byte, outCSV As String, outTar As String, decFile As String
    
            outCSV = App.Path & "\myTest.csv" 
            outTar = App.Path & "\myTest.tar.gz"
    
           decFile = New_c.Crypt.Base64Dec(encFile)
              New_c.FSO.WriteTextContent outTar, decFile 'write the output-file
        Set Stream = New_c.FSO.CreateFileStream(outCSV, STRM_WRITE) 'create the csv file as a Stream
              BSrc = New_c.FSO.ReadByteContent(outTar)       'read the source-file
              New_c.Crypt.GzDecompress(BSrc, BDst)           'GZ-Decompression from Src-to-Dst-ByteArrays
              Stream.WriteFromByteArr(BDst)                  'append the ByteArray to the out-stream
              New_c.FSO.DeleteFile(outTar)
    The question is : could I pass the decoded file decFile to the GzDecompress function directly without to save it on disk via WriteTextContent ? As long as the purpose is to extract the csv file from archive I would like to avoid to save/delete the tar.gz file. Thanks.
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,376

    Re: RC5 Stream

    Since calling:
    New_c.Crypt.Base64Dec(strB64, True)
    will return a ByteArray (due to the last optional Param at True),
    you can then directly pass this Base64Dec-Result as the input of the GZDecompress-Method...

    As e.g. in this example-routine, which puts that into practice:
    Code:
    Function DecodeMimeB64(sMimeB64 As String) As Byte()
      New_c.Crypt.GzDecompress New_c.Crypt.Base64Dec(sMimeB64, True), DecodeMimeB64
    End Function
    Usage of that routine then similar as in the following test-code:
    Code:
    Private Sub Form_Load()
      With New_c.ArrayList(vbString)
        'the following 5 lines represent valid (base64-encoded) tar.gz content
        .Add "H4sICMZVGl0CAFRlc3QudGFyAO2PQXKDMAxFWXemd9ABmIxpApyiu15AYIE9NTJjmSTcvnLaRS+Q"
        .Add "VXgbe76+vqQvknzK91w9kcaYvu+hgoL59/5x+WigObemu7RNdzaqtN25hcpUB8/mExm8gPWSPc+b"
        .Add "F0e2Bo4ZIocdhh2clhOhRK5h2HKRctGk+AMmWFHER4YpxQVidpQA2S8YpIb3t5vzoysjEMImGjuB"
        .Add "WmDxbGv9YQnU2kpJ6EoJeaTisRT87DJ4ftjHyLrfRlajrYqWJsx+xiGQzpiJtTPrEqX1m+MtkJ2p"
        .Add "BrqPRFYeEeJiynAlRwv9DtGsHUZMjAHWoCduiU7VwcHBwWvwA90zgp8ACAAA"
        
        'B64-decode + GZ-decompress the (joined) Input-String
        Dim BDec() As Byte: BDec = DecodeMimeB64(.Join(vbCrLf))
        
        'Print the BDec-content via StrConv (treating the ByteArray as ANSI-Input)
        Debug.Print "ByteLen:"; UBound(BDec) + 1; vbLf; StrConv(BDec, vbUnicode)
      End With
    End Sub
    Olaf

  3. #3

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: RC5 Stream

    Thank you for your support, Olaf. Very useful the Base64Dec function because it may return the result either as string or byte array. In the same time it is amazing that in a single line of code you extracted a content that is encoded, compressed and archived. I felt from beginning that my code is redundant but I omitted to read carefully that second parameter...Because it's not my style to write the code as condensed as you do I preferred the following 3 lines that solve my task in a very readable manner. By the way, I also appreciated Thomas Hobbes's quote.
    Code:
    BSrc = New_c.Crypt.Base64Dec(encFile, True)
           New_c.Crypt.GzDecompress BSrc, BDst
           New_c.FSO.WriteByteContent outCSV, BDst
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  4. #4
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: [RESOLVED] RC5 Stream

    There is a bit of overhead with the New_C.Crypt call as the initialization of the Crypt class takes some time and you are performing it twice with your New_C.Crypt calls.

    If this is a routine you'll be calling often, or in a tight loop, you might want to cache an instance of the cCrypt class in a variable to avoid this overhead.

    e.g.

    Code:
    Option Explicit
    
    Private mo_Crypt As vbRichClient5.CCrypt
    
    Public Sub DecodeMimeB64ToFile(sMimeB64 As String, ByVal OutFilePath As String)
       With mo_Crypt
          BSrc = .Base64Dec(sMimeB64 , True)
          .GzDecompress BSrc, BDst
       End With
    
       New_c.FSO.WriteByteContent OutFilePath, BDst
    End Function
    
    Private Sub Class_Initialize()
       Set mo_Crypt = New_C.Crypt
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width