0

In Spring Boot the zip file that comes as a response has a corrupted structure before saving, but there is no problem when I save it physically. I need to take the file in the zip file and process the information in the database, but I cannot physically download this file because I am using GCP. How can I extract the file in this zip file that comes as a response?. How can I solve this please help.

Here is what it looks like in response.body() before saving (part of it):

"PK C`iUq �=n 緰) bu_customerfile_22110703_B001_10292121141�]i��������RI%U�v��CJ� ���×��My��y/ίϹ�������=>����}����8���׿}~}~yz�������ͲL�� �o�0�fV�29f�����6΋$K�c$�F��/�8˳�L��_�QaZ-q�F�d4γE�[���(f�8�D�0��2_��P"�I�A��D��4�߂�����D��(�T�$.��<�,���i]Fe�iM�q<ʨ�Olmi�(&���?�y�y4��<��Q�X�ޘp�@�6f-.F����8����"I㢨ҤU]�E��WI� %@������(W�8*0c�p:L��:� �}�G����e<����a�"

Here is the request call:

OkHttpClient client1 = new OkHttpClient().newBuilder()
        .build();
MediaType mediaType1 = MediaType.parse("text/plain");
RequestBody body1 = RequestBody.create(mediaType1, "");
Request request1 = new Request.Builder()
        .url(vers)
        .method("POST", body1)
        .addHeader("Cookie", "ASP.NET_SessionId=44dxexdxass5mtf00udjfwns")
        .build();
Response response1 = client1.newCall(request1).execute();

String data = response1.body().string();

1 Answers1

0

Would it be a matter of encoding type? String is of type UTF-16 (I think). Try a different datatype that is more like an array/vector of bytes.

Try something like what is mentioned here: Having trouble reading http response into input stream

Update: Get the response as a stream of bytes and feed it into a ZipInputStream object as shown here https://zetcode.com/java/zipinputstream/#:~:text=ZipInputStream%20is%20a%20Java%20class,both%20compressed%20and%20uncompressed%20entries.

Then iterate over the contained files to find the one you need. Then retrieve the stream associated with the zipped file. Then you can read from there. (I realize that is a bit of handwaving, but it's been a while since I used Zip files and Java.) That should get you down the correct path.

Joe Derham
  • 37
  • 1
  • 7
  • I applied the decode operations, but none of them gave any results. I don't think this is an encoded file. – Fatma Nur AYDIN Nov 16 '22 at 18:32
  • You may need to play with the encoding type to something more binary related (application/octet-stream). What do you plan to do with the data once you've loaded it into memory? – Joe Derham Nov 16 '22 at 19:51
  • There is route information in the file, I need to print this information to the database. I want to do this in a cronjob and update the data in the database on certain days of the month. – Fatma Nur AYDIN Nov 17 '22 at 07:22
  • See update above. Good luck Fatma! – Joe Derham Nov 17 '22 at 16:30