SAP提供了一个类CL_ABAP_ZIP来创建.zip扩展名的压缩文件。
代码:
先将文件通过cl_gui_frontend_services=>gui_upload以BIN的文件类型上载,然后通过cl_abap_zip中的方法add()
save()压缩,最后下载到本地。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
REPORT ztest_zip.
TYPES: BEGIN OF bin_file,
name TYPE string,
size TYPE i,
data TYPE solix_tab,
END OF bin_file .
DATA: lv_filename TYPE string,
wa_bindata TYPE bin_file,
it_bindata TYPE STANDARD TABLE OF bin_file,
oref_zip TYPE REF TO cl_abap_zip,
lv_zip_xstring TYPE xstring,
lv_xstring TYPE xstring,
lv_path TYPE string,
it_filetab TYPE filetable,
lv_retcode TYPE i,
v_usr TYPE i,
v_zip_size TYPE i,
it_zip_bin_data TYPE STANDARD TABLE OF raw255,
v_dest_filepath TYPE string.
DATA lv_filesize TYPE i.
DATA wa_filetab LIKE LINE OF it_filetab.
"create object
CREATE OBJECT oref_zip.
"upload
cl_gui_frontend_services=>file_open_dialog(
EXPORTING
window_title = 'Select files that you want to ZIP'
multiselection = 'X'
CHANGING
file_table = it_filetab
rc = lv_retcode
user_action = v_usr ).
LOOP AT it_filetab INTO wa_filetab.
lv_filename = wa_filetab-filename.
cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = lv_filename
filetype = 'BIN'
IMPORTING
filelength = wa_bindata-size
CHANGING
data_tab = wa_bindata-data ).
CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
EXPORTING
full_name = lv_filename
IMPORTING
stripped_name = wa_bindata-name
EXCEPTIONS
x_error = 1
OTHERS = 2.
APPEND wa_bindata TO it_bindata.
ENDLOOP.
LOOP AT it_bindata INTO wa_bindata.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = wa_bindata-size
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = wa_bindata-data.
oref_zip->add( name = wa_bindata-name
content = lv_xstring ).
ENDLOOP .
lv_zip_xstring = oref_zip->save( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_xstring
IMPORTING
output_length = v_zip_size
TABLES
binary_tab = it_zip_bin_data.
cl_gui_frontend_services=>file_save_dialog(
EXPORTING
window_title = 'SELECT THE LOCATION TO SAVE THE FILE'
file_filter = '(*.ZIP)|*.ZIP|'
CHANGING
filename = lv_filename
path = lv_path
fullpath = v_dest_filepath ).
cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = v_zip_size
filename = v_dest_filepath
filetype = 'BIN'
IMPORTING
filelength = lv_filesize
CHANGING
data_tab = it_zip_bin_data ).
|
以上。