B4J Code Snippet zip multiple files and folders using zip4j

Discussion in 'B4J Code Snippets' started by xulihang, Oct 23, 2018.

  1. xulihang

    xulihang Active Member Licensed User

    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.

    Code:
    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 StringAs 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 StringAs 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 StringAs JavaObject
        
    Dim fileJO As JavaObject
        fileJO.InitializeNewInstance(
    "java.io.File",Array(path))
        
    Return fileJO
    End Sub
    To use:
    Code:
    #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: Jun 5, 2019
    Erel, inakigarm and DonManfred like this.
  2. Michael Müller Anywhere

    Michael Müller Anywhere Member Licensed User

    Here is an example for unzip (you can expand the class-modul code above):
    (That was the best combination for me without errors when a file is not readable)
    --------------------------------------------------------------------------

    Public Sub unzipFiles(outDir As String, zipFilename As String)
    Dim zipFile As JavaObject
    Dim constants As JavaObject

    zipFile.InitializeNewInstance("net.lingala.zip4j.core.ZipFile", Array(zipFilename))
    constants.InitializeStatic("net.lingala.zip4j.util.Zip4jConstants")

    If File.IsDirectory( outDir, "") Then
    Try
    zipFile.RunMethod("extractAll", Array(outDir))
    Catch
    Log("ZipERR:" & outDir & " - " & zipFilename)
    End Try
    Else
    Log("Dir-ERR: " & outDir & " - " & zipFilename)
    End If
    End Sub
     
  3. xulihang

    xulihang Active Member Licensed User

    Updated with unzip method and Async methods using the Threading Library
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice