Skip to Content
Technical Articles
Author's profile photo Simone Milesi

Unzip & Read files with ABAP class CL_ABAP_ZIP

On SCN exist a wiki showing how to create a ZIP file (here http://wiki.scn.sap.com/wiki/display/ABAP/Zip+any+file+via+ABAP+using+CL_ABAP_ZIP )  and a wiki showing how to ZIP a report output in PDF (here http://wiki.scn.sap.com/wiki/display/ABAP/CL_ABAP_ZIP+usage+-+Zipping+ABAP+report+output) but I did not found a guide for opening and using a compressed file.

This little document is just a quick example built from the answer around the SCN (like Want to Unzip a file using CL_ABAP_ZIP | SCN and UNZIP file from ABAP)  to show how to use class CL_ABAP_ZIP to read a zip file and show in ALV the content of zipped file but it can easily adapted to manage any other file type (i.e. an image to be displayed).

Objects Used in the Example

Class

Method

Used For

CL_GUI_FRONTED_SERVICES

FILE_OPEN_DIALOG

Chose the input file

CL_GUI_FRONTED_SERVICES

FILE_UPLOAD

Upload the file

CL_ABAP_ZIP

LOAD

Read the zip file in the buffer

CL_ABAP_ZIP

GET

Read compressed file’s content

CL_SALV_TABLE

FACTORY

Create ALV grid

CL_SALV_TABLE

SHOW

Display ALV grid

CL_SALV_TABLE

GET_FUNCTIONS

Retrieve available SALV functions

CL_SALV_FUNCTIONS

SET_ALL

Activate ALV Functions

Functions Used in the Example

Class

Used For

SCMS_BINARY_TO_XSTRING

Convert Binary stream file into XSTRING

SCMS BINARY_TO_STRING

Convert Binary stream into STRING

SCMS XSTRING_TO_BINARY

Convert XSTRING data in Binary

Flow of the report

  1. 1.      Upload ZIP file
  2. 2.      Load ZIP into buffer
  3. 3.      Read packed file
  4. 4.      Show packed file in ALV

Coding for ZTEST_UNZIP

*&---------------------------------------------------------------------*
*& Report  ZTEST_UNZIP
*& AUTHOR: Simone Milesi
*& CREATED ON: 18/02/2016
*&---------------------------------------------------------------------*
*& The report read a zip file from user's PC and unzip it, showing
*& the content into an alv list
*&---------------------------------------------------------------------*

REPORT ztest_unzip.
*&---------------------------------------------------------------------*
*&      Class Application
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
CLASS application DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF ty_file,
name TYPE string,
date TYPE d,
time TYPE t,
size TYPE i,
END OF ty_file.
TYPES: tty_files TYPE TABLE OF ty_file.


CLASS-METHODS: execute,
get_file CHANGING value(c_file) TYPE string.
PRIVATE SECTION.
CLASS-DATA: t_out TYPE filetable,
l_out TYPE file_table,
files TYPE filetable,
t_file TYPE tty_files,
cont TYPE xstring,
file TYPE ty_file.
CLASS-DATA: o_zip TYPE REF TO cl_abap_zip.
CLASS-METHODS: show_alv,
fill_out,
upload_data.
ENDCLASS.              "Application

PARAMETERS: p_file TYPE application=>ty_file-name OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
application=>get_file( CHANGING c_file = p_file ).

START-OF-SELECTION.
application=>execute( ).
*&---------------------------------------------------------------------*
*&      Class (Implementation)  Application
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
CLASS application IMPLEMENTATION.
METHOD execute.
upload_data( ).
LOOP AT o_zip->files INTO file.
CLEAR cont.
o_zip->get( EXPORTING name = file-name
IMPORTING content = cont ).
fill_out( ).
ENDLOOP.
show_alv( ).
ENDMETHOD.                    "execute
METHOD fill_out.
DATA: olen TYPE i,
xtab TYPE TABLE OF x255,
ls TYPE string.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer          = cont
IMPORTING
output_length  = olen
TABLES
binary_tab      = xtab.
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
EXPORTING
input_length = olen
IMPORTING
text_buffer  = ls
TABLES
binary_tab  = xtab
EXCEPTIONS
failed      = 1
OTHERS      = 2.
IF sy-subrc <> 0.
EXIT.
ENDIF.

  DO.
IF ls CA cl_abap_char_utilities=>cr_lf.
SPLIT ls AT cl_abap_char_utilities=>cr_lf INTO l_out
ls.
APPEND l_out TO t_out.
ELSE.
EXIT.
ENDIF.
ENDDO.

l_out = ls.
APPEND l_out TO t_out.
ENDMETHOD.                    "fill_out
METHOD upload_data.
DATA:flen TYPE i,
xhead TYPE xstring,
xtab TYPE TABLE OF x255.
cl_gui_frontend_services=>gui_upload( EXPORTING filename = p_file
filetype = 'BIN'
IMPORTING filelength = flen

CHANGING data_tab = xtab ).
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = flen
IMPORTING
buffer      = xhead
TABLES
binary_tab  = xtab
EXCEPTIONS
failed      = 1
OTHERS      = 2.
IF sy-subrc <> 0 OR xhead IS INITIAL.
IF o_zip IS NOT INITIAL.
FREE o_zip.
ENDIF.
ENDIF.
CREATE OBJECT o_zip.
o_zip->load( xhead ).
ENDMETHOD.                    "upload_data

METHOD show_alv.
DATA: salv TYPE REF TO cl_salv_table,
functions TYPE REF TO cl_salv_functions.
cl_salv_table=>factory( IMPORTING r_salv_table = salv
CHANGING t_table = t_out  ).
functions = salv->get_functions( ).
functions->set_all( abap_true ).
salv->display( ).

ENDMETHOD.                    "show_Alv
METHOD get_file.
DATA: fresult TYPE file_table,
rc TYPE i.
CLEAR files[].
cl_gui_frontend_services=>file_open_dialog(
CHANGING file_table = files
rc        = rc ).
IF files[] IS NOT INITIAL.
CLEAR c_file.
READ TABLE files INTO fresult INDEX 1.
c_file = fresult.

ENDIF.
ENDMETHOD.                    "get_file
ENDCLASS.              "Application

Test Case

Create a TXT file

/wp-content/uploads/2016/02/file_891375.png

And zip it (Attention! Do not use Winzip!)

Run your report

Screen.png

And you got your result

/wp-content/uploads/2016/02/result_891383.png

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Philip Bremner
      Philip Bremner

      Thank you Simone, Nice!

      Author's profile photo Srinivas Sunki
      Srinivas Sunki

      Appreciate if can get code to extract content for .DAT.GZ file

      Author's profile photo Yann SZWEC
      Yann SZWEC

      Excellent. Thanks

       

      Author's profile photo Pavlo Astashonok
      Pavlo Astashonok

      Cool blog and cool example. But why not to use WinZip?

      Author's profile photo Simone Milesi
      Simone Milesi
      Blog Post Author

      Hello Pavel!

      At this blog's time, this was more or less a POC born from an user's requirement: they wanted to upload and change a zip file, adding some content from SA and re-zipping everything.

      On SAP server, i couldn't install anything.

      So, yes, it's more an accademic work, a simple POC 🙂

      Author's profile photo Pavlo Astashonok
      Pavlo Astashonok

      no, I am referring to this passage from the blog:

      And zip it (Attention! Do not use Winzip!)

      is it CL_ABAP_ZIP limitation that it cannot unzip files compressed by WinZip?

      Author's profile photo Simone Milesi
      Simone Milesi
      Blog Post Author

      Mmmm... i do not remeber, to be honest, i'm sorry!
      Maybe at given time there were some limits, but this blog it's 6 years old right now 🙂

      Author's profile photo Hamza Masood
      Hamza Masood

      Hi Simone,

      Great blog. Thank you for this I learnt a lot.

      Is there such an implementation to unzip for nodejs?

      Here is some data that I zipped using CL_ABAP_ZIP:

      UEsDBBQAAAAIAEVySlTcBQjw0QEAAJ8RAAAGAAAAREwudHh01dfZjoIwFAbgd+m1TU6LCHh3cEEH pKSUcZbMkxjffdqCGRANYxRBb2x/mlK+dIEDWaJCMv8+kPdwlaRkTqD8MR/IRIfqM9PhQpexQKl0 +UtIcyUWMtI15jHbUO1XtmoqeVa2BHsljqTpwhbDQpbFuEhTaZuYDsz9dJhs13lsQkdXQlzEidB9 woQooTAhczYhK5nizgxIinSbLu2w9CPohAMHCg7lLjHNLoTJOrLdoy4vdlGuyqFEYa5OcZzrwocu ZHl5Eav/2I77OGk5ufxJTmCZ3FndiXU6pW+4265ykbakZpQHLakq7EfK8Z8q5dWlvE6pqMgUYovJ o1a5yVSFfTDpsTvPZGKNhdc9oUKRb6jMMBSqZRVQ4C2rKuzJyhvOqntKjcuK8cGsXHg1q9lwVvzF rPj0mVZTqFvdvV+1j8Aq7MnqwUcg86DGVdbunV1XDsKA2sGfY9mwHyw/GPMivMzE4MKcOoW9MDn2 0B75nJIoMtyj7v/ci1G71za9qrAXL++/axDu/bCxVC7cJmX3q8UGVWvDYhR8CtMm1l/YE9Y/1+Bj sMoHufGlYTxYzgusxKvn4SBiwYO/dW4Wu/YWzzs/oZnZ1pl/7nUKH+D1c/wFUEsBAhMAFAAAAAgA RXJKVNwFCPDRAQAAnxEAAAYAAAAAAAAAAAAAAAAAAAAAAERMLnR4dFBLBQYAAAAAAQABADQAAAD1 AQAAAAA=
      I want to unzip this using nodejs. Is this possible?
      Thanks. Regards,
      Hamza Masood