从一网站上来载的xls,是用gzip压缩的,下面的代码不能解压,gzipstream.read()啥也没有。如果我直接将mem写入文件,然后用winrar解压是正确的。 bool ret = false;
byte[] bs = Encoding.ASCII.GetBytes(url);
Stream reqStream = null;
Stream stream = null;
StreamReader streamReader = null;
HttpWebResponse webResponse = null;
HttpWebRequest req = null; try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
req.CookieContainer = SpeedCookie;
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; reqStream = req.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
webResponse = (HttpWebResponse)req.GetResponse();
stream = webResponse.GetResponseStream();//以流的形式读取
MemoryStream mem = new MemoryStream(); //先写入内存,再解压
Byte[] buf = new Byte[4096];
int count = stream.Read(buf, 0, 4096);
while (count > 0)
{
mem.Write(buf, 0, count);
count = stream.Read(buf, 0, 4096);
}mem中的数据是没有问题的,我直接将mem写入文件,然后用winrar打开是对的
//GZIP解压先
GZipStream zip = new GZipStream(mem, CompressionMode.Decompress);
FileStream file = new FileStream(path, FileMode.Create);
count = zip.Read(buf, 0, 4096);
while (count > 0)
{
file.Write(buf, 0, count);
count = zip.Read(buf, 0, 4096);
} file.Close();
mem.Close();
}
我用http的stream来构造gzipstream,也啥都没读出来,返回的头是这样的:
Content-Disposition: attachment;filename=summary_2013_04_13_11_06_52.xls.gz
Content-Length: 2346
Content-Type: application/x-gzip;charset=utf-8
Date: Sat, 13 Apr 2013 03:06:53 GMT
Server: Apache-Coyote/1.1
byte[] bs = Encoding.ASCII.GetBytes(url);
Stream reqStream = null;
Stream stream = null;
StreamReader streamReader = null;
HttpWebResponse webResponse = null;
HttpWebRequest req = null; try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
req.CookieContainer = SpeedCookie;
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; reqStream = req.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
webResponse = (HttpWebResponse)req.GetResponse();
stream = webResponse.GetResponseStream();//以流的形式读取
MemoryStream mem = new MemoryStream(); //先写入内存,再解压
Byte[] buf = new Byte[4096];
int count = stream.Read(buf, 0, 4096);
while (count > 0)
{
mem.Write(buf, 0, count);
count = stream.Read(buf, 0, 4096);
}mem中的数据是没有问题的,我直接将mem写入文件,然后用winrar打开是对的
//GZIP解压先
GZipStream zip = new GZipStream(mem, CompressionMode.Decompress);
FileStream file = new FileStream(path, FileMode.Create);
count = zip.Read(buf, 0, 4096);
while (count > 0)
{
file.Write(buf, 0, count);
count = zip.Read(buf, 0, 4096);
} file.Close();
mem.Close();
}
我用http的stream来构造gzipstream,也啥都没读出来,返回的头是这样的:
Content-Disposition: attachment;filename=summary_2013_04_13_11_06_52.xls.gz
Content-Length: 2346
Content-Type: application/x-gzip;charset=utf-8
Date: Sat, 13 Apr 2013 03:06:53 GMT
Server: Apache-Coyote/1.1
下载地址
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
http://blog.csdn.net/newsseasky/article/details/1752310
http://hi.baidu.com/ajiecs/item/40cdfcc7f86fe10dc710b296