# How to Zip and download files in Oracle APEX?

## Introduction

In this tutorial, we will be focusing on how to use the apex\_zip utility package to zip files and enable them to be downloaded with just a single button click from within an Oracle APEX page.

## Background

In the employee self-service portal, employees frequently request reimbursements for various expenses such as travel allowances, shift allowances, phone reimbursements, and more. To process these requests, employees are required to upload digital copies of their supporting documents to the portal. At the end of each month, the HR admin is responsible for downloading these requests and submitting them to the accounts team. However, this process can be quite time-consuming, especially when it comes to downloading multiple attachments. To simplify this process, we've created a solution that allows users to download all attachments for all requests submitted by employees during a specific period as a single zip file. To learn more about how this solution works, please watch the video provided below.

%[https://youtu.be/sJkwweDD9kU] 

## Steps

To create a functionality to download attachments as a zip file, follow these steps:

1. Create an application process named DOWNLOAD\_ATTACHMENTS in the Shared Components or at the page level in the Ajax Callback section.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681044982946/88f0f3e7-93cc-48e4-8060-abc7f2a4eff4.png align="center")
    
    ```sql
    DECLARE
       l_download_file_name  VARCHAR2(64) := 'reimbursement-attachments.zip';
       l_zip_file        BLOB;
       l_disposition     VARCHAR2(30) := 'attachment';
    BEGIN
       FOR i IN (
          SELECT file_name,
                 file_content
          FROM my_files
       ) LOOP
         -- Add files to the zip 
        apex_zip.add_file(
                           p_zipped_blob => l_zip_file
                         , p_file_name => i.file_name
                         , p_content => i.file_content
          );
       END LOOP;
    
       -- Finish zipping
       apex_zip.finish(p_zipped_blob => l_zip_file);
       -- Download zip file
       sys.htp.init;
       sys.owa_util.mime_header(
                               'application/zip'
                             , false
       );
       sys.htp.p('Content-length: '
                 || sys.dbms_lob.getlength(l_zip_file));
    
       sys.htp.p('Content-Disposition: attachment; filename="'
                 || l_download_file_name
                 || '"');
       sys.owa_util.http_header_close;
       sys.wpg_docload.download_file(l_zip_file);
       apex_application.stop_apex_engine;
    END;
    ```
    
2. Create a button and set the behavior to "Submit Page".
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681045028512/98c26503-53c7-4ebe-9c10-49dfadeea670.png align="center")
    
3. Under the processing tab, create a branch after the submit (or after processing) and set the behavior to:
    
    * Type: Page or URL (Redirect)
        
    * Target: Same page
        
    * Advanced → Request: APPLICATION\_PROCESS=DOWNLOAD\_ATTACHMENTS
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681045060054/9d5239ef-48c2-4e20-86ec-5e475b23a9a6.png align="center")
        
4. Save and run the application.
    

## References

[https://docs.oracle.com/en/database/oracle/apex/22.2/aeapi/APEX\_ZIP.html#GUID-270BFF3A-5FB1-4089-894E-978608F9BD87](https://docs.oracle.com/en/database/oracle/apex/22.2/aeapi/APEX_ZIP.html#GUID-270BFF3A-5FB1-4089-894E-978608F9BD87)
