Base64 decode in C# or Java

14,482

Solution 1

I was able to use the following code to convert an .xfdl document into a Java DOM Document.

I used iHarder's Base64 utility to do the Base64 Decode.

private static final String FILE_HEADER_BLOCK = 
        "application/vnd.xfdl;content-encoding=\"base64-gzip\"";  

    public static Document OpenXFDL(String inputFile) 
            throws IOException, 
                ParserConfigurationException,
                SAXException

    {
        try{

            //create file object
            File f = new File(inputFile);
            if(!f.exists()) {
                throw new IOException("Specified File could not be found!");
            }

            //open file stream from file
            FileInputStream fis = new FileInputStream(inputFile);

            //Skip past the MIME header
            fis.skip(FILE_HEADER_BLOCK.length());   

            //Decompress from base 64                   
            Base64.InputStream bis = new Base64.InputStream(fis, 
                    Base64.DECODE);

            //UnZIP the resulting stream
            GZIPInputStream gis = new GZIPInputStream(bis);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(gis);

            gis.close();
            bis.close();
            fis.close();

            return doc;
        }
        catch (ParserConfigurationException pce) {
            throw new ParserConfigurationException("Error parsing XFDL from file.");
        }
        catch (SAXException saxe) {
            throw new SAXException("Error parsing XFDL into XML Document.");
        }
    }

Still working on successfully modifying and re-encoding the document.

Hope this helps.

Solution 2

To decode the Base64 content in C# you can use the Convert Class static methods.

byte[] bytes = Convert.FromBase64String(base64Data);

You can also use the GZipStream Class to help deal with the GZipped stream.

Another option is SharpZipLib. This will allow you to extract the original data from the compressed data.

Solution 3

In Java, you can use the Apache Commons Base64 class

String decodedString = new String(Base64.decodeBase64(encodedBytes));

Solution 4

It sounds like you're dealing with data that is both gzipped and Base 64 encoded. Once you strip off any mime headers, you should convert the Base64 data to a byte array using something like Apache commons codec. You can then wrap the byte[] in a ByteArrayInputStream object and pass that to a GZipInputStream which will let you read the uncompressed data.

Share:
14,482
Jason
Author by

Jason

Updated on September 05, 2022

Comments

  • Jason
    Jason over 1 year

    I have a Base64-encoded object with the following header:

    application/x-xfdl;content-encoding="asc-gzip"
    

    What is the best way to proceed in decoding the object? Do I need to strip the first line? Also, if I turn it into a byte array (byte[]), how do I un-gzip it?

    Thanks!


    I think I misspoke initially. By saying the header was

    application/x-xfdl;content-encoding="asc-gzip"
    

    I meant this was the first line of the file. So, in order to use the Java or C# libraries to decode the file, does this line need to be stripped?

    If so, what would be the simplest way to strip the first line?