Although our connections are now so fast we may still need sometimes to compress data before attaching to mail or downloading into PC. SAP has possibilities to ZIP the content in class CL_ABAP_ZIP and I've used this class to create a method to simplify the process of zipping.
First of all class CL_ABAP_ZIP is zipping xstrings so I'll provide you the possibility to pass not only xstring variables but also a string, soli_tab or solix_tab. These are commonly used type when coding files content.
Importing:
I_XSTRING TYPE XSTRING OPTIONAL -> xstring to be zipped
I_STRING TYPE STRING OPTIONAL -> string to be zipped
I_FILENAME TYPE STRING -> filename used as name of file inside the zip container
I_SOLIX TYPE SOLIX_TAB OPTIONAL -> SOLIX_TAB to be zipped
I_SOLI TYPE SOLI_TAB OPTIONAL -> SOLI_TAB to be zipped
I_GET_XSTRING TYPE C DEFAULT SPACE -> 'X' if you want to retrieve zipped file as xstring
Changing:
C_ZIPPER TYPE REF TO CL_ABAP_ZIP OPTIONAL Zip Utility -> If you have own object into which you want to add the content then pass it here
Exporting:
E_XSTRING TYPE XSTRING -> xstring with zipped file (you get it when you put 'X' to I_GET_XSTRING )
Exceptions:
ATTACHMENT_IS_EMPTY I_XSTRING & I_STRING variables are empty
STRING_CONV_ERROR Error during conversion string to xstring
Implementation:
method zip.
field-symbols: <zip> type ref to cl_abap_zip.
data: m_zipper type ref to cl_abap_zip.
data: m_xstring type xstring.
data: m_solix type solix_tab.
if c_zipper is supplied.
assign c_zipper to <zip>.
if sy-subrc ne 0.
exit.
endif.
else.
create object m_zipper.
assign m_zipper to <zip>.
if sy-subrc ne 0.
exit.
endif.
endif.
*get xstring of file to be packed
if i_xstring is not initial.
m_xstring = i_xstring.
elseif i_string is not initial.
call function 'SCMS_STRING_TO_XSTRING'
exporting
text = i_string
* MIMETYPE = ' '
* ENCODING =
importing
buffer = m_xstring
exceptions
failed = 1
others = 2
.
if sy-subrc <> 0.
raise string_conv_error.
endif.
elseif i_solix[] is not initial.
try.
m_xstring = cl_bcs_convert=>solix_to_xstring(
it_solix = i_solix
* iv_size = iv_size
).
catch cx_bcs.
message e445(so).
endtry.
elseif i_soli[] is not initial.
try.
m_solix[] = cl_bcs_convert=>soli_to_solix( it_soli = i_soli ).
m_xstring = cl_bcs_convert=>solix_to_xstring(
it_solix = m_solix
* iv_size = iv_size
).
catch cx_bcs.
message e445(so).
endtry.
else.
raise attachment_is_empty.
endif.
* Add File
<zip>->add(
exporting
name = i_filename
content = m_xstring ).
if i_get_xstring is not initial.
* Get Zip-Archive
e_xstring = <zip>->save( ).
endif.
endmethod.
This is fast and handy method. You'll see how to use it soon.