27 APEX_ZIP

This package manages the zipping and unzipping of files.

Data Types

The data types used by the APEX_ZIP package are described in this section.

t_files

type t_files is table of varchar2(32767) index by binary_integer;

ADD_FILE Procedure

This procedure adds a single file to a zip file.

Note:

After all files are added, you must call APEX_ZIP.FINISH.

Syntax

APEX_ZIP.ADD_FILE (
    p_zipped_blob IN OUT NOCOPY BLOB,
    p_file_name   IN VARCHAR2,
    p_content     IN BLOB );

Parameters

Table 27-1 ADD_FILE Procedure Parameters

Parameter Description

p_zipped_blob

BLOB containing the zip file.

p_file_name

File name, including path, of the file to be added to the zip file.

p_content

BLOB containing the file.


Example

This example reads multiple files from a table and puts them into a single zip file.

declare
    l_zip_file blob;
begin
    for l_file in ( select file_name,
                            file_content
                       from my_files )
    loop
        apex_zip.add_file (
            p_zipped_blob => l_zip_file,
            p_file_name   => l_file.file_name,
            p_content     => l_file.file_content );
    end loop;

    apex_zip.finish (
        p_zipped_blob => l_zip_file );

end;

FINISH Procedure

This procedure completes the creation of a zip file after adding files with APEX_ZIP.ADD_FILE.

Syntax

APEX_ZIP.FINISH (
    p_zipped_blob IN OUT NOCOPY BLOB );

Parameters

Table 27-2 FINISH Procedure Parameters

Parameter Description

p_zipped_blob

BLOB containing the zip file.


Example

See "ADD_FILE Procedure" for an example.

GET_FILE_CONTENT Function

This function returns the BLOB of a file contained in a provided zip file.

Syntax

APEX_ZIP.GET_FILE_CONTENT (
    p_zipped_blob IN BLOB,
    p_file_name   IN VARCHAR2,
    p_encoding    IN VARCHAR2 DEFAULT NULL )
RETURN BLOB;

Parameters

Table 27-3 GET_FILE_CONTENT Function Parameters

Parameter Description

p_zipped_blob

This is the BLOB containing the zip file.

p_file_name

File name, including path, of a file located in the zip file.

p_encoding

Encoding used to zip the file.


Returns

Table 27-4 GET_FILE_CONTENT Function Returns

Return Description

BLOB

BLOB containing the zip file.


Example

See "GET_FILES Function" for an example.

GET_FILES Function

This function returns an array of file names, including the path, of a provided zip file that contains a BLOB.

Syntax

APEX_ZIP.GET_FILES (
    p_zipped_blob IN BLOB,
    p_only_files  IN BOOLEAN DEFAULT TRUE,
    p_encoding    IN VARCHAR2 DEFAULT NULL )
RETURN t_files;

Parameters

Table 27-5 GET_FILES Function Parameters

Parameter Description

p_zipped_blob

This is the zip file containing the BLOB.

p_only_files

If set to TRUE, empty directory entries are not included in the returned array. Otherwise, set to FALSE to include empty directory entries.

p_encoding

This is the encoding used to zip the file.


Returns

Table 27-6 GET_FILES Function Returns

Return Description

t_files

A table of file names and path. See "Data Types" for more details.


Example

This example demonstrates reading a zip file from a table, extracting it and storing all files of the zip file into my_files.

declare
    l_zip_file      blob;
    l_unzipped_file blob;
    l_files         apex_zip.t_files;
begin
    select file_content
        into l_zip_file
        from my_zip_files
    where file_name = 'my_file.zip';

    l_files := apex_zip.get_files (
            p_zipped_blob => l_zip_file );

    for i in 1 .. l_files.count loop
        l_unzipped_file := apex_zip.get_file_content (
            p_zipped_blob => l_zip_file,
            p_file_name   => l_files(i) );

        insert into my_files ( file_name, file_content )
        values ( l_files(i), l_unzipped_file );
    end loop;
end;