0

I have a base64 string which when decoded online using this website gives a zip file. When the zip file is downloaded it is password protected.

In my case, I know the password but I am not able to convert base64 to a zip file and also open it and read it in Android.

I used this answer on stack overflow but I am getting an error

Error: "java.util.zip.ZipException: Not in GZIP format"

I need a solution where I have the base64 String that it should decode into a zip file and then read the content of this zip file using the given password which has XML data.

Here is the code I have used so far:

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;

public class GzipUtil {
    public static void unzip() throws Base64DecodingException {
        String encoded = "PUT BASE 64ENCODED GZIPPED STRING HERE";
        byte[] compressed = Base64.decode(encoded);
        String data = new String(compressed);
        //System.out.println(data);AAAA";

        if ((compressed == null) || (compressed.length == 0)) {
            throw new IllegalArgumentException("Cannot unzip null or empty bytes");
        }
        if (!isZipped(compressed)) {
            System.out.println(compressed);
        }

        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
            try (GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream)) {
                try (InputStreamReader inputStreamReader =
                             new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8)) {
                    try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                        StringBuilder output = new StringBuilder();
                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            output.append(line);
                            System.out.println(output.toString());
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Failed to unzip content", e);
        }
    }

    public static boolean isZipped(final byte[] compressed) {
        return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
                && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
    }
}

Please help!

Shubham Agrawal
  • 1,252
  • 12
  • 29
  • Please replace("convert", "decode"). – blackapps Jan 26 '23 at 19:53
  • Please stick to one problem. If you cannot decode then you dont have to talk about read and show file. Confusing. – blackapps Jan 26 '23 at 19:55
  • You should post your code. Not only a link. But i can already tell you that `String data = new String(compressed);` is nonsense as one cannot put a pdf file in a string. – blackapps Jan 26 '23 at 19:58
  • "I am getting an error" -- a ZIP file is not compressed with GZIP, despite the similarity in the names. But I agree with blackapps: please provide a [mcve] and a complete stack trace. – CommonsWare Jan 26 '23 at 20:04

0 Answers0