我正在使用C#编写代码,从互联网上下载了一个包含一个XML文件的zip压缩文件,并希望可以加载该XML文件。以下是我的目前的代码:
byte[] data;
WebClient webClient = new WebClient();
try {
data = webClient.DownloadData(downloadUrl);
}
catch (Exception ex) {
Console.WriteLine("Error in DownloadData (Ex:{0})", ex.Message);
throw;
}
if (data == null) {
Console.WriteLine("Bulk data is null");
throw new Exception("Bulk data is null");
}
//Create the stream
MemoryStream stream = new MemoryStream(data);
XmlDocument document = new XmlDocument();
//Gzip
GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress);
//Load report straight from the gzip stream
try {
document.Load(gzipStream);
}
catch (Exception ex) {
Console.WriteLine("Error in Load (Ex:{0})", ex.Message);
throw;
}
在document.Load
中,我总是会遇到以下异常:
GZip头部的魔数不正确。请确保传入的是GZip流。
我做错了什么?
GZip
还是Zip
?这两者并不相同。 - Odedzip != gzip
- 请参阅http://en.wikipedia.org/wiki/Gzip和http://en.wikipedia.org/wiki/ZIP_%28file_format%29。 - Polynomial