B4J Question [ Solved] Detect Corrupt .zip File

Harris

Expert
Licensed User
Longtime User
Using the Archive lib for zip files, AsyncUnZip attempts to unzip but when corrupt, it seems to keep a handle on it, so I cant delete it (programmatically).
This stalls my process to unzip many files - since this corrupt one from the folder in which it is located still exists....

How can one test if the zip file is valid prior to AsyncUnZip getting at it?
The Archive lib does not seem to have this test....

Thanks
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I guess the only person in the forum who could help you is informatix, but he is not so active now days. Your best bet would be to test his other libraries libraries including archiveplus
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
Longtime User
the archive is corrupted? or 1 or more entries in the archive are corrupted? i'm sorry, but
i found your description confusing. in either case, the nature of the archive's "structure"
is such that you have to (try to) uncompress each entry in the archive to know if something is
wrong.

in other words, if the archive itself is bad - what that means is unclear - it might still be
possible to step through it to salvage some or all of the entries. for that you would need
a routine. there is nothing in a zip archive that says "i'm good to go". you have to tell
the archiver to unzip it. if it can't, then you need some handwritten routine to see what
can be done. but a "corrupted archive" could mean anything. realistically, the only way
to know if the archive might be bad is to have a crc available when you acquired it. you
wouldn't have to uncompress all the entries, but you would have to scan all the bytes to
check the crc.

if an entry is corrupted, and assuming the archiver "keep(s) a handle on it", the easiest
solution is to unzip the archive and to rezip the entries that were actually created successfully.
(this may seem primitive, but it's actually how some archivers handle deletions, given how
the archive is structured in the first place.) in the case of a bad entry, it effectively removes
itself since the archiver can't do anything with it. you simply rezip the files which were
handled successfully. this, you could handle programmatically. otherwise, the only way
to know something is wrong is to tell the archiver to do what it's meant to do: unzip itself.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Thanks for the tip @EnriqueGonzalez

Using ProBundle ArchiverPlusZip lib, it has the method I was looking for: IsValidZipFile!

As you can see below, I put 3 good files and 3 corrupt files to test... Works as expected - brilliant!
Corrupt file is available for deletion!

Now to finish and actually unzip the file and place contents in appropriate directory... Easy...

Stay tuned....

B4X:
            If arc.IsValidZipFile( ecmFolder1&"/"& str) Then
                Log("   --- "&str&" is VALID zip")
                delzip(str)
            Else
                Log("   --- "& str&" is NOT VALID zip")
                delzip(str)    
            End If




' Results:
2021-10-30 - 10:38:02 how many new ecmfiles to process: 6
 Folder to create: 72205  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1468146508464_72205.zip is VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468146508464_72205.zip
2021-10-30 - 10:38:03 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468146508464_72205.zip
2021-10-30 - 10:38:13 how many new ecmfiles to process: 5
 Folder to create: 72197  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1468195386877_72197.zip is VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468195386877_72197.zip
2021-10-30 - 10:38:13 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468195386877_72197.zip
2021-10-30 - 10:38:23 how many new ecmfiles to process: 4
 Folder to create: 72205  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1468208415986_72205.zip is VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468208415986_72205.zip
2021-10-30 - 10:38:23 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1468208415986_72205.zip
2021-10-30 - 10:38:33 how many new ecmfiles to process: 3
 Folder to create: 72201  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1633825796558_72201.zip is NOT VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1633825796558_72201.zip
2021-10-30 - 10:38:33 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1633825796558_72201.zip
2021-10-30 - 10:38:43 how many new ecmfiles to process: 2
 Folder to create: 72201  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1633825796622_72201.zip is NOT VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1633825796622_72201.zip
2021-10-30 - 10:38:43 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1633825796622_72201.zip
2021/10/30 - 10:38:53 how many new ecmfiles to process: 1
 Folder to create: 72201  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1634085418341_72201.zip is NOT VALID zip
 File to delete: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1634085418341_72201.zip
2021/10/30 - 10:38:53 File WAS Deleted!!!: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm/1634085418341_72201.zip
B4X:
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Unfortunately, I could not use ArchiverPlusZip - UnZip... it generated errors looking for DoEvents!!!!!

No problem, used the original Archiver to unzip along with ArchiverPlusZip to check if zip is valid...

All is good.
Again, anything can be accomplished with B4X!

Thanks all!


B4X:
            If arc.IsValidZipFile( ecmFolder1&"/"& str) Then
                Log("   --- "&str&" is VALID zip")
                ' could not use ArchiverPlusZip UnZip...  generated error looking for DoEvents!!!!!
                'arc.UnZip(File.Combine( ecmFolder1,str), File.Combine(  ecmFolder,newfld), "Archiver" )
                arc1.AsyncUnZip(ecmFolder1, str, ecmFolder&"/"&newfld, "unzip")

            Else
                Log("   --- "& str&" is NOT VALID zip")
                delzip(str)   
            End If



The error log when using ArchiverPlusZip - UnZip method
Error log:
2021-10-30 - 12:10:42 how many new ecmfiles to process: 6
 Folder to create: 72205  file got from: C:\ANYWHE~1\B4J_61\ABM110\ABMTI\Objects/www/tripinspect/inecm
   --- 1468146508464_72205.zip is VALID zip
main._appstart (java line: 279)
java.lang.RuntimeException: java.lang.NoSuchMethodError: anywheresoftware.b4a.keywords.Common.DoEvents()V
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:120)
    at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:153)
    at abmtripinspect.ti.com.main._appstart(main.java:279)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at abmtripinspect.ti.com.main.main(main.java:28)
Caused by: java.lang.NoSuchMethodError: anywheresoftware.b4a.keywords.Common.DoEvents()V
    at b4a.flm.archiverplus.ArchiverPlusZip.a(SourceFile:208)
    at b4a.flm.archiverplus.ArchiverPlusZip.UnZip(SourceFile:321)
    at abmtripinspect.ti.com.main._placeindir(main.java:463)
    at abmtripinspect.ti.com.main._prtim_tick(main.java:601)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    ... 12 more
 
Upvote 0
Top