How to vaildate a zip file, before post to API

Rafeda Malki

New Member
Hi,

I need some tips about how I can validate zip file before I will post it to API.

The API integration must select a zip file, that contains several files with different file extension.

The files need to be "*.cpg *.dbf *.prj *.shp *.shx".



Thanks for all help.



/Rafeda
 
Hello

You will need to use unzip.exe from infozip (Info-ZIP Home Page), then some code on the lines of the following:

Code:
DEFINE VARIABLE cTextLine AS CHARACTER   NO-UNDO.
input through unzip.exe -t test.zip.
REPEAT:
    import unformatted cTextLine.
    <here you test the contents of cTextLine to check that the file is what you are waiting for>
END.
input close.

to test you can first MESSAGE cTextLine VIEW-AS ALERT-BOX INFORMATION BUTTONS OK. juste after the import statement, you'll see that each line of unzip.exe statement goes one by one into the variable.

You will have lines like:
testing: test/test.cpg OK
which allow you to check the extensions you are looking for,

and at the end, it could be good to check that the zip is not corrupted, the last line being:
No errors detected in compressed data of test.zip.

++
JC
 

Rafeda Malki

New Member
Hi Jean-Christophe, thanks for you tips,
I try to test it, and download unzip, and add to the path, but still get
(unzip.exe is not recognized as an internal or external command)
 

Cecil

19+ years progress programming and still learning.
Another possible alternative would be to use ABL .NET

This code will open a ZIP and loop through each file entry within ZIP file.
Please note that is does require the use of the assemblies.xml file update to include System.IO.Compression and System.IO.Compression.FileSystem.


Code:
using System.*.
using System.IO.*.
using System.IO.Compression.*.
using System.IO.Compression.FileSystem.*.

block-level on error undo, throw.

define variable zipFilePath as character no-undo.
define variable archive     as class     ZipArchive                                                                                 no-undo.
define variable zipEntries  as class     "System.Collections.ObjectModel.ReadOnlyCollection<System.IO.Compression.ZipArchiveEntry>" no-undo.
define variable zipEntry    as class     ZipArchiveEntry                                                                            no-undo.
define variable iLoop       as integer   no-undo.

zipFilePath = "2024-04-06.zip".

if File:Exists(zipFilePath) then
do:
   
   archive = ZipFile:OpenRead(zipFilePath).
   
    if valid-object(archive) then
        message "ZIP file is valid"
            view-as alert-box information title "ABL ZIP Check".
    else
        message "Error: ZIP file not valid"
            view-as alert-box error title "ABL ZIP Check".
       
    zipEntries = archive:Entries.
   
    do iLoop = 1 to zipEntries:count :
       
        zipEntry = zipEntries:Item[iLoop - 1].
        message zipEntry:name  truncate((zipEntry:CompressedLength / zipEntry:length) * 100,2) "% Compressed".
        pause 0.
    end.
end.
else
    message "Error: ZIP file not found." view-as alert-box error title "ABL ZIP Check".

finally:

    if valid-object(archive) then
        delete object    archive.

    if valid-object(zipEntries) then
        delete object zipEntries.
       
    if valid-object(zipEntry) then
        delete object zipEntry.
   
end finally.
 

Rafeda Malki

New Member
Hi,

Thanks everyone, i got it to work now .

But the issue is, this code validate need to be done for every post resquest, and it in the customer side.
they choose a zip file from a drive and with POST request it send to the API . it will be difficult to install unzip.exe on the all maskin...
and the zip file is diffrent for each time.
 

Cecil

19+ years progress programming and still learning.
will you still be doing server side validation of the zip file?

if the users front end application is web based, you could use javascript libraries to inspect the zip file before posting to the api.
 
ok, so they deliver some .p or .r files; you could deliver also unzip.exe along those files, no need to "install" it, just copying unzip.exe is enough. You put it into the PROPATH, then SEARCH it then INPUT THROUGH VALUE(<result of SEARCH("unzip.exe")>)...
 

Rafeda Malki

New Member
Yes, i can include it with the install of new version for the customer. thanks a lot for all your help
ok, so they deliver some .p or .r files; you could deliver also unzip.exe along those files, no need to "install" it, just copying unzip.exe is enough. You put it into the PROPATH, then SEARCH it then INPUT THROUGH VALUE(<result of SEARCH("unzip.exe")>)...
 
Top