Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:52+00:00 2026-05-26T23:43:52+00:00

I am trying to replicate the php function gzuncompress in C# So far I

  • 0

I am trying to replicate the php function gzuncompress in C#
So far I got part of following code working. see comment and code below.

I thing the tricky bit is happening during byte[] and string convertion.
How can I fix this? and where did I missed??

I am using .Net 3.5 environment

        var plaintext = Console.ReadLine();
        Console.WriteLine("string to byte[] then to string");
        byte[] buff = Encoding.UTF8.GetBytes(plaintext);

        var compress = GZip.GZipCompress(buff);
        //Uncompress working below
        try
        {
            var unpressFromByte = GZip.GZipUncompress(compress);
            Console.WriteLine("uncompress successful by uncompress byte[]");
        }catch
        {
            Console.WriteLine("uncompress failed by uncompress byte[]");
        }

        var compressString = Encoding.UTF8.GetString(compress);
        Console.WriteLine(compressString);
        var compressBuff = Encoding.UTF8.GetBytes(compressString);
        Console.WriteLine(Encoding.UTF8.GetString(compressBuff));
        //Uncompress not working below by using string
        //The magic number in GZip header is not correct
        try
        {
            var uncompressFromString = GZip.GZipUncompress(compressBuff);
            Console.WriteLine("uncompress successful by uncompress string");
        }
        catch
        {
            Console.WriteLine("uncompress failed by uncompress string");
        }

code for class Gzip

public static class GZip
      {
        public static byte[] GZipUncompress(byte[] data)
        {
            using (var input = new MemoryStream(data))
            using (var gzip = new GZipStream(input, CompressionMode.Decompress))
            using (var output = new MemoryStream())
            {
                gzip.CopyTo(output);
                return output.ToArray();
            }
        }
        public static byte[] GZipCompress(byte[] data)
        {
            using (var input = new MemoryStream(data))
            using (var output = new MemoryStream())
            {
                using (var gzip = new GZipStream(output, CompressionMode.Compress, true))
                {
                    input.CopyTo(gzip);
                }
                return output.ToArray();
            }
        }

        public static long CopyTo(this Stream source, Stream destination)
        {
            var buffer = new byte[2048];
            int bytesRead;
            long totalBytes = 0;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                destination.Write(buffer, 0, bytesRead);
                totalBytes += bytesRead;
            }
            return totalBytes;
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:43:53+00:00Added an answer on May 26, 2026 at 11:43 pm

    This is inappropriate:

    var compressString = Encoding.UTF8.GetString(compress);
    

    compress isn’t a UTF-8-encoded piece of text. You should treat it as arbitrary binary data – which isn’t appropriate to pass into Encoding.GetString. If you really need to convert arbitrary binary data into text, use Convert.ToBase64String (and then reverse with Convert.FromBase64String):

    var compressString = Convert.ToBase64String(compress);
    Console.WriteLine(compressString);
    var compressBuff = Convert.FromBase64String(compressString);
    

    That may or may not match what PHP does, but it’s a safe way of representing arbitrary binary data as text, unlike treating the binary data as if it were valid UTF-8-encoded text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to replicate the following in php although i am stuck at Encoding.ASCII.GetBytes(paramList)
I'm trying the replicate the imagecopy PHP function in .NET. I have two images
Basically I'm trying to replicate YouTube's ability to begin video playback from any part
I'm trying to replicate a template I've used before with a member function, and
I am trying to replicate a forum function by getting the last reply of
I've done this before once, I'm trying to replicate what I did so far
I am trying to replicate the applet found here as a part of an
I'm trying to replicate CI's humanize() and underscore() function in Javascript. From the CI
Trying to replicate the following query using Zend_Db_Select . Any pointers? SELECT compounds.id as
I'm trying to create a chaining function for working with strings that are returned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.