Results 1 to 3 of 3

Thread: Accessing zipped XML files from VB6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    245

    Accessing zipped XML files from VB6

    Hi,

    Many apps use a zipped xml file format these days. I've been searching the forums (and the rest of the web) for the best way to access these files from VB6. Unfortunately the terms zip/zipped/xml return so many results that I couldn't find anything of use.

    Does somebody have a suggestion on how to best handle these files? Rename, unzip to a temp folder, and then access the XML file(s) from code? Or is there a way to do all of that in memory, and avoid having to deal with temp files?

    Thanks in advance for all help, tips, etc.

    Regards,
    Erwin

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    Re: Accessing zipped XML files from VB6

    For zips you can use this `cZipArchive` class (pure VB6, no external/dll dependecies) along w/ `cMemoryStream` sample class from tests to extract files from a zip archive to byte-arrays.

    Easiest would be to pass an instance of `cMemoryStream` as `OutputFile` param of `Extract` method on `cZipArchive` like this:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim oArchive As cZipArchive
    3.     Dim oStylesStream As cMemoryStream
    4.    
    5.     Set oArchive = New cZipArchive
    6.     Set oStylesStream = New cMemoryStream
    7.     oArchive.OpenArchive "D:\TEMP\Book1.xlsx"
    8.     oArchive.Extract vbNullString, "xl\styles.xml", oStylesStream
    9.     Debug.Print UBound(oStylesStream.Contents)
    10. End Sub

    A more complicated approach would be to sink `BeforeExtract` event and create/assign instances of `cMemoryStream` to `File` event parameter as needed. These instances have to be appended to a collection to be accessible after extraction.

    cheers,
    </wqw>

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    Re: Accessing zipped XML files from VB6

    FYI, just added built-in support for byte arrays to the VFS (virtual file system) of cZipArchive project allowing for all in-memory operations w/ no memory stream helper needed any more.

    Here is a simplified yet more complete sample client code, incl. utf-8 conversion for the XML payload.
    vb Code:
    1. Option Explicit
    2.  
    3. '--- for WideCharToMultiByte
    4. Private Const CP_UTF8                       As Long = 65001
    5.  
    6. Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
    7. Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    8.  
    9. Private Sub Form_Load()
    10.     Dim oArchive    As cZipArchive
    11.     Dim baStyles()  As Byte
    12.    
    13.     Set oArchive = New cZipArchive
    14.     oArchive.OpenArchive "D:\TEMP\Book1.xlsx"
    15.     oArchive.Extract baStyles, "xl\styles.xml" '<--- extracts to byte array
    16.     Debug.Print FromUtf8Array(baStyles)
    17. End Sub
    18.  
    19. Public Function ToUtf8Array(sText As String) As Byte()
    20.     Dim baRetVal()      As Byte
    21.     Dim lSize           As Long
    22.    
    23.     lSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), ByVal 0, 0, 0, 0)
    24.     If lSize > 0 Then
    25.         ReDim baRetVal(0 To lSize - 1) As Byte
    26.         Call WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), baRetVal(0), lSize, 0, 0)
    27.     Else
    28.         baRetVal = vbNullString
    29.     End If
    30.     ToUtf8Array = baRetVal
    31. End Function
    32.  
    33. Public Function FromUtf8Array(baText() As Byte) As String
    34.     Dim lSize           As Long
    35.    
    36.     FromUtf8Array = String$(2 * UBound(baText), 0)
    37.     lSize = MultiByteToWideChar(CP_UTF8, 0, baText(0), UBound(baText) + 1, StrPtr(FromUtf8Array), Len(FromUtf8Array))
    38.     FromUtf8Array = Left$(FromUtf8Array, lSize)
    39. End Function
    cheers,
    </wqw>

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width