B4J Question [Solved] Reading version number in manifest.txt from a b4xlib

aeric

Expert
Licensed User
Longtime User
The only way I know is to unzip the library to a temp folder (using a library such as Archiver), read the txt file into a Map and delete the folder recursively.
Any other suggestion?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Solution

aeric

Expert
Licensed User
Longtime User
I get this code from DeepSeek.
B4X:
' Reads the version from a .b4xlib file (B4A / B4J)
Sub GetB4XLibVersion (LibPath As String) As String
    Dim jZip As JavaObject
    jZip.InitializeNewInstance("java.util.zip.ZipFile", Array(LibPath))
    
    Dim jManifestEntry As JavaObject = jZip.RunMethod("getEntry", Array("manifest.txt"))
    If jManifestEntry.IsInitialized Then
        Dim jInputStream As JavaObject = jZip.RunMethod("getInputStream", Array(jManifestEntry))
        Dim jScanner As JavaObject
        jScanner.InitializeNewInstance("java.util.Scanner", Array(jInputStream, "UTF-8"))
        jScanner.RunMethod("useDelimiter", Array("\\A")) ' Read entire stream
        Dim manifestText As String = jScanner.RunMethod("next", Null)
        jScanner.RunMethod("close", Null)
        
        ' Parse version (e.g., "Version=1.00")
        Dim lines() As String = Regex.Split(CRLF, manifestText)
        For Each line As String In lines
            If line.StartsWith("Version=") Then
                Return line.SubString(8) ' Extract version number
            End If
        Next
    End If
    
    Return "Unknown"
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Using ZipToMap :
B4X:
Private Sub GetB4XLibVersion (LibPath As String) As String
    Dim files As Map = ZipToMap(LibPath)
    Dim b() As Byte = files.Get("manifest.txt")
    Dim txt As String = BytesToString(b, 0, b.Length, "utf8")
    Dim lines() As String = Regex.Split(CRLF, txt)
    For Each line As String In lines
        If line.StartsWith("Version=") Then
            Return line.SubString(8)
        End If
    Next
    Return "Unknown"
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub GetB4XLibVersion (LibPath As String) As String
    Dim jZip As JavaObject
    jZip.InitializeNewInstance("java.util.zip.ZipFile", Array(LibPath))
    Dim ZipEntry As JavaObject = jZip.RunMethod("getEntry", Array("manifest.txt"))
    If ZipEntry.IsInitialized Then
        Dim jInputStream As JavaObject = jZip.RunMethod("getInputStream", Array(ZipEntry))
        Dim b() As Byte = Bit.InputStreamToBytes(jInputStream)
        Dim txt As String = BytesToString(b, 0, b.Length, "utf8")
        Dim lines() As String = Regex.Split(CRLF, txt)
        For Each line As String In lines
            If line.StartsWith("Version=") Then
                Return line.SubString(8) ' Extract version number
            End If
        Next
    End If
    Return "Unknown"
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…