Skip to Content

[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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 26, 2008 at 07:22 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 18, 2012 at 01:22 PM

    direct solution is simple and described here:

    create folder in zip file (cl_abap_zip)

    I am not sure if this is officially supported, but it works for my use case. Recurse directories by yourself and add files to the ZIP files with the relative path in name, delimiter is '/'. Keep in mind that you can't do recursion with function moduls, so use methods.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.