As is stated here: https://www.b4x.com/android/forum/threads/zip-multiple-folders-and-files.98548/, I need to zip multiple files and folders into one zip file.
zip4j is an easy to use library. The code is very simple. I only implemented the zip part.
To use:
zip4j is an easy to use library. The code is very simple. I only implemented the zip part.
B4X:
Sub Class_Globals
Private fx As JFX
Private th As Thread
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
th.Initialise("th")
End Sub
Public Sub unzipAsync(dir As String,filename As String,outdir As String) As ResumableSub
th.Start(Me,"unzip",Array As Object(dir,filename,outdir))
wait for th_Ended(endedOK As Boolean, error As String)
Log(endedOK)
Return endedOK
End Sub
Public Sub zipFilesAsync(inDir As String,outDir As String,zipFilename As String) As ResumableSub
th.Start(Me,"zipFiles",Array As Object(inDir,outDir,zipFilename))
wait for th_Ended(endedOK As Boolean, error As String)
Log(endedOK)
Return endedOK
End Sub
Public Sub unzip(dir As String,filename As String,outdir As String)
Dim zipFile As JavaObject
zipFile.InitializeNewInstance("net.lingala.zip4j.core.ZipFile",Array(File.Combine(dir,filename)))
zipFile.RunMethod("extractAll",Array(outdir))
End Sub
Public Sub zipFiles(inDir As String,outDir As String,zipFilename As String)
If File.Exists(outDir,zipFilename) Then
File.Delete(outDir,zipFilename)
End If
Dim zipFile As JavaObject
zipFile.InitializeNewInstance("net.lingala.zip4j.core.ZipFile",Array(File.Combine(outDir,zipFilename)))
Dim constants As JavaObject
constants.InitializeStatic("net.lingala.zip4j.util.Zip4jConstants")
Dim params As JavaObject
params.InitializeNewInstance("net.lingala.zip4j.model.ZipParameters",Null)
params.RunMethod("setCompressionMethod",Array(constants.GetField("COMP_DEFLATE")))
params.RunMethod("setCompressionLevel",Array(constants.GetField("DEFLATE_LEVEL_NORMAL")))
For Each filename In File.ListFiles(inDir)
Dim path As String
path=File.Combine(inDir,filename)
If File.IsDirectory(inDir,filename) Then
zipFile.RunMethod("addFolder",Array(path,params))
Else
zipFile.RunMethod("addFile",Array(getFile(path),params))
End If
Next
End Sub
Sub getFile(path As String) As JavaObject
Dim fileJO As JavaObject
fileJO.InitializeNewInstance("java.io.File",Array(path))
Return fileJO
End Sub
To use:
B4X:
#AdditionalJar: zip4j-1.3.2
Dim zip As zip4j
zip.Initialize
wait for (zip.zipFilesAsync(File.Combine(File.DirApp,"inDir"),File.DirApp,"out.zip")) Complete (success as boolean)
wait for (zip.unzipAsync(File.DirApp,"out.zip",File.Combine(File.DirApp,"outDir")) Complete (success as boolean)
Last edited: