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: 849
  • SD_ZipLibrary.bas
    4.8 KB · Views: 593
Last edited:

ilan

Expert
Licensed User
Longtime User
NEW
I decided to make public the code with which I written this library. Use some java code, I think it can help the community understand how to use java (I know very little about it) inside b4x and maybe develop their own libraries.

Find the .BAS file with the source at the first post

i think you have uploaded the wrong .bas file. i see "Starter.bas" wich is the starter service.
 

Star-Dust

Expert
Licensed User
Longtime User
i think you have uploaded the wrong .bas file. i see "Starter.bas" wich is the starter service.
Excuse me. As soon as I return from the sea, post the file
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Freeze
 

Star-Dust

Expert
Licensed User
Longtime User
i think you have uploaded the wrong .bas file. i see "Starter.bas" wich is the starter service.
FIX it.

I live at a distance of 5 minutes from the sea, when I want to close the office and go to the sea.
1598002931751.png
 
Last edited:

lelelor

Member
Licensed User
buongiorno, per il momento ho accantonato il progetto per l'invio di scontrini, ma lo riprenderò.
ho la necessità di creare un file zip ed ho scaricato la Vs. libreria, siccome questo file .zip sarà composto da un unico file .txt non ho trovato il modo di crearlo, può darmi un consiglio?

Grazie
 

Star-Dust

Expert
Licensed User
Longtime User
Please, in the international forum, write in English
B4X:
Dim Zip As SD_ZipLibrary
Dim ZipFileDirectory As String = XUI.DefaultFolder
Dim MyFileName As String = File.Combine(OriginalPath,"myfiles.txt")
Zip.Initialize(Me,"Zip")

Zip.Zip(array as string(MyFileName), File.Combine(ZipFileDirectory,"myfilecompress.zip"))

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

You have to take into account that now with the latest Android versions you can't access DirRootExternal easily. So if the original file to be compressed is external you have to use ContentChooser

And the compressed file must be saved internally. (e.g. XUI.DefaultFolder). If you need to copy it externally or pass it to some other app you have to see the latest instructions on this
 
Last edited:

lelelor

Member
Licensed User
Please, in the international forum, write in English
B4X:
Dim Zip As SD_ZipLibrary
Dim ZipFileDirectory As String = XUI.DefaultFolder
Dim MyFileName As String = File.Combine(OriginalPath,"myfiles.txt")
Zip.Initialize

Zip.Zip(array as string(MyFileName), File.Combine(ZipFileDirectory,"myfilecompress.zip"))

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

You have to take into account that now with the latest Android versions you can't access DirRootExternal easily. So if the original file to be compressed is external you have to use ContentChooser

And the compressed file must be saved internally. (e.g. XUI.DefaultFolder). If you need to copy it externally or pass it to some other app you have to see the latest instructions on this

Hello, I am attaching the button I use to create the .zip file, from the example I do not understand what UZ.finish is
basically I can't find the .zip file, it's not created

</>
Sub BTZip_click
Dim giorno As String
Dim sf As StringFunctions2
Dim aaaa As String
Dim gg As String
Dim mm As String
Dim oraminutisecondi As String
Dim adesso As String
Dim hh As String
Dim mn As String
Dim ss As String


Dim Zip As SD_ZipLibrary
Dim ZipFileDirectory As String = xui.DefaultFolder
'Dim ZL As SD_ZipLibrary
Dim myfilename As String = File.Combine(File.DirInternal,"/anaiper/anaiperC.txt")
Dim zipfilename As String

'Log("fileinternal " & File.dirinternal)

giorno=sf.Today
mm=giorno.substring2(0,2)
gg=giorno.substring2(3,5)
aaaa=giorno.SubString2(6,10)
adesso=DateTime.Now
oraminutisecondi = DateTime.Time(adesso)
hh=oraminutisecondi.SubString2(0,2)
mn=oraminutisecondi.SubString2(3,5)
ss=oraminutisecondi.SubString(6)
'Log ("anno " & aaaa)
'Log("giorno " & gg)
'Log("mese " & mm)
'Log(oraminutisecondi)
'Log("ora "& hh)
'Log("minuti " & mn)
'Log("secondi " & ss)
'Zip.Initialize

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

zipfilename="rileva"& aaaa & mm & gg & hh & mn & ss & "_" & gg &"-" & mm & "-" & aaaa & "-" & hh & "." & mn & "." & ss & "_SC_.zip "

Log("zipfilename " & zipfilename)

Log("zipfiledirectory " & ZipFileDirectory)

Log("MYFILENAME " & myfilename)

Zip.Initialize (Me,"Zip")
' ZL.Zip(Array As String(myfilename), File.Combine(ZipFileDirectory,"myfilecompress.zip"))
Zip.Zip(Array As String(myfilename), File.Combine(ZipFileDirectory,"rileva"& aaaa & mm & gg & hh & mn & ss & "_" & gg &"-" & mm & "-" & aaaa & "-" & hh & "." & mn & "." & ss & "_SC_.zip "))
' Zip.Zip(myfilename, File.Combine(ZipFileDirectory,zipfilename))
'Zip.Zip(Array As String(myfilename), File.Combine(ZipFileDirectory,"myfilecompress.zip"))

Wait For UZ_finish(Success As Boolean)
Log("result:" & Success)
' Log(Zip.Zip)
'Log("Num: " & Zip.Zip )
Log("fatto?")
End Sub
</>
 

Star-Dust

Expert
Licensed User
Longtime User
please when posting some code use the panel to insert the source. so it is illegible.

Zip_Finish (Uz_Finish Is wrong) is the event that is raised when the compression is complete. If successful, the success variable is true. you can find the zip file in the folder you chose if you have permission to write to that folder.
The name of the event is determined by the name you chose in initialization
 

lelelor

Member
Licensed User
please when posting some code use the panel to insert the source. so it is illegible.

Zip_Finish (Uz_Finish Is wrong) is the event that is raised when the compression is complete. If successful, the success variable is true. you can find the zip file in the folder you chose if you have permission to write to that folder.
The name of the event is determined by the name you chose in initialization
ok, but maybe I haven't explained myself, the zip file IS NOT CREATED, I attached the code maybe I did something wrong, it didn't let me insert the terminators from the panel ...
 

Star-Dust

Expert
Licensed User
Longtime User
I didn't understand anything about the code you wrote, it seems full of errors and lines of code that does nothing.

I have attached you a simple and working example of compressing a text file.
 

Attachments

  • zip_sample2.zip
    10.4 KB · Views: 156
Last edited:

lelelor

Member
Licensed User
I didn't understand anything about the code you wrote, it seems full of errors and lines of code that does nothing.

I have attached you a simple and working example of compressing a text file.
thanks for the example file, but it doesn't work, I can't find the lelelor.zip file nor the b4a.example.zip folder, I'm a beginner and I make mistakes but something I understand. if the example sent to me has to create the lelelor.zip file in a folder on the periphery where I execute the file, this is not the case. Thank you
 

Star-Dust

Expert
Licensed User
Longtime User
The source I sent you, I tried it and it's working.
So we have two alternatives. You have a problem with the device or you are doing something wrong.

Let's start with the second hypothesis.
How do you say it's not there?
Find this code at the end of compression that checks if the file is there and puts the result on the boolean variable.
Are you sure the file exists?
B4X:
Dim b As Boolean = File.Exists(File.DirInternal, "leleolor.zip")
If b Then
    xui.MsgboxAsync("File creato ed esistente","Success")
Else
    xui.MsgboxAsync("Compressione effetuata, file non trovato","Success")
End If
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
If, on the other hand, your question is different ... How can you share the ZIP file created ... then the question is different.

As you know the DirInternal folder is not accessible from the eternal. But only from the App that contains it.
So for file sharing you can do a search on the forum and you will find many examples
 

ilan

Expert
Licensed User
Longtime User
thanks for the example file, but it doesn't work, I can't find the lelelor.zip file nor the b4a.example.zip folder, I'm a beginner and I make mistakes but something I understand. if the example sent to me has to create the lelelor.zip file in a folder on the periphery where I execute the file, this is not the case. Thank you

how do you know that it does not exists? Are u checking it with file.exists() function?

are u trying to look for the file from an 3rd party app like a file explorer?
 

lelelor

Member
Licensed User
how do you know that it does not exists? Are u checking it with file.exists() function?

are u trying to look for the file from an 3rd party app like a file explorer?
I would just like to create this blessed file.zip from a fie.txt and make it visible to be able to copy it to another device on the network.
if I do not see I do not believe it, said St. Thomas, it is difficult to understand where you are wrong if the others do not point out the error but only that the error is there.
if I knew the language I would not ask for help, I am studying but at a certain age it is more difficult, maybe ...
Thanks
 

ilan

Expert
Licensed User
Longtime User
I would just like to create this blessed file.zip from a fie.txt and make it visible to be able to copy it to another device on the network.
if I do not see I do not believe it, said St. Thomas, it is difficult to understand where you are wrong if the others do not point out the error but only that the error is there.
if I knew the language I would not ask for help, I am studying but at a certain age it is more difficult, maybe ...
Thanks

as @Star-Dust already mentioned you cannot access the File.DirInternal folder from outside the app.

after you create the file check if exists.

using @Star-Dust example you can check if with this command:

B4X:
Log($"File exists: ${File.Exists(File.DirInternal, "leleolor.zip")}"$)

File exists: true

so you can see that the file was created. this file is created in File.DirInternal what means ONLY you can access it. now if you want to share it use the FileProvider library


and you can share your zip with other apps like email, whatsapp, ...
 

Star-Dust

Expert
Licensed User
Longtime User
your problem is not that it fails to generate the Zip file or that you cannot see if it exists. your problem is you don't know how to share it. this aspect is not part of the library. as I wrote to you before you have to search the forum and understand how to share it.

PS. I remind you that St. Thomas when he said this expression that there is a very bad figure, because Jesus answered him blessed are those who believe without seeing.
 

AnandGupta

Expert
Licensed User
Longtime User
Hi lelelor

Welcome. We all have to learn a new language we have to survive in this ever changing world.
That said, since you are learning the language and Android logic, which Google keeps on changing all time, making experience users pull their hairs, forget about learners.

Now for your problem, I will suggest go step by step.
If you are using old version of Android like 5 or 6, then many things will work easy as Android was less restrictive and more open then.
If you are using newer version of Android like 10 or 11, then many things will not work easy as Android is more restrictive now.

Please give your Android details here and we may be able to give better solution.

Keep in mind if you are afraid to fall down, you will never learn to walk. Look at the baby he/she is never afraid to fall but looks for support.
Members here are more supportive and helpful and we will not let you fall. That's all I can say.
 
Top