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: 

cl_abap_gzip

Former Member
0 Kudos

I want to read a file "Dsn1" and zip it into "Dsn2" and put it in the application server

. The commands that I used are

1. Open dataset dsn1 for input in text mode encoding default.

2. Open dataset dsn2 for output in binay mode.

3. read dataset dsn1.

3. use cl_abap_gzip=>Compress_text , which converts the string from dsn1 to xstring.

4. transfer this xsting to dsn2.

5. close both the datasets.

I dont get any error while running the code.

It is put in application server. But when they try to unzip it in unix server using "GUNZIP dsn2"

they get an error like "not in gzip format"

Please let me know in case any one faced this issue.

Thanks,

Kavitha

1 ACCEPTED SOLUTION

Clemenss
Active Contributor
0 Kudos

Hi Kavitha,

for zipping files it may be easier (and much faster) to let the operating system handle this with the filter addition as mentioned in the [help page|http://help.sap.com/abapdocu_702/en/abapopen_dataset_os_addition.htm#!ABAP_ADDITION_2@2@].

OPEN DATASET file FOR OUTPUT IN BINARY MODE FILTER 'compress'.

Regards,

Clemens

12 REPLIES 12

Sandeep_Kumar
Product and Topic Expert
Product and Topic Expert
0 Kudos

0 Kudos

Hi Sandeep,

Thks for the help.

But still I have some doubts.

I opened the first file in text mode. So I used compress_text method for compressing the text .

This converts each line item to a xstring format.

I directly transfered thisxstring into the other file(with gz extension). I had opened the second file in binary mode.

Can you please tell me where I am wrong ?

Thanks,

Kavitha

Edited by: Kavitha Subash on Feb 12, 2011 6:41 PM

Edited by: Kavitha Subash on Feb 12, 2011 6:42 PM

Clemenss
Active Contributor
0 Kudos

Hi Kavitha,

for zipping files it may be easier (and much faster) to let the operating system handle this with the filter addition as mentioned in the [help page|http://help.sap.com/abapdocu_702/en/abapopen_dataset_os_addition.htm#!ABAP_ADDITION_2@2@].

OPEN DATASET file FOR OUTPUT IN BINARY MODE FILTER 'compress'.

Regards,

Clemens

Former Member
0 Kudos

Hi,

I used the command that you told. This is my code.

data:fdata type string

dsn = 'usr\test1'

dsn2 = 'usr\test1.gz'

OPEN DATASET dsn

FOR INPUT IN TEXT MODE ENCODING DEFAULT.

OPEN DATASET dsn1 FOR OUTPUT

IN BINARY MODE FILTER 'compress' .

DO.

READ DATASET dsn INTO fdata.

IF sy-subrc = 0.

TRANSFER fdata TO dsn1.

if sy-subrc NE 0.

exit.

endif.

endif.

enddo.

CLOSE DATASET dsn.

CLOSE DATASET dsn1.

They were able to unzip it.. but the data is mostly junk,

Any inputs will be highly appreciated.

Thanks ,

Kavitha

0 Kudos

Hi,

I think, "compress" option is a UNIX compression and no standard ZIP compresssion. So you can only read it from SAP with "uncompress" option or from UNIX with UNCOMPRESS command.

Regards,

Klaus

Edited by: Klaus Babl on Feb 14, 2011 10:59 AM

Former Member
0 Kudos

Hi Klaus,

Thanks for your help.

My requirement is to zip a file in application server and FTP it to another server.I can't send the unzipped file as it is very huge.

Is there a way to zip it in sap and put it in the application server.

I tried using CL_ABAP_GZIP (COMPRESS_TEXT) where I get the issue "Not in GZIP format"

When I used "COMPRESS" statement , When they decompress using Unix command the data is corrupted.

Please let me know if there are any code to zip the file and store it in application server.

Thanks and Regards,

Kavitha

0 Kudos

Hi Kavitha,

when you used the COMPRESS option, did you call the class method additionally? I think this shouldn't work!. Try to use COMPRESS without the GZIP class method and use the UNIX UNCOMPRESS command.

Whe you called the GZIP method, did you do that for a whole string table or line by line in a DO loop? I think GZIP only works for a whole file and not line by line.

Regards,

Klaus

0 Kudos

Hi Klaus,

I have tried 2 different options.

1.Using the Filter u201CCOMPRESSu201D with open dataset.

This is the code with which I used the COMPRESS option. I didnu2019t use GZIP class with u201Ccompressu201D filter. The decompress happens in unix using OS command . The command is u201Cgzip -d file.gzu201D( -d for decompress) .. Here the data is corrupted

data:fdata type string

dsn = 'usr\test1'

dsn2 = 'usr\test1.gz'

OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.

OPEN DATASET dsn1 FOR OUTPUT IN BINARY MODE FILTER 'compress' .

DO.

READ DATASET dsn INTO fdata.

IF sy-subrc = 0.

TRANSFER fdata TO dsn1.

if sy-subrc NE 0.

exit.

endif.

endif.

enddo.

CLOSE DATASET dsn.

CLOSE DATASET dsn1.

2.Using the method cl_abap_gzip=>compress_text

Here I get u201CNot in GZIP format u201C when they use GUNZIP usr\test1.gz

data:fdata type string data:lv_string type Xstring.

dsn = 'usr\test1'

dsn2 = 'usr\test1.gz'

OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.

OPEN DATASET dsn1 FOR OUTPUT IN BINARY MODE.

DO.

READ DATASET dsn INTO fdata.

IF sy-subrc = 0.

lv_result = fdata.

TRY.

CALL METHOD cl_abap_gzip=>compress_text

EXPORTING

text_in = lv_result

compress_level = lv_level

conversion = 'DEFAULT'

IMPORTING

gzip_out = lv_xstring.

CATCH cx_parameter_invalid_range .

CATCH cx_sy_buffer_overflow .

CATCH cx_sy_conversion_codepage .

CATCH cx_sy_compression_error .

ENDTRY.

TRANSFER lv_xstring TO dsn1.

if sy-subrc NE 0.

exit.

endif.

endif.

enddo.

CLOSE DATASET dsn.

CLOSE DATASET dsn1.

Thanks for ur help,

Kavitha

0 Kudos

Hi´Kavitha,

why didn't you try 1st option with OPEN FOR OUTPUT IN TEXT MODE WITH FILTER?

You haven't converted the text to BINARY xstring.

Regards,

Klaus

Edited by: Klaus Babl on Feb 14, 2011 12:34 PM

0 Kudos

Additional information:

Please have a look on SAP note 616958 when to use ABAP classes CL_ABAP_ZIP and CL_ABAP_GZIP

Former Member
0 Kudos

Hi Klaus,

Your inputs really helped.

Thanks for your help.

Kavitha

Former Member
0 Kudos

Hello,

also just came around this:

The problem is, that gzip_out only contains the gzipped data, not the header information.

-->http://www.onicos.com/staff/iz/formats/gzip.html

So when adding the needed information, it works:

DATA:
   lv_text              TYPE string,
   lv_gzip              TYPE xstring.

cl_abap_gzip=>compress_text( EXPORTING text_in     = lv_text
                              IMPORTING gzip_out    = lv_gzip ).

lv_gzip = '1F8B0800000000000003' && lv_gzip.


1F8B       --> GZIP Magic number

08           -->  compression method "deflate"

00           --> file probably ascii text

00000000 --> initial timestamp

00           --> no extra flags

03           --> os type unix (my SAP system is running on linux)


Then it is possible to gunzip it on linux/unix level.


Regards,

     Thorsten