Vous êtes sur la page 1sur 4

CL_ABAP_ZIP usage - Zipping ABAP report

output

Skip to end of metadata


Created by Venkat O, last modified by Moshe Naveh on Feb 24, 2010
Go to start of metadata

Information on CL_ABAP_ZIP class


As of Release 6.40 the class CL_ABAP_ZIP is also available to read or create ZIP files.Therefore, if We want to process
compressed files or folders, we must use CL_ABAP_ZIP, as the system creates or reads the headers or trailers required for
file processing here only.

Zipping ABAP report output.


Steps involved in the development.
1.

Send report output to spool and get spool number.

2.

Convert spool to PDF using CONVERT_ABAPSPOOLJOB_2_PDF function module.

3.

Convert Binary data to XSTRING data using SCMS_BINARY_TO_XSTRING function module.

4.

The zipping functionality is done by the class CL_ABAP_ZIP

a.

use method CL_ABAP_ZIP->ADDto add a file to compress PDF file.This method requires the content in
xstring fromat and not in an internal table. If Our data is in an internal table We will have to convert it into xstring(Step-3).
CL_ABAP_ZIP->ADD method requires file name and Content in xstring format.

b.
5.

CL_ABAP_ZIP->SAVE method gives the zipped file we want. Zipped file is also type xstring.
As CL_ABAP_ZIP->SAVE gives xstring file, we need to convert using SCMS_XSTRING_TO_BINARY to binary
and download using GUI_DOWNLOAD with file type 'BIN'.

Sample ABAP Program


Here is one working sample program. The report sends output to spool and based on spool, spool is converted to PDF, PDF
file is Zipped using CL_ABAP_ZIP and downloaded finally.

REPORT ztest_zip_repoutout.
DATA:g_val
TYPE c,
w_pripar
TYPE pri_params,
w_arcpar
TYPE arc_params,
input_length TYPE i,
content_x
TYPE xstring,
content_s
TYPE string,
spool_no
LIKE tsp01-rqident,

it_pdf
TYPE
wa_pdf
LIKE
file_tab
TYPE
bin_file
TYPE
bytecount
TYPE
zip
TYPE
path_table
TYPE
DATA: BEGIN OF it_t001
bukrs
TYPE
butxt
TYPE
END OF it_t001.
DATA:g_zipper
TYPE
DATA:file_name
TYPE
DATA:file_path
TYPE

TABLE OF tline WITH HEADER LINE,


LINE OF it_pdf,
STANDARD TABLE OF solisti1,
xstring,
i,
xstring,
TABLE OF char1024.
OCCURS 0,
t001-bukrs,
t001-butxt,
REF TO cl_abap_zip.
string VALUE 'Report_output.pdf'.
string VALUE 'C:\temp\pdf_report.ZIP'.

START-OF-SELECTION.
PERFORM get_data.
PERFORM print_data CHANGING spool_no.
PERFORM convert_spool_2_pdf.
PERFORM convert_binary_2_xstring.
PERFORM zipping_pdf_file.
PERFORM download_zip_file.
*&---------------------------------------------------------------------*
*&
Form get_data
*&---------------------------------------------------------------------*
FORM get_data .
SELECT * FROM t001
INTO CORRESPONDING FIELDS OF TABLE it_t001 UP TO 10 ROWS.
ENDFORM.
" get_data
*&---------------------------------------------------------------------*
*&
Form print_data
*&---------------------------------------------------------------------*
FORM print_data CHANGING spool_no.
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
in_archive_parameters = w_arcpar
in_parameters
= w_pripar
layout
= 'X_65_132'
line_count
= 65
line_size
= 132
no_dialog
= 'X'
IMPORTING
out_archive_parameters = w_arcpar
out_parameters
= w_pripar
valid
= g_val.
IF g_val NE space AND sy-subrc = 0.
w_pripar-prrel = space.
w_pripar-primm = space.
NEW-PAGE PRINT ON NEW-SECTION PARAMETERS w_pripar ARCHIVE PARAMETERS
w_arcpar NO DIALOG.
"Output which is gone to spool
LOOP AT it_t001.
WRITE:/ it_t001.
ENDLOOP.

NEW-PAGE PRINT OFF.


CALL FUNCTION 'ABAP4_COMMIT_WORK'.
ENDIF.
IF NOT sy-spono IS INITIAL.
spool_no = sy-spono.
ENDIF.
ENDFORM.
" print_data
*&---------------------------------------------------------------------*
*&
Form convert_spool_2_pdf
*&---------------------------------------------------------------------*
FORM convert_spool_2_pdf .
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid
= spool_no
no_dialog
= space
IMPORTING
pdf_bytecount = bytecount
TABLES
pdf
= it_pdf.
CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
EXPORTING
line_width_src = 134
line_width_dst = 255
TABLES
content_in
= it_pdf
content_out
= file_tab.
ENDFORM.
" convert_spool_2_pdf
*&---------------------------------------------------------------------*
*&
Form convert_binary_2_xstring
*&---------------------------------------------------------------------*
FORM convert_binary_2_xstring .
"create xstring from table
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = bytecount
IMPORTING
buffer
= content_x
TABLES
binary_tab
= file_tab.
ENDFORM.
" convert_binary_2_xstring
*&---------------------------------------------------------------------*
*&
Form zipping_pdf_file
*&---------------------------------------------------------------------*
FORM zipping_pdf_file .
"create our zipper object
CREATE OBJECT g_zipper.
"add file to zip
CALL METHOD g_zipper->add
EXPORTING
name
= file_name
content = content_x.
"save zip
CALL METHOD g_zipper->save
RECEIVING
zip = zip.

ENDFORM.
" zipping_pdf_file
*&---------------------------------------------------------------------*
*&
Form download_zip_file
*&---------------------------------------------------------------------*
FORM download_zip_file .
"convert to table
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer
= zip
IMPORTING
output_length = bytecount
TABLES
binary_tab
= file_tab.
"Save the file
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = bytecount
filename
= file_path
filetype
= 'BIN'
TABLES
data_tab
= file_tab.
ENDFORM.
" download_zip_file