Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Zip a zipped file through ABAP code

0 Kudos
1,771

Hi Experts,

My requirement is, I have data in internal table and from that I need to create .csv file and then zip this file. Once this file is zipped, then I need to zip this zipped File and download it to the presentation server. I am able to do the first zip by using class CL_ABAP_ZIP (methods are add and save). But how to zip this zipped file again? If I use the same method then what I need to pass in the content parameter as we can only pass xstring. Please provide your suggestions on the same.

IT_TABLE->.CSV File->ABC.ZIP (This part is done).

Now

ABC.ZIP->XYZ.ZIP (How to achieve this)

Best Regards

Sumit

16 REPLIES 16

abo
Active Contributor
867

Out of curiosity, why do you need to zip it twice? It won't shrink further, on the contrary it will grow.

Sandra_Rossi
Active Contributor
867

You probably didn't do the first ZIP correctly because the output MUST BE an xstring variable. And consequently you can ZIP it again.

NB: I don't understand why you "need to zip this zipped file"....

matt
Active Contributor
0 Kudos
867

Zipping a zipped file won't compress the file further. It can be useful if you need to put a bunch of zipped files into one zip file.

867

I know this sounds bit weired, but that's the requirement from the client side hence I need to zip the file once and then zip that zipped folder (compressed folder) which contains the file.

FredericGirod
Active Contributor
867

Because it would like one archive file for all the compressed file.

It could be so simple to do it with command line, if you run in Linux : tar cvzf new_archive.tar.bz /directory

Sandra_Rossi
Active Contributor
867

You say "but that's the requirement from the client", so you don't discuss a weird request? Most of the time a weird request is in fact a request badly expressed and finally the client will be unhappy that you understood incorrectly and that you didn't ask for confirmation.

867

Sandra, we discussed it number of times and client already setup a process where they need to receive the file in zipped folder, they will unzipped it and then another zipped file will be there which contains the data. So now it's our task to zip the already zipped file.

matt
Active Contributor
867

SAVE and ADD both take xstrings. GET returns an xstring.

I don't see what the issue is. You've got a program like:

data csv_file type xstring.
csv_file = get_csv_file( ).
data zipped_file type xstring.
zipped_file = zip( csv_file).

so all you need is

data double_zipped_file type xstring.
double_zipped_file = zip( zipped_file ).

Sandra_Rossi
Active Contributor
0 Kudos
867

Thanks for confirming that there is a good reason to "double zip".

0 Kudos
867

This is the program I have prepared and first zipped folder I have created is 'TEST_ZIP.ZIP', so Matt where I need to write the code for zipping the file again and how to achieve it, what all I need to pass. do I need to pass lv_xstring, if yes then how I am going to ZIP 'TEST_ZIP.ZIP' again in another compressed folder let's say 'TEST_REZIPPED.ZIP'.

0 Kudos
867

CREATE OBJECT go_zip.
* Add teh X String as a Zip file
CALL METHOD go_zip->add
EXPORTING
name = 'Test_ZIP'
content = lv_xstring.

CALL METHOD go_zip->save
RECEIVING
zip = lv_xstring.

* Convert Xstring to Binary Table
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_bin.


* Download the Binary table
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\Users\SumitKumar\Documents\Test_Files\TEST_ZIP.ZIP'
filetype = 'BIN'
TABLES

data_tab = lt_bin.

Sandra_Rossi
Active Contributor
0 Kudos
867

You don't need to download the first zip, it's already stored in lv_xstring.

Your code to create the first ZIP file (containing lv_xstring):

DATA(go_zip) = NEW cl_abap_zip( ).
go_zip->add( name = 'file name in ZIP file' content = lv_xstring ).
DATA(zip_xstring) = go_zip->save( ).

The ZIP file is zip_xstring.

Add this code to create the "ZIP ZIP" file:

DATA(go_zip2) = NEW cl_abap_zip( ).
go_zip2->add( name = 'ZIP file name in ZIP file' content = zip_xstring ).
DATA(zip2_xstring) = go_zip2->save( ).

And now you can download the ZIP ZIP file.

0 Kudos
867

Thanks for your reply Sandra, but still not working. It is creating a compressed folder but as soon as I double click on it, it is showing me the .csv file which is not expected. Once I double click on the first compressed folder (.zip folder) then it should show me another compressed folder and then the .csv file. I tried your code, but still not getting the expected result. I am attaching the code below:

CALL METHOD go_zip->add
EXPORTING
name = 'Test_ZIP'
content = lv_xstring.
****************************************************
* CALL METHOD go_zip->save
* RECEIVING
* zip = lv_xstring.
DATA(zip_xstring) = go_zip->save( ).

DATA(go_zip2) = NEW cl_abap_zip( ).
go_zip2->add( name = 'TEST_REZIP' content = zip_xstring ).
DATA(zip2_xstring) = go_zip2->save( ).

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = zip2_xstring
TABLES
binary_tab = lt_bin.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\Users\SumitKumar\Documents\Test_Files\TEST_ZIP.ZIP'
filetype = 'BIN'
TABLES
data_tab = lt_bin.

Sandra_Rossi
Active Contributor
867

Code for "double zipping" a UTF-8 text containing "ABCD".

DATA(go_zip) = NEW cl_abap_zip( ).
DATA(lv_xstring) = CONV xstring( '41424344' ). " UTF-8 text "ABCD"
go_zip->add( name = 'file name in ZIP file.txt' content = lv_xstring ).
DATA(zip_xstring) = go_zip->save( ).
DATA(go_zip2) = NEW cl_abap_zip( ).
go_zip2->add( name = 'ZIP file name in ZIP file.zip' content = zip_xstring ).
DATA(zip2_xstring) = go_zip2->save( ).
DATA(lt_bin) = cl_bcs_convert=>xstring_to_solix( zip2_xstring ).
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename = 'C:\Users\Sandra.Rossi\Downloads\TEST_ZIP.ZIP'
    filetype = 'BIN'
    bin_filesize = xstrlen( zip2_xstring )
  TABLES
    data_tab = lt_bin.<br>

867

Thanks a lot Sandra, its working now!!

matt
Active Contributor
867

Why are you using CALL METHOD? That's been unnecessary for well over a decade.Far better to use the short form - much easier to read.