-
Oct 20th, 2017, 08:40 AM
#1
Thread Starter
Addicted Member
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
-
Oct 20th, 2017, 09:00 AM
#2
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:
Private Sub Form_Load()
Dim oArchive As cZipArchive
Dim oStylesStream As cMemoryStream
Set oArchive = New cZipArchive
Set oStylesStream = New cMemoryStream
oArchive.OpenArchive "D:\TEMP\Book1.xlsx"
oArchive.Extract vbNullString, "xl\styles.xml", oStylesStream
Debug.Print UBound(oStylesStream.Contents)
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>
-
Nov 17th, 2017, 12:36 PM
#3
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:
Option Explicit
'--- for WideCharToMultiByte
Private Const CP_UTF8 As Long = 65001
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
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
Private Sub Form_Load()
Dim oArchive As cZipArchive
Dim baStyles() As Byte
Set oArchive = New cZipArchive
oArchive.OpenArchive "D:\TEMP\Book1.xlsx"
oArchive.Extract baStyles, "xl\styles.xml" '<--- extracts to byte array
Debug.Print FromUtf8Array(baStyles)
End Sub
Public Function ToUtf8Array(sText As String) As Byte()
Dim baRetVal() As Byte
Dim lSize As Long
lSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), ByVal 0, 0, 0, 0)
If lSize > 0 Then
ReDim baRetVal(0 To lSize - 1) As Byte
Call WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), baRetVal(0), lSize, 0, 0)
Else
baRetVal = vbNullString
End If
ToUtf8Array = baRetVal
End Function
Public Function FromUtf8Array(baText() As Byte) As String
Dim lSize As Long
FromUtf8Array = String$(2 * UBound(baText), 0)
lSize = MultiByteToWideChar(CP_UTF8, 0, baText(0), UBound(baText) + 1, StrPtr(FromUtf8Array), Len(FromUtf8Array))
FromUtf8Array = Left$(FromUtf8Array, lSize)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|