Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Decompressing file created with UTL_COMPRESS

orclrunnerJul 30 2015 — edited Aug 3 2015

Can we not use the UNIX tool "gzip" to decompress a "gz" file created with UTL_COMPRESS.LZ_COMPRESS?

gzip -d 2015-07-30_ccr_records_arc_purged.csv.gz

gzip: 2015-07-30_ccr_records_arc_purged.csv.gz: not in gzip format

Do we have to use UTL_COMPRESS.LZ_UNCOMPRESS? That wouldn't make sense. We should be able to use gzip or gunzip.

Here is the procedure to compress "csv" files:

PROCEDURE compress_files

IS

  dir_path          VARCHAR2(1024)          ;

  src_file          BFILE                   ;

  l_content         BLOB                    ;

  l_blob_len        INTEGER                 ;

  l_file            utl_file.file_type      ;

  l_buffer          RAW(32767)              ;

  l_amount          BINARY_INTEGER := 32767 ;

  l_pos             INTEGER := 1            ;

  l_compress_rate   INTEGER := 6            ;

  l_csv             VARCHAR2(3) := 'csv'    ;

BEGIN

  get_dir_info(dir_path) ;

   -- retrieve all *.csv files using DBMS_BACKUP_RESTORE.SEARCHFILES in ListDir pipe-lined function

   FOR file IN ( SELECT column_value FROM TABLE(ListDir(dir_path,l_csv)) )

   LOOP

       src_file := BFILENAME(cDIRNAME, file.column_value);

       dbms_lob.FILEOPEN(src_file, dbms_lob.file_readonly);

       l_content  := utl_compress.LZ_COMPRESS(src_file, l_compress_rate);

       l_blob_len := dbms_lob.GETLENGTH(l_content);

       l_file     := utl_file.FOPEN(cDIRNAME, file.column_value || '.gz','wb');

     

       WHILE ( l_pos < l_blob_len  )

       LOOP

           dbms_lob.READ(l_content, l_amount, l_pos, l_buffer);

           utl_file.PUT_RAW(l_file, l_buffer, TRUE);

           l_pos := l_pos + l_amount;

       END LOOP ;

     

       dbms_lob.FILECLOSE(src_file) ;

       utl_file.FCLOSE(l_file);

   END LOOP ;

  

   EXCEPTION

       WHEN   others

       THEN

           IF utl_file.is_open(l_file)

           THEN

              utl_file.fclose(l_file);

           END IF;

             

           RAISE;

END compress_files ;

This post has been answered by orclrunner on Aug 3 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 31 2015
Added on Jul 30 2015
3 comments
1,295 views