C# 或 Java 中的 Base64 解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1915898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base64 decode in C# or Java
提问by Jason
I have a Base64-encoded object with the following header:
我有一个带有以下标头的 Base64 编码对象:
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?
解码对象的最佳方法是什么?我需要去掉第一行吗?另外,如果我把它变成一个字节数组 (byte[]),我该如何解压缩它?
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?
我的意思是这是文件的第一行。那么,为了使用 Java 或 C# 库来解码文件,是否需要剥离这一行?
If so, what would be the simplest way to strip the first line?
如果是这样,剥离第一行的最简单方法是什么?
回答by scptre
To decode the Base64 content in C# you can use the Convert Class static methods.
要在 C# 中解码 Base64 内容,您可以使用Convert Class 静态方法。
byte[] bytes = Convert.FromBase64String(base64Data);
You can also use the GZipStream Classto help deal with the GZipped stream.
您还可以使用GZipStream 类来帮助处理 GZipped 流。
Another option is SharpZipLib. This will allow you to extract the original data from the compressed data.
另一种选择是SharpZipLib。这将允许您从压缩数据中提取原始数据。
回答by Spike Williams
In Java, you can use the Apache Commons Base64 class
在 Java 中,您可以使用Apache Commons Base64 类
String decodedString = new String(Base64.decodeBase64(encodedBytes));
回答by MrWizard54
I was able to use the following code to convert an .xfdl document into a Java DOM Document.
我能够使用以下代码将 .xfdl 文档转换为 Java DOM 文档。
I used iHarder'sBase64 utility to do the Base64 Decode.
我使用iHarder 的Base64 实用程序进行 Base64 解码。
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.
希望这可以帮助。
回答by Jherico
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 ByteArrayInputStreamobject and pass that to a GZipInputStreamwhich will let you read the uncompressed data.
听起来您正在处理 gzip 和 Base 64 编码的数据。去除任何 mime 标头后,您应该使用类似 Apache commons 编解码器的方式将 Base64 数据转换为字节数组。然后,您可以将 byte[] 包装在一个ByteArrayInputStream对象中并将其传递给 a GZipInputStream,这样您就可以读取未压缩的数据。
回答by user12786
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.
对于java,您是否尝试过java 内置的java.util.zip 包?或者,Apache Commons 有 Commons Compress 库来处理 zip、tar 和其他压缩文件类型。至于解码 Base 64,有几个开源库,或者你可以使用 Sun 的 sun.misc.BASE64Decoder 类。
回答by Alex
Copied from elsewhere, for Base64 I link to commons-codec-1.6.jar:
从别处复制,对于 Base64 我链接到 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();
}

