Compress and Decompress an Internal table without loss of data

By Sharat Chandra Panuganti, YASH Technologies

Purpose:

To compress and decompress an internal table which is required to be exported (imported) to (from) a Memory Id.  

Functionality:

In most of the instances we come across a situation where there’s a requirement that an internal table or a text or Ranges is to be exported to a memory id and then later on imported from that memory id, inside the same or a different program.

Keeping in view the cost for memory utilization, here is a tip in order to lessen the memory burden by compressing before exporting and decompressing after importing from the memory id without loss of intelligent and important data in the SAP programs. 

*&---------------------------------------------------------------------
*& Program name: ZCOMPRESS_DECOMPRESS
*& Author      : Sharath Chandra Panuganti
*& Created On: 
04/05/2009
*& Description: Compressing and decompressing a internal table
*&---------------------------------------------------------------------

REPORT zcompress_decompress.
DATA :
      t_spfli TYPE TABLE OF spfli,
  t_out TYPE TABLE OF spfli,
  fs_spfli LIKE LINE OF t_out,
  lw_xml_in   TYPE string,
  lw_xml_out  TYPE xstring.

START-OF-SELECTION.
  SELECT carrid
         connid
   UP TO 10 ROWS
    INTO TABLE t_spfli
    FROM spfli .
  WRITE'Before COMPRESS'.
  LOOP AT t_spfli INTO fs_spfli.
    WRITE:  / fs_spfli-carrid, fs_spfli-connid.
  ENDLOOP.

  CALL TRANSFORMATION id
       SOURCE output = t_spfli
       RESULT XML lw_xml_in.  " XML Format
  IF sy-subrc NE 0.
    RAISE compress_error.
  ENDIF.                               " IF 
SY-SUBRC   NE  0
  CALL METHOD cl_abap_gzip=>compress_text
    
EXPO RTING
      text_in        = lw_xml_in
*    text_in_len    = -1
*    compress_level = 6
*    conversion     = 'DEFAULT'
  IMPORTING
    gzip_out       = lw_xml_out  " Compressed Output
*    gzip_out_len   =
      .
  IF sy-subrc NE 0.
    RAISE compress_error.
  ENDIF.                               " IF 
SY-SUBRC   NE  0
*** Decompressing***
  CALL METHOD cl_abap_gzip=>decompress_text
    EXPORTING
      gzip_in      = lw_xml_out
*    gzip_in_len  = -1
*    conversion   = 'DEFAULT'
    IMPORTING
      text_out     = lw_xml_in
*    text_out_len =
  CALL TRANSFORMATION id
   SOURCE XML lw_xml_in
   RESULT output = t_out.
  IF sy-subrc NE 0.
    RAISE compress_error.
  ENDIF.
  ULINE.
  WRITE'After successful COMPRESS and DE-COMPRESS of Internal table',
  / 'No data has been lost during the process'.
  LOOP AT t_out INTO fs_spfli.
    WRITE:  / fs_spfli-carrid, fs_spfli-connid.
  ENDLOOP.
 

Output of the above Program  

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image