C# 或 Java 中的 Base64 解码
我有一个带有以下标头的 Base64 编码对象:
I have a Base64-encoded object with the following header:
application/x-xfdl;content-encoding="asc-gzip"
继续解码对象的最佳方法是什么?我需要剥离第一行吗?另外,如果我把它变成一个字节数组(byte[]),我该如何解压缩呢?
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?
谢谢!
我想我一开始说错了.通过说标题是
I think I misspoke initially. By saying the header was
application/x-xfdl;content-encoding="asc-gzip"
我的意思是这是文件的第一行.那么,为了使用 Java 或 C# 库来解码文件,是否需要剥离这一行?
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?
推荐答案
我能够使用以下代码将 .xfdl 文档转换为 Java DOM 文档.
I was able to use the following code to convert an .xfdl document into a Java DOM Document.
我使用 iHarder 的 Base64 实用程序进行 Base64 解码.
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.
希望这会有所帮助.
相关文章