|
-
February 16th, 2010, 03:37 PM
#1
how corectly unzip GZipStream
hi all, i have trouble with my application, i writen my personal cache using sockets and streams, but i have big trouble with unpack GZip, this string int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length); is trouble, when i try read from stream, i have an Exception, that the GZip header magic number is not corrent. InvalidDataException. here is my code...
public long WriteURL()
{
long num1 = 0;
string host = new Uri(_url).Host;
IPHostEntry ipAddress = Dns.GetHostEntry(host);
IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
s.Connect(ip);
using (NetworkStream n = new NetworkStream(s))
{
if (HttpRequestType == "POST")
{
//
}
bool saveItAtCache = false;
bool existsAtCache = false;
byte[] cachedFile = null;
string ext = Path.GetExtension(_url).ToLower();
if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
{
saveItAtCache = true;
cachedFile = cache.GetFile(_url);
existsAtCache = (cachedFile != null);
}
if (existsAtCache)
{
writeSuccess(cachedFile.Length, null);
socket.Send(cachedFile);
}
else
{
SendRequest(n, new[] { HttpQuery });
Dictionary<string, string> headers = new Dictionary<string, string>();
while (true)
{
string line = ReadLine(n);
if (Equals(line.Length, 0))
{
break;
}
int index = line.IndexOf(':');
if (!headers.ContainsKey(line.Substring(0, index)))
{
headers.Add(line.Substring(0, index), line.Substring(index + 2));
}
}
string str = "";
string contentEncoding;
if (headers.TryGetValue("Content-Encoding", out contentEncoding))
{
Stream responseStream = n;
if (contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
}
else if (contentEncoding.Equals("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
var memStream = new MemoryStream();
var respBuffer = new byte[4096];
try
{
int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
while (bytesRead > 0)
{
memStream.Write(respBuffer, 0, bytesRead);
bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
}
}
finally
{
responseStream.Close();
}
str = encoding.GetString(memStream.ToArray());
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
cachedFile = encoding.GetBytes(str);
if (saveItAtCache)
{
cache.Store(_url, cachedFile);
}
num1 = str.Length;
}
else
{
while (true)
{
string line = ReadLine(n);
if (line == null)
{
str.Remove(str.Length - 1, 1);
break;
}
num1 += line.Length;
str += line;
}
}
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
byte[] buffer3 = encoding.GetBytes(str);
writeSuccess(buffer3.Length, _headers["Set-Cookie"]);
socket.Send(buffer3);
}
}
}
return num1;
}
void SendRequest(Stream stream, IEnumerable<string> request)
{
if (request != null)
{
foreach (var r in request)
{
byte[] data = encoding.GetBytes(r);
stream.Write(data, 0, data.Length);
stream.Write(LineTerminator, 0, 2);
}
stream.Write(LineTerminator, 0, 2);
// Eat response
string response = ReadLine(stream);
Console.WriteLine(response);
}
}
string ReadLine(Stream stream)
{
List<byte> lineBuffer = new List<byte>();
while (true)
{
try
{
int b = stream.ReadByte();
if (b == -1)
{
return null;
}
if (b == 10)
break;
if (b!=13)
lineBuffer.Add((byte)b);
}
catch (Exception)
{
return null;
}
}
return encoding.GetString(lineBuffer.ToArray());
}
-
February 16th, 2010, 03:40 PM
#2
Re: how corectly unzip GZipStream
you've forgotten the code tags... I'm not going to read it.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 16th, 2010, 05:19 PM
#3
Re: how corectly unzip GZipStream
Using zlib, I wrote the following code to decompress a gzip compressed byte[] array msgData. Used it for my soulseek client, worked great. Maybe it's of use for you. You can google zlib i assume.
Code:
public bool decompress()
{
System.IO.MemoryStream instream = new System.IO.MemoryStream(msgData);
System.IO.MemoryStream outstream = new System.IO.MemoryStream();
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outstream);
try
{
CopyStream(instream, outZStream);
}
catch (Exception e)
{
return false;
}
finally
{
instream.Close();
}
if (outstream.Length > 0)
{
outstream.Position = 0;
BinaryReader reader = new BinaryReader(outstream);
msgData = reader.ReadBytes((int)outstream.Length);
return true;
}
return false;
}
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}
-
February 16th, 2010, 07:26 PM
#4
Re: how corectly unzip GZipStream
So , do you still have a problem?
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 17th, 2010, 05:06 AM
#5
Re: how corectly unzip GZipStream
 Originally Posted by Kakoon
Using zlib, I wrote the following code to decompress a gzip compressed byte[] array msgData. Used it for my soulseek client, worked great. Maybe it's of use for you. You can google zlib i assume.
Code:
public bool decompress()
{
System.IO.MemoryStream instream = new System.IO.MemoryStream(msgData);
System.IO.MemoryStream outstream = new System.IO.MemoryStream();
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outstream);
try
{
CopyStream(instream, outZStream);
}
catch (Exception e)
{
return false;
}
finally
{
instream.Close();
}
if (outstream.Length > 0)
{
outstream.Position = 0;
BinaryReader reader = new BinaryReader(outstream);
msgData = reader.ReadBytes((int)outstream.Length);
return true;
}
return false;
}
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}
This method maybe true, but i have exception: inflating: unknown compression method
-
February 17th, 2010, 05:08 AM
#6
Re: how corectly unzip GZipStream
 Originally Posted by rliq
So , do you still have a problem?
Yes, I stiil still have problem with this...any ideas?
-
February 17th, 2010, 10:54 AM
#7
Re: how corectly unzip GZipStream
 Originally Posted by IICuX
Yes, I stiil still have problem with this...any ideas?
what problem exacly? still the InvalidDataException or something new now?
I wanted to help but apparently you won't me/us to help you... I told you that I won't read your post until you edit it and add the code tags. and I'm not sure whether I'm still willing.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 17th, 2010, 10:57 AM
#8
Re: how corectly unzip GZipStream
 Originally Posted by memeloo
what problem exacly? still the InvalidDataException or something new now?
I wanted to help but apparently you won't me/us to help you... I told you that I won't read your post until you edit it and add the code tags. and I'm not sure whether I'm still willing.
I have problem, writen by me in subj, InvalidDataException....
-
February 17th, 2010, 01:35 PM
#9
Re: how corectly unzip GZipStream
Code:
public long WriteURL()
{
long num1 = 0;
string host = new Uri(_url).Host;
IPHostEntry ipAddress = Dns.GetHostEntry(host);
IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
s.Connect(ip);
using (NetworkStream n = new NetworkStream(s))
{
if (HttpRequestType == "POST")
{
//
}
bool saveItAtCache = false;
bool existsAtCache = false;
byte[] cachedFile = null;
string ext = Path.GetExtension(_url).ToLower();
if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
{
saveItAtCache = true;
cachedFile = cache.GetFile(_url);
existsAtCache = (cachedFile != null);
}
if (existsAtCache)
{
writeSuccess(cachedFile.Length, null);
socket.Send(cachedFile);
}
else
{
SendRequest(n, new[] { HttpQuery });
Dictionary<string, string> headers = new Dictionary<string, string>();
while (true)
{
string line = ReadLine(n);
if (Equals(line.Length, 0))
{
break;
}
int index = line.IndexOf(':');
if (!headers.ContainsKey(line.Substring(0, index)))
{
headers.Add(line.Substring(0, index), line.Substring(index + 2));
}
}
string str = "";
string contentEncoding;
if (headers.TryGetValue("Content-Encoding", out contentEncoding))
{
Stream responseStream = n;
if (contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
}
else if (contentEncoding.Equals("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
var memStream = new MemoryStream();
var respBuffer = new byte[4096];
try
{
int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
while (bytesRead > 0)
{
memStream.Write(respBuffer, 0, bytesRead);
bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
}
}
finally
{
responseStream.Close();
}
str = encoding.GetString(memStream.ToArray());
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
cachedFile = encoding.GetBytes(str);
if (saveItAtCache)
{
cache.Store(_url, cachedFile);
}
num1 = str.Length;
}
else
{
while (true)
{
string line = ReadLine(n);
if (line == null)
{
str.Remove(str.Length - 1, 1);
break;
}
num1 += line.Length;
str += line;
}
}
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
byte[] buffer3 = encoding.GetBytes(str);
writeSuccess(buffer3.Length, _headers["Set-Cookie"]);
socket.Send(buffer3);
}
}
}
return num1;
}
void SendRequest(Stream stream, IEnumerable<string> request)
{
if (request != null)
{
foreach (var r in request)
{
byte[] data = encoding.GetBytes(r);
stream.Write(data, 0, data.Length);
stream.Write(LineTerminator, 0, 2);
}
stream.Write(LineTerminator, 0, 2);
// Eat response
string response = ReadLine(stream);
Console.WriteLine(response);
}
}
string ReadLine(Stream stream)
{
List<byte> lineBuffer = new List<byte>();
while (true)
{
try
{
int b = stream.ReadByte();
if (b == -1)
{
return null;
}
if (b == 10)
break;
if (b!=13)
lineBuffer.Add((byte)b);
}
catch (Exception)
{
return null;
}
}
return encoding.GetString(lineBuffer.ToArray());
}
-
February 17th, 2010, 02:05 PM
#10
Re: how corectly unzip GZipStream
I can't see anything strange in your code. is the stream that you try to decompress really a stream compressed with gzipstream and not a zip file? because gzipstream does not support multiple files.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 17th, 2010, 02:45 PM
#11
Re: how corectly unzip GZipStream
i have exists code but using HttpWebResponse and WebRequest, there is all good unpack and decompress, with any extenitions, but using sockets and NetworkStream i can't get Response data...
-
February 17th, 2010, 05:58 PM
#12
Re: how corectly unzip GZipStream
-
February 18th, 2010, 12:03 AM
#13
Re: how corectly unzip GZipStream
 Originally Posted by IICuX
any can help me ?
do you think we're some kind of answering machines? there is no rule that anyone have to give you an answer within 5minutes. be patient and kind!
I suggest you save the source gzipstream into a file on the server as a copy then save the received stream on the client side into another file wihtout decompressing it and check if they are the same.
Last edited by memeloo; February 18th, 2010 at 01:22 AM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 18th, 2010, 10:30 AM
#14
Re: how corectly unzip GZipStream
 Originally Posted by memeloo
do you think we're some kind of answering machines?  there is no rule that anyone have to give you an answer within 5minutes. be patient and kind!
I suggest you save the source gzipstream into a file on the server as a copy then save the received stream on the client side into another file wihtout decompressing it and check if they are the same.
I don't have acces to server, i must do this without server, only client side
-
February 19th, 2010, 06:30 PM
#15
Re: how corectly unzip GZipStream
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|