B4A Library [Lib] Archiver

By courtesy of Djembefola, this library is released as freeware for the benefit of the community. You should send him a big "thank you".

This library provides a basic support for Zip, Tar and Gzip archives (including TarGz). It can compress/uncompress synchronously or asynchronously (one task in the background at a time). This lib doesn't support encryption (but you can use it with encrypted files).

Its use is fairly simple. Examples:
B4X:
Dim Arc As Archiver

Arc.Gzip(File.DirRootExternal, "test2.tar", File.DirRootExternal)
Arc.UnGzip(File.DirRootExternal, "test2.tar.gz", File.DirRootExternal & "/testTARgz2")
Arc.AsyncGzip(File.DirRootExternal & "/media/image", "test.jpg", File.DirRootExternal, "Arc")

Dim m As Map
Try
   m = Arc.ListZipEntries(File.DirRootExternal & "/media/image/Icones", "android-icons.zip")
   For i = 0 To m.Size - 1
      Dim Info(3) As Long
      Info = m.GetValueAt(i)
      Log(m.GetKeyAt(i))
      Log("   Uncompressed size = " & Info(0))
      Log("   Compressed size = " & Info(1))
      Log("   CRC = " & Info(2))
   Next
Catch
   Log(LastException.Message)
End Try

This library is provided as-is and will not be extended to other formats. It uses the JTar library.

Don't forget to credit the authors if you include it in your application:
Frédéric Leneuf-Magaud & Kamran Zafar

v1.11:
I improved the synchronization of asynchronous operations (Async orders can be enqueued now);
The event declarations appear now with Space+Tab in the IDE.*

There's an improved version of this library for Zip files in my ProBundle that supports encryption and decryption.
 

Attachments

  • Archiver v1.11.zip
    26.4 KB · Views: 3,783
Last edited:

alienhunter

Active Member
Licensed User
Longtime User
Hi ,
thanks for the lib
Question
can update a archive with a new file without unzipping ?
 

padvou

Active Member
Licensed User
Longtime User
Excellent work, as usual!
Does it always overwrite while unzipping?
 

padvou

Active Member
Licensed User
Longtime User
Is there a way for UnZipDone to show the filename that was unzipped successfully?
 

padvou

Active Member
Licensed User
Longtime User
Yeap, but this returns the filenames unzipped, not the name of the zip file.
 

padvou

Active Member
Licensed User
Longtime User
True, but if I have my app download a list of files and then asynchronously unzip them, how would I know which file failed to unzip?
 

Informatix

Expert
Licensed User
Longtime User
True, but if I have my app download a list of files and then asynchronously unzip them, how would I know which file failed to unzip?
The first UnzipDone matches the first AsyncUnZip called. The second UnzipDone matches the second AsyncUnzip called, and so on. Remove from your list the first item for each UnzipDone and you'll get the current unzipped file at position 0.
 

padvou

Active Member
Licensed User
Longtime User
Here's another question:
B4X:
Dim fileList As List
  Dim N As Int

Dim path1 As String = File.DirRootExternal & "/mydir/"
  fileList = File.ListFiles(path1)
  fileList.Sort(True)

  For N = 0 To fileList.Size-1
    Dim file1 As String = fileList.Get(N)
    If file1.EndsWith(".zip") Then
        Dim Arc As Archiver
        Try
        Dim catchval As Int=0
        Arc.ListZipEntries(File.DirRootExternal & "/mydir/", file1)
        Catch
        catchval=1
        End Try
        If catchval=0 Then
        Arc.AsyncUnZip(File.DirRootExternal & "/mydir/", file1, File.DirRootExternal,"Updatez")
     
        End If
    End If
  Next
It seems to be working fine (suggestions welcome, of course), but what is wiser to use AsyncUnzip or Unzip?
If I 'd prefer to have files unzipped one after the other, should unzip used and not asyncunzip?
 

padvou

Active Member
Licensed User
Longtime User
I have a problem when obfuscating my code. Subs which are called during the progression and the done phases of unzipping are not called because of the underscore in their names... Any ideas please?

Example of code:

Sub Updatez_UnZipDone(CompletedWithoutError As Boolean, NbOfFiles As Int)
Try

CSE.RunOnMainThread("ExitLog",Null,False)

Catch
Log(LastException.Message)
End Try

End Sub

Sub ExitLog
'Do stuff here on main thread..
End Sub
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
It seems to be working fine (suggestions welcome, of course), but what is wiser to use AsyncUnzip or Unzip?
If I 'd prefer to have files unzipped one after the other, should unzip used and not asyncunzip?

"Dim Arc" should be outside the loop. "Dim CatchVal" should be outside the Try. And probably is not needed: you could put Arc.AsyncUnZip just after Arc.ListZipEntries (if ListZipEntries fails, AsyncUnZip won't be called).
And file1 should be converted to lower case to ensure that ".ZIP" files are not missed.

The choice between UnZip and AsyncUnZip depends on what you want to do. One blocks the program flow, the other let it continue.
 

Informatix

Expert
Licensed User
Longtime User
I have a problem when obfuscating my code. Subs which are called during the progression and the done phases of unzipping are not called because of the underscore in their names... Any ideas please?

It's the contrary. To make your code compatible with the obfuscation mode, your sub names must have an underscore.
And I can't see why you can't do that with this library.
 

padvou

Active Member
Licensed User
Longtime User
"Dim Arc" should be outside the loop. "Dim CatchVal" should be outside the Try. And probably is not needed: you could put Arc.AsyncUnZip just after Arc.ListZipEntries (if ListZipEntries fails, AsyncUnZip won't be called).
And file1 should be converted to lower case to ensure that ".ZIP" files are not missed.

The choice between UnZip and AsyncUnZip depends on what you want to do. One blocks the program flow, the other let it continue.
So AsyncUnzip runs on a seperate thread?
Maybe that's why I needed CSE to call stuff on the main thread.
 

padvou

Active Member
Licensed User
Longtime User
From the description of the function:
Uncompresses the given zip archive (with a thread running in the background).

"Async" in the name means "asynchronously".

Thank you. I must be getting too old and too tired... :)
 
Top