FNA
FNA

Reputation: 11

In Spring Boot Java Extracting the zip file from response without physically saving it

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();

Upvotes: 0

Views: 1160

Answers (1)

Joe Derham
Joe Derham

Reputation: 43

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.

Upvotes: 0

Related Questions