Convert Base64 to zip file (password protected) in android

240 Views Asked by At

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!

0

There are 0 best solutions below