8i | 9i | 10g | 11g | 12c | 13c | 18c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » 10g » Here

UTL_COMPRESS : Compress and Uncompress Data from PL/SQL

The UTL_COMPRESS package provides an API to allow compression and decompression of binary data (RAW, BLOB and BFILE). It uses the Lempel-Ziv compression algorithm which is equivalent to functionality of the gzip utility. A simple example of it's use is shown below.

SET SERVEROUTPUT ON
DECLARE
  l_original_blob      BLOB;
  l_compressed_blob    BLOB;
  l_uncompressed_blob  BLOB;
BEGIN
  -- Initialize both BLOBs to something.
  l_original_blob     := TO_BLOB(UTL_RAW.CAST_TO_RAW('1234567890123456789012345678901234567890'));
  l_compressed_blob   := TO_BLOB('1');
  l_uncompressed_blob := TO_BLOB('1');
  
  -- Compress the data.
  UTL_COMPRESS.lz_compress (src => l_original_blob,
                            dst => l_compressed_blob);
  
  -- Uncompress the data.
  UTL_COMPRESS.lz_uncompress (src => l_compressed_blob,
                              dst => l_uncompressed_blob);
  
  -- Display lengths.
  DBMS_OUTPUT.put_line('Original Length    : ' || LENGTH(l_original_blob));
  DBMS_OUTPUT.put_line('Compressed Length  : ' || LENGTH(l_compressed_blob));
  DBMS_OUTPUT.put_line('Uncompressed Length: ' || LENGTH(l_uncompressed_blob));
  
  -- Free temporary BLOBs.             
  DBMS_LOB.FREETEMPORARY(l_original_blob);
  DBMS_LOB.FREETEMPORARY(l_compressed_blob);
  DBMS_LOB.FREETEMPORARY(l_uncompressed_blob);
END;
/
Original Length    : 40
Compressed Length  : 33
Uncompressed Length: 40

PL/SQL procedure successfully completed.

SQL>

The results of the compression will vary depending on the size and type of the data being compressed.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.