Android Question How to zip a folder and saveas?

Scantech

Well-Known Member
Licensed User
Longtime User
Im using ArchiverPlus. It zips but can't save it for some strange reason. I checked the zip bytes = 3603. result = 0. File saved does not get logged. it saves in the download section i choose with 0 bytes??

B4X:
    Dim Arc As ArchiverPlusZip
    Arc.ZipCompression = True
    Arc.ZipExecutionMode = Arc.ZIP_EXECMODE_ASYNCHRONOUS
    Arc.AddFolderToZip(File.DirInternal & "/TempBackup/", File.DirInternal & "/Test.zip", "ZIP")

    Wait For ZIP_ZipResult(Result As Int, ErrorMsg As String)

    Log(Result)

    If File.Exists(File.DirInternal, "/Test.zip") Then
        Log(File.Size(File.DirInternal, "/Test.zip"))
        Log("its there!!!!!!")
    End If
   
   
    Wait For (SaveAs(File.OpenInput(File.DirInternal, "/Test.zip"), "application/zip", "test.zip")) Complete (Success As Boolean)
    Log("File saved successfully? " & Success)

B4X:
Sub SaveAs (Source As InputStream, MimeType As String, Title As String) As ResumableSub
    Dim intent As Intent
    intent.Initialize("android.intent.action.CREATE_DOCUMENT", "")
    intent.AddCategory("android.intent.category.OPENABLE")
    intent.PutExtra("android.intent.extra.TITLE", Title)
    intent.SetType(MimeType)
    StartActivityForResult(intent)
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim result As Intent = Args(1)
        Dim jo As JavaObject = result
        Dim ctxt As JavaObject
        Dim ContentResolver As JavaObject = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null)
        Dim out As OutputStream = ContentResolver.RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null), "wt")) 'wt = Write+Trim
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    Dim ion As String
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Not sure that I understood where is the problem. Is the zip created properly? Where do you get 0 bytes?
2. If I'm not mistaken ArchiverPlus raises the event on a background thread and this causes all kinds of problems. Worth trying with a different library such as: https://www.b4x.com/android/forum/threads/sd-ziplibrary.90733/
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
It zips fine and saved to file.internal with no problems. The problem is with

B4X:
 Wait For (SaveAs(File.OpenInput(File.DirInternal, "/Test.zip"), "application/zip", "test.zip")) Complete (Success As Boolean)

In addition, i want to save it in download section, too. It saves it as 0 bytes and the message file saved successfully? is not logged.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…