B4A Library SD: ZipLibrary

SD_ZipLibray

Author:
Star-Dust
Version: 1.03
  • SD_ZipLibrary
    • Event
      • finish (Success as Boolean)
    • Functions:
      • Class_Globals As String
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • unZip (FileZip As String, OutPutFolder As String) As String
      • unZipList (FileZip As String) As List
      • Zip (FilesTxt As String(), FileZip As String) As String

SD_ZipLibray

Author:
Star-Dust
Version: 1.03
  • SD_ZipLibrary
    • Event
      • finish (Success as Boolean)
    • Functions:
      • Class_Globals As String
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • unZip (FileZip As String, OutPutFolder As String) As String
      • unZipList (FileZip As String) As List
      • Zip (FilesTxt As String(), FileZip As String) As String

List file in to Zip
B4X:
Dim UZ As SD_ZipLibrary

    UZ.Initialize
    Log("File: " & File.Exists(File.DirRootExternal,fileZipName))
    Dim L As List = UZ.unZipList(File.Combine(File.DirRootExternal,fileZipName))

Unzip
B4X:
Dim UZ As SD_ZipLibrary

UZ.Initialize
    UZ.unZip(File.Combine(File.DirRootExternal,fileZipName),File.Combine(File.DirRootExternal,DestinationPath))


Zip files
B4X:
Dim ZL As SD_ZipLibray
Dim ListFiles as List

ZL.Initialize(Me,"ZL")
ZL.Zip(ListFiles,ZipFileName)

Wait For
B4X:
Dim UZ As SD_ZipLibrary

UZ.Initialize
Log("File: " & File.Exists(File.DirRootExternal,fileZipName))
Dim L As List = UZ.unZipList(File.Combine(File.DirRootExternal,fileZipName))

Wait For UZ_finish(Success As Boolean)
Log("result:" &Success)
Log("Num: " & L.Size)
 

Attachments

  • SD_ZipLibray 0.13.zip
    3.9 KB · Views: 885
  • SD_ZipLibrary.bas
    4.8 KB · Views: 632
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Frankly I do not know, to create my application B4J I had to create a library to compress and decompress that B4J is not found.

I used Inline Java with the native library java.util.zip. *;
Then I thought about rewriting it for B4A, I did not even see if there were other Zip libraries in B4A.

My library is very simple because I am very limited in java, I suppose @Informatix has created something more complete.

Among other things, the source code of my library can be found within this application that I shared in the forum.

So who wants to make changes can also have the source code.
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Thanks, I was not sure and I solved this way.
But good to know for future uses
 

ilan

Expert
Licensed User
Longtime User
hi, @Star-Dust, your lib is working well for me. i have 2 questions.
the zip that is created is also created with the folder scheme as in my phone so if i zip a file in filedireinternel i get all folders to the zip and i have to dig in the zip to find the file itself. would it be possible to filter those folders and add only the file to the zip so when i open it i see all files in it and not inside folders?

question 2, it would be awesome to have a finish zip/unzip event that will be called when app has finished the job.

thanx a lot, ilan

btw. the difference between archiverplus and this lib is that archiverplus crashes on android 8+ and this one works ;)
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
hi, @Star-Dust, your lib is working well for me. i have 2 questions.
the zip that is created is also created with the folder scheme as in my phone so if i zip a file in filedireinternel i get all folders to the zip and i have to dig in the zip to find the file itself. would it be possible to filter those folders and add only the file to the zip so when i open it i see all files in it and not inside folders?

question 2, it would be awesome to have a finish zip/unzip event that will be called when app has finished the job.

thanx a lot, ilan

btw. the difference between archiverplus and this lib is that archiverplus crashes on android 8+ and this one works ;)
I believe it is possible to make selections and raise a decompression file event.I've simply used a native Java library, they are really 2 lines of code.
But I have to look good, because I do not overdo myself like I did. :p

Do you dare to java?
 

ilan

Expert
Licensed User
Longtime User
I believe it is possible to make selections and raise a decompression file event.I've simply used a native Java library, they are really 2 lines of code.
But I have to look good, because I do not overdo myself like I did. :p

Do you dare to java?

can you post the class that you converted to b4a lib?

thank you
 

wbtcpip

Member
Licensed User
Longtime User
sorry but i don't know how to initialize the object

ZL.Initialize is not enough, now a callback and event are required, what i have to pass here? (note that i need to receive the finish event)

i created the sub

Sub zl_finish(Success As Boolean)

End Sub

grazie
 

Star-Dust

Expert
Licensed User
Longtime User
sorry but i don't know how to initialize the object

ZL.Initialize is not enough, now a callback and event are required, what i have to pass here? (note that i need to receive the finish event)

i created the sub

Sub zl_finish(Success As Boolean)

End Sub

grazie
B4X:
Dim ZL As SD_ZipLibray
Dim ListFiles as List
 
ZL.Initialize(Me,"ZL")
ZL.Zip(ListFiles,ZipFileName)
 

wbtcpip

Member
Licensed User
Longtime User
During compiling i get:

ZL.Zip(ListFiles,zipfilename)
javac 1.8.0_144
src\mbsoft\mblive\main.java:1335: error: incompatible types: List<Object> cannot be converted to String[]
mostCurrent._zl._v7((String[])(_listfiles.getObject()),_zipfilename);

what im doing wrong?
 

Star-Dust

Expert
Licensed User
Longtime User
Show your code, the error says you tried to insert a string where there should be a list.

ListFile is a array of String, which must be populated with the names of the files to be compressed complete with the path.
If you use SDK 23+ you must have access to the card
 
Last edited:

wbtcpip

Member
Licensed User
Longtime User
B4X:
Dim ListFiles As List, zipfilename As String, mbint As Int, myfile As String
    ListFiles.Initialize
    DateTime.DateFormat ="dd_MMMM_yyyy"
    For mbint=0 To 6
        myfile="MBLive_" & DateTime.Date(DateTime.Add(DateTime.Now,0,0,-mbint)) & ".txt"'
        If File.Exists(File.DirInternal, myfile) Then
            ListFiles.Add(File.Combine(File.DirInternal,myfile))
        End If
    Next
    If ListFiles.Size > 0 Then
        ZL.Initialize(Me,"ZL")
        zipfilename=File.Combine(File.DirInternal, idtelefono & "-" & DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now) & ".zip")
        ZL.Zip(ListFiles,zipfilename)
    End If
 

Star-Dust

Expert
Licensed User
Longtime User
ListFiles is not a List but an Array of Strings. Change as necessary

B4X:
    Dim ListFiles As List, zipfilename As String, mbint As Int, myfile As String
    ListFiles.Initialize
    DateTime.DateFormat ="dd_MMMM_yyyy"
    For mbint=0 To 6
        myfile="MBLive_" & DateTime.Date(DateTime.Add(DateTime.Now,0,0,-mbint)) & ".txt"'
        If File.Exists(File.DirInternal, myfile) Then
            ListFiles.Add(File.Combine(File.DirInternal,myfile))
        End If
    Next
    If ListFiles.Size > 0 Then
        ZL.Initialize(Me,"ZL")
        zipfilename=File.Combine(File.DirInternal, idtelefono & "-" & DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now) & ".zip")
        ZL.Zip(ListToArray(ListFiles),zipfilename)
    End If

End Sub

B4X:
Sub ListToArray(L As List) As String()
    Dim S(l.Size) As String
  
    For i=0 To L.Size-1
        S(i)=L.Get(i)
    Next
  
    Return S
End Sub
 
Last edited:

wbtcpip

Member
Licensed User
Longtime User
all is ok now.

One last question: when i decomrpess the zip file on my windows desktop i get the full path:

"\data\user\0\mbsoft.mblive\files\MBLive_06_Novembre_2018.txt"

is there an option to zip a file without the full path? i only need "MBLive_06_Novembre_2018.txt"
 

Star-Dust

Expert
Licensed User
Longtime User
In my library you can select the destination folder, but recreate the original subfolders.
It is possible to delete the subfolders, but not in my library. I just realized something simple.

But you will find many libraries on the forum that is more powerful than this and will certainly better meet your needs.
I do not foresee any further updates to this library
 

JohnC

Expert
Licensed User
Longtime User
I can't seem to figure out how I can eliminate the separate ZL_Finish sub and simply use a "Wait For".

I tried the below, but the code just hangs:
B4X:
Zip.Initialize(Me,"Zip")
Zip.Zip(ArrayofFiles, File.Combine(ZipFileDirectory,ZipFileName))
Wait For (Zip) Finish(Success As Boolean)

Anyone have any suggestions?
 

DonManfred

Expert
Licensed User
Longtime User
try it like this
B4X:
Zip.Initialize(Me,"Zip")
Zip.Zip(ArrayofFiles, File.Combine(ZipFileDirectory,ZipFileName))
Wait For Zip_Finish(Success As Boolean)
' or 
'Wait For (Zip) Zip_Finish(Success As Boolean)
 
Top