Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

[CL_ABAP_ZIP] How to zip a folder ?

Hi,

No problem to add one or several files to a ZIP archive using CL_ABAP_ZIP but how to add a folder (recursively if possible) or more simply create a folder in the archive ?

Many thanks in advance.

Best regards,

Guillaume

Former Member
Former Member replied

Hi Guillaume,

Don't know if there is a direct way to do that. But there is a workaround.....

1) Get the list of all files in the specified folder.

2) Add all the files to a zip folder and download it...

DATA : gv_file_length    TYPE i,
       gv_filehexcontent TYPE xstring,
       gv_zipfilehex     TYPE xstring,
       gt_filebincontent TYPE solix_tab,
       gt_zipfilebin     TYPE solix_tab,
       go_zipper         TYPE REF TO cl_abap_zip,

       gv_zipfilename    TYPE string,
       gv_folder         TYPE string,
       gt_files          TYPE TABLE OF char255 WITH HEADER LINE,
       gv_file           TYPE string,
       gv_file_fullpath  TYPE string,
       gv_no_files       TYPE i.

cl_gui_frontend_services=>directory_browse(  EXPORTING  window_title    = 'Folder selection'
                                             CHANGING   selected_folder = gv_folder
                                             EXCEPTIONS OTHERS          = 4 ).
CHECK sy-subrc EQ 0.

cl_gui_frontend_services=>directory_list_files( EXPORTING  directory   = gv_folder
                                                           files_only  = 'X'
                                                CHANGING   file_table  = gt_files[]
                                                           count       = gv_no_files
                                                EXCEPTIONS OTHERS      = 4 ).
CHECK sy-subrc EQ 0.
CHECK gv_no_files GT 0.

CREATE OBJECT go_zipper.

LOOP AT gt_files.

  MOVE gt_files TO gv_file.
  CONCATENATE gv_folder '\' gv_file INTO gv_file_fullpath.

  cl_gui_frontend_services=>gui_upload(
    EXPORTING
      filename   = gv_file_fullpath
      filetype   = 'BIN'
    IMPORTING
      filelength = gv_file_length
    CHANGING
      data_tab   = gt_filebincontent
    EXCEPTIONS
      OTHERS     = 4 ).

  CHECK sy-subrc EQ 0.

  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = gv_file_length
    IMPORTING
      buffer       = gv_filehexcontent
    TABLES
      binary_tab   = gt_filebincontent
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.

  CHECK sy-subrc EQ 0.

  go_zipper->add( name    = gv_file
                  content = gv_filehexcontent ).
ENDLOOP.

gv_zipfilehex = go_zipper->save( ).

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer     = gv_zipfilehex
  TABLES
    binary_tab = gt_zipfilebin.

CONCATENATE gv_folder '.zip' INTO gv_zipfilename.
CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    filename = gv_zipfilename
    filetype = 'BIN'
  CHANGING
    data_tab = gt_zipfilebin
  EXCEPTIONS
    OTHERS   = 4.

Cheers,

Jose.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question