简体   繁体   中英

Base64 decode in C# or Java

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?

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.

在Java中,您可以使用Apache Commons Base64类

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

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.

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.

For java, have you tried java's built in java.util.zip package? Alternately, Apache Commons has the Commons Compress library to work with zip, tar and other compressed file types. As to decoding Base 64, there are several open source libraries, or you can use Sun's sun.misc.BASE64Decoder class.

Copied from elsewhere, for Base64 I link to commons-codec-1.6.jar:

public static String decode(String input) throws Exception {
    byte[] bytes = Base64.decodeBase64(input);
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new GZIPInputStream(new ByteArrayInputStream(bytes))));
    StringBuffer buffer = new StringBuffer();
    char[] charBuffer = new char[1024]; 
    while(in.read(charBuffer) != -1) {
        buffer.append(charBuffer);
    }
    return buffer.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM