title: Better Package for File Compression using PL/SQL?
body: <p>I have an xlsx file <code>sample.xlsx</code> stored in a remote directory with around 1,699 KB in Size.
  I have tried two popular PL/SQL packages (<a href="https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_compr.htm#i1001997" rel="nofollow noreferrer">UTL_COMPRESS</a> and <a href="https://technology.amis.nl/wp-content/uploads/2010/06/as_zip7.txt" rel="nofollow noreferrer">AS_ZIP</a>) that compresses them into <code>gzip</code> and <code>zip</code>, rescpectively.</p>
  
  <p>With the code below using <code>AS_ZIP</code>, I have compressed the file to 1,619 KB:</p>
  
  <pre><code>declare
    g_zipped_blob blob;
    l_file_name   varchar2(100) := 'sample.xlsx';
    l_directory   varchar2(100) := 'EXT_TAB_DATA';
  begin
    as_zip.add1file( g_zipped_blob, l_file_name, as_zip.file2blob(l_directory, l_file_name));
    as_zip.finish_zip( g_zipped_blob );
    as_zip.save_zip( g_zipped_blob, l_directory, 'my2.zip' );
    dbms_lob.freetemporary( g_zipped_blob );
  end;
  </code></pre>
  
  <p>With the code below (taken from the original <a href="https://stackoverflow.com/questions/26772666/how-to-use-utl-compress-package-to-compress-the-txt-file-and-create-gz-file-in-u">post</a>) using <code>UTL_COMPRESS</code>, I have compressed the file to 1,618 KB:</p>
  
  <pre><code>DECLARE
     in_filename VARCHAR2(100) := 'sample.xlsx';
     l_directory   varchar2(100) := 'EXT_TAB_DATA';
     src_file   BFILE;
     v_content  BLOB;
     v_blob_len INTEGER;
     v_file     utl_file.file_type;
     v_buffer   RAW(32767);
     v_amount   BINARY_INTEGER := 32767;
     v_pos      INTEGER := 1;
  BEGIN
     src_file := bfilename(l_directory, in_filename);
     dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
     v_content  := utl_compress.lz_compress(src_file, 9);
     v_blob_len := dbms_lob.getlength(v_content);
     v_file     := utl_file.fopen(l_directory,
                                  in_filename || '.gz',
                                  'wb');
     WHILE v_pos &lt; v_blob_len LOOP
        dbms_lob.READ(v_content, v_amount, v_pos, v_buffer);
        utl_file.put_raw(v_file, v_buffer, TRUE);
        v_pos := v_pos + v_amount;
     END LOOP;
     utl_file.fclose(v_file);
  
  EXCEPTION
     WHEN OTHERS THEN
        IF utl_file.is_open(v_file) THEN
           utl_file.fclose(v_file);
        END IF;
        RAISE;
  END;
  </code></pre>
  
  <p>Although minimal, it seems that <code>UTL_COMPRESS</code> has better compression in terms of file size.
  I was wondering if there was some unseen advantage using the custom <code>AS_ZIP</code> over the Oracle-supplied <code>UTL_COMPRESS</code>?</p>
  
  <p>Thank you.</p>