Skip to Content
Technical Articles
Author's profile photo Hugo A

ABAP programming: How to zip a file with password

Introduction

Some time ago I received a new challenge at work: The business requested to send a zipped file by e-mail, with password.

Sending files by e-mail is not something new. But zip the file and set a password from ABAP was a challenge. After some researching, I have not found something like a function module or a class to do it from an ABAP program. But what I have found is that from an ABAP program, we can run commands on the server side. This, open the door for a possible solution.

Solution

So, basically, what I did to solve this issue is: generate the file from the ABAP program and save it on the server side. Then run this command and zip the file together with the password. All in the same command.

Below, the steps to be followed:

  1. Generate the file in the server side. In this case was an excel so first I converted to an XSTRING and then save with the OPEN DATASET/TRANSFER:
  OPEN DATASET p_file FOR OUTPUT IN BINARY MODE.

  IF sy-subrc = 0.
    TRANSFER lv_xstring TO p_file.
    CLOSE DATASET p_file.
  ENDIF.

2. We have to create the command on the server. It is quite simple, in our case we called it ZSD_ENCRYPT (maybe not so correctly called :). And then the most important is that as marked in below red box we enter “ZIP”.

Command created in server (Linux)

3. Then we call below function modules. There is no much to comment about it but it is to do kind of check of the command. Please adapt the coding with correct error handling and according to the naming convention you use:

DATA: BEGIN OF command_list OCCURS 0.
      INCLUDE STRUCTURE sxpgcolist.
  DATA: END OF command_list .

DATA: status      LIKE btcxp3-exitstat,
      commandname LIKE sxpgcolist-name VALUE 'ZSD_ENCRYPT',
      sel_no      LIKE sy-tabix.

* GET LIST OF EXTERNAL COMMANDS
  CALL FUNCTION 'SXPG_COMMAND_LIST_GET'
    EXPORTING
      commandname     = commandname
      operatingsystem = sy-opsys
    TABLES
      command_list    = command_list
    EXCEPTIONS
      OTHERS          = 1.

  CALL FUNCTION 'SXPG_COMMAND_CHECK'
    EXPORTING
      commandname                = command_list-name
      operatingsystem            = sy-opsys
    EXCEPTIONS
      no_permission              = 1
      command_not_found          = 2
      parameters_too_long        = 3
      security_risk              = 4
      wrong_check_call_interface = 5
      x_error                    = 6
      too_many_parameters        = 7
      parameter_expected         = 8
      illegal_command            = 9
      communication_failure      = 10
      system_failure             = 11
      OTHERS                     = 12.

 

4. Below is where the magic occurs. Here I generate the zip file protected by password:

* In below concatenate I set the parameters to the ZIP command is going to be run in the server side.
* -P means the zip goes with password.
* LV_PASSWORD -> I set the password
* L_FILE_ZIP -> is the zip file name (destination)
* L_FILE -> is the file to be zipped (origin)

CONCATENATE '-j' '-P' lv_password l_file_zip l_file INTO v_dir_input SEPARATED BY space.

* C_EXTCOM is the command I created in SM49 before. In my case ZSD_ENCRYPT.
* C_OPER is the operating system. In S4 Hana normally is Linux.

  CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
    EXPORTING
      commandname                   = c_extcom
      additional_parameters         = v_dir_input
      operatingsystem               = c_oper
    TABLES
      exec_protocol                 = t_result
    EXCEPTIONS
      no_permission                 = 1
      command_not_found             = 2
      parameters_too_long           = 3
      security_risk                 = 4
      wrong_check_call_interface    = 5
      program_start_error           = 6
      program_termination_error     = 7
      x_error                       = 8
      parameter_expected            = 9
      too_many_parameters           = 10
      illegal_command               = 11
      wrong_asynchronous_parameters = 12
      cant_enq_tbtco_entry          = 13
      jobcount_generation_error     = 14
      OTHERS                        = 15.

Summary

Once you create the file on the server it is up to you how to continue. You could copy from the server to a local laptop or send by e-mail. This is up to the business request. Take into account that you can face authorization issues when running the command. Maybe you have to talk with basis or authorization guy. In this case, we run the command to zip a file but take into account that you could run any other command which is available in the op system (Linux in our case).

Sometimes we get challenges at work. At first time they could look impossible to solve but if we struggle a little bit and try to be creative many times we can reach to a good solution for the business.

Hope it be useful for you this small “hack” 🙂

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Matthew Billingham
      Matthew Billingham

      https://blogs.sap.com/2012/07/06/email-pdf-attachment-with-password-protection/

      https://blogs.sap.com/2021/10/13/send-password-protected-pdf-file-from-sap-via-email/

       

      Author's profile photo M KesavaSai Karthikeya
      M KesavaSai Karthikeya

      Hi Hugo.
      Thank you for such a useful blog. The article is so helpful.

      On going through the blog, I have below questions -

      1. Are u placing the .ZIP File in AL11 and encrypting it with OS Command.?
      2.CONCATENATE '-j' '-P' lv_password l_file_zip l_file INTO v_dir_input SEPARATED BY space.
      Can you let us know how are you sending the data to l_file_zip and l_file

      Author's profile photo Hugo A
      Hugo A
      Blog Post Author

      Hi. Happy to hear that.

      Regarding to 1st question answer is not. In my case I placed an excel file to AL11 (it could be any type of file) and then with the command I zip and set the password.

      Regarding to your 2nd question:

      In my case was:

      l_file '/usr/sap/tmp/encryptedfile_job.xlsx'. --> the file to be converted. In this case was an excel but could be any type of file.

      l_file_zip '/usr/sap/tmp/encryptedfile_job.zip'. --> is the zip file