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 6797081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:33:53+00:00 2026-05-26T18:33:53+00:00

Background I’m setting up a generic handler to: Combine & compress Javascript and CSS

  • 0

Background

I’m setting up a generic handler to:

  • Combine & compress Javascript and CSS files
  • Cache a GZip version & a Non-GZip version
  • Serve the appropriate version based on the request

I’m working in MonoDevelop v2.8.2 on OSX 10.7.2

Problem

Since I want to Cache the GZipped version, I need to GZip without using a response filter

Using this code, I can compress and decompress a string on the server successfully, but when I serve it to the client I get:

  • Error 330 (net::ERR_CONTENT_DECODING_FAILED): Unknown error. (Chrome)
  • Cannot decode raw data (Safari)
  • The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression. (Firefox)

Relevant Code

string sCompiled =null;
if(bCanGZip)
{
    context.Response.AddHeader("Content-Encoding", "gzip");
    bHasValue = CurrentCache.CompiledScripts.TryGetValue(context.Request.Url.ToString() + "GZIP", out sCompiled);
}

//...
//Process files if bHasVale is false
//Compress result of file concatination/minification

//Compression method
public static string CompressString(string text)
{
    UTF8Encoding encoding = new UTF8Encoding(false);

    byte[] buffer = encoding.GetBytes(text);
    using(MemoryStream memoryStream = new MemoryStream()){
        using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
        {
            gZipStream.Write(buffer, 0, buffer.Length);

        }
        memoryStream.Position = 0;

        byte[] compressedData = new byte[memoryStream.Length];
        memoryStream.Read(compressedData, 0, compressedData.Length);

        byte[] gZipBuffer = new byte[compressedData.Length + 4];
        Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
        return Convert.ToBase64String(gZipBuffer);

    }

}

//...
//Return value
switch(Type){
    case FileType.CSS:
        context.Response.ContentType = "text/css";  
        break;
    case FileType.JS:
        context.Response.ContentType = "application/javascript"; 
        break;
}
context.Response.AddHeader("Content-Length", sCompiled.Length.ToString());
context.Response.Clear();

context.Response.Write(sCompiled);  

Attempts to Resolve

Since I’m not sure what the lines:

byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);

are accomplishing, I tried removing them.

I tried playing with different Encodings/options.

At this point I’m really not sure how to attack the problem since I don’t know the source of the error (Encoding/Compression/other).

Any help would be very appreciated!

Other Resources I’ve found on the subject

  • http://beta.blogs.microsoft.co.il/blogs/mneiter/archive/2009/03/24/how-to-compress-and-decompress-using-gzipstream-object.aspx
  • http://madskristensen.net/post/Compress-and-decompress-strings-in-C.aspx
  • http://www.codeproject.com/KB/files/GZipStream.aspx
  • http://www.codeproject.com/KB/aspnet/HttpCombine.aspx
  • http://webreflection.blogspot.com/2009/01/quick-tip-c-gzip-content.html
  • http://www.dominicpettifer.co.uk/Blog/17/gzip-compress-your-websites-html-css-script-in-code
  • 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-26T18:33:54+00:00Added an answer on May 26, 2026 at 6:33 pm

    This is one of those things where once you explain you problem, you quickly find the answer.

    I need to write out the response as Binary. So modifying the compression algorithum to return a byte array:

    public static byte[] CompressStringToArray(string text){
        UTF8Encoding encoding = new UTF8Encoding(false);
    
        byte[] buffer = encoding.GetBytes(text);
        using(MemoryStream memoryStream = new MemoryStream()){
            using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
    
            }
            memoryStream.Position = 0;
    
            byte[] compressedData = new byte[memoryStream.Length];
            memoryStream.Read(compressedData, 0, compressedData.Length);
    
            return compressedData;
        }
    }
    

    and then calling:

    //Writes a byte buffer without encoding the response stream
    context.Response.BinaryWrite(GZipTools.CompressStringToArray(sCompiled));
    

    Solves the issue. Hopefully this helps others who will face the same problem.

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

Sidebar

Related Questions

BACKGROUND: I'm writing a single level cache simulator in C for a homework assignment,
Background: writing an automated release script to export changed files between versions from SVN
Background I have a secured folder containing secret report files (in pdf format). Each
Background: I have a servlet in which I am dynamically generating javascript and putting
Background: I have a little video playing app with a UI inspired by the
Background: At my company we are developing a bunch applications that are using the
Background: Some time ago, I built a system for recording and categorizing application crashes
Background: I need to reserve an amount of memory below 0xA0000 prior to my
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background I have a massive db for a SharePoint site collection. It is 130GB

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.