Skip to Content

Introduction 

As of ABAP kernel 620 patch 841 gzip functionality exists inside the SAP system. From rel. NetWeaver 04 we have the possibility to create complete zip files using the CL_ABAP_ZIP class what is utilizing the gzip methods provided by the kernel. This provides us the possibility to get rid of RFC calls made to external programs to create zip files. Another thing that will make you happy: the usage is more simple compared to the zipping functionality provided by the IGS /Internet Graphics Server/.

Let’s see now how we can switch:

The zipping functionality is realized by the class: CL_ABAP_ZIP

Step 1. use a reference to CL_ABAP_ZIP instead of CL_IGS_ZIPPER.

 

The class CL_ABAP_ZIP has two simple methods that will allow you to create a zip file. These are ADD and SAVE.

Step 2. use method cl_abap_zip->add instead of  cl_igs_zipper->add_file to add a file to your compressed object. There are two differences you have to consider:

  • the new method requires the content in xstring fromat and not in an internal table. If your data is in an internal table you will have to convert it into xstring.
  • cl_abap_zip->add expects the path and filename concatenated into one string. (Name)

 

Step 3. method: cl_abap_zip->save will replace both cl_igs_zipper->execute and cl_igs_zipper->get_zipfile methods. The result is the zipped object you want. This is also xstring format and not a date table. According your needs you can convert this stream to data table.

A working sample

For those who need a working zipping sample: here is a simple report that loads a file from your computer packs it into a zip and sends it back to you to save it on your disk. I tried to keep this sample as simple as possible, for easy understanding. / So e.g. no error handling /. This is not intended for real life usage.

 


"Zipper test
REPORT  Z_PAP_UP_ZIP_DL.
DATA: L_ZIPPER TYPE REF TO cl_abap_zip.
DATA: FILEX type XSTRING.
DATA: FILENAME type string.
DATA: PATH type string.
DATA: zip type xstring.
DATA: FILE_N_TAB type FILETABLE.
DATA: FULL_PATH type string.
DATA: FILE_LENGTH type i.
DATA: FILE_TAB type w3mimetabtype.
DATA: WA_INT type int4.
DATA: PATH_TABLE TYPE TABLE of char1024.
"-----------------------------------------------------------------------------
"    Load the file
"-----------------------------------------------------------------------------
"Which file to upload
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
  CHANGING
    FILE_TABLE              = FILE_N_TAB
    RC                      = WA_INT
        .
"load the (first) file from the frontend the user has selected.
LOOP at FILE_N_TAB into FULL_PATH.
 "get the file
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
     EXPORTING
       FILENAME                = FULL_PATH
       FILETYPE                = 'BIN'
    IMPORTING
      FILELENGTH              = FILE_LENGTH
    CHANGING
      DATA_TAB                = file_tab.
    exit.
endloop.
"create xstring from table
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
  EXPORTING
    INPUT_LENGTH       = FILE_LENGTH
  IMPORTING
    BUFFER             = FILEX
  TABLES
    BINARY_TAB         = file_tab
   .
"get the name of the file. we take entry after the last '\' ...windows.
SPLIT FULL_PATH AT '\' INTO TABLE PATH_TABLE.
DESCRIBE TABLE PATH_TABLE LINES WA_INT.
READ TABLE PATH_TABLE INTO FILENAME INDEX WA_INT.

"-------------------------------------------------------------------------------
"   ZIP the file
"-------------------------------------------------------------------------------
"create our zipper object
CREATE OBJECT L_ZIPPER.
"add file to zip
CALL METHOD L_ZIPPER->ADD
  EXPORTING
    NAME    = filename
    CONTENT = filex
   .
"save zip
 CALL METHOD L_ZIPPER->SAVE
   RECEIVING
     ZIP    = zip
     .

"-------------------------------------------------------------------------------
"    Save the file
"-------------------------------------------------------------------------------
"convert to table
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    BUFFER                = zip
  IMPORTING
    OUTPUT_LENGTH         = file_length
  TABLES
    BINARY_TAB            = file_tab
          .
"File save dialog
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG

  CHANGING
    FILENAME             = filename
    PATH                 = path
    FULLPATH             = full_path
        .
"Save the file
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      BIN_FILESIZE              = file_length
      FILENAME                  = full_path
      FILETYPE                  = 'BIN'
    CHANGING
      DATA_TAB                  = file_tab
      .

 

And what else: Good luck 🙂

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply