CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2010
    Posts
    10

    Question 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());
    }

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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

  3. #3
    Join Date
    Feb 2010
    Posts
    7

    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();
        }

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: how corectly unzip GZipStream

    So , do you still have a problem?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    Quote Originally Posted by Kakoon View Post
    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

  6. #6
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    Quote Originally Posted by rliq View Post
    So , do you still have a problem?
    Yes, I stiil still have problem with this...any ideas?

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: how corectly unzip GZipStream

    Quote Originally Posted by IICuX View Post
    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

  8. #8
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    Quote Originally Posted by memeloo View Post
    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....

  9. #9
    Join Date
    Feb 2010
    Posts
    10

    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());
            }

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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

  11. #11
    Join Date
    Feb 2010
    Posts
    10

    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...

  12. #12
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    any can help me ?

  13. #13
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: how corectly unzip GZipStream

    Quote Originally Posted by IICuX View Post
    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

  14. #14
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    Quote Originally Posted by memeloo View Post
    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

  15. #15
    Join Date
    Feb 2010
    Posts
    10

    Re: how corectly unzip GZipStream

    any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured