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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:56:45+00:00 2026-06-16T03:56:45+00:00

so we’re on the same page: i’m using NSData+GZIP , JSONKit, and NSData+Base64 my

  • 0

so we’re on the same page: i’m using NSData+GZIP, JSONKit, and NSData+Base64

my goal here, as the title suggests, is to take a json object, compress it, base64 encode it, and send it up to a .net server as a form post which in turn expects to take this data and reverse the process and end with a json object.

i need to do this on both android and iOS. android is done, it works well. for reference, here is what i’m doing there:

public String compress(String string) throws IOException {
    byte[] blockcopy = ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN).putInt(string.length()).array();
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());
    gos.close();
    os.close();
    byte[] compressed = new byte[4 + os.toByteArray().length];
    System.arraycopy(blockcopy, 0, compressed, 0, 4);
    System.arraycopy(os.toByteArray(), 0, compressed, 4, os.toByteArray().length);

    StringBuffer hexString = new StringBuffer();
    for (int i=0; i<compressed.length; i++)
        hexString.append(Integer.toHexString(0xFF & compressed[i]));

    return Base64.encodeBytes(compressed);
}

here is a test on iOS that succeeds:

    NSLog(@"test? %@", [[NSString alloc]initWithData:[[NSData dataFromBase64String:[[[@"test" dataUsingEncoding:NSUTF8StringEncoding] gzippedData] base64EncodedString]] gunzippedData] encoding:NSUTF8StringEncoding]);

this prints:

"test? test"

the area of concern:

    NSStringEncoding encoding = NSUTF8StringEncoding;

    NSString* string = [[NSArray arrayWithArray:mutableData] JSONString];
    NSData* data = [string dataUsingEncoding:encoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/myendpoint.aspx?deviceid=%@&accesstoken=%@", [defaults objectForKey:@"deviceId"], [defaults objectForKey:@"ggAccessToken"]]]];
    [request setHTTPMethod:@"POST"];
    NSMutableData *requestBodyData = nil;

    requestBodyData = [NSMutableData data];

    // Create boundry
    NSString *boundary = [NSString stringWithFormat:@"--%@--", [[NSProcessInfo processInfo] globallyUniqueString]];
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
    NSData *boundaryData = [[[@"--" stringByAppendingString:boundary] stringByAppendingString:@"\r\n"] dataUsingEncoding:encoding];

    // Add Data to POST
    [requestBodyData appendData:boundaryData];
    [requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; "] dataUsingEncoding:encoding]];
    [requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", @"contacts"] dataUsingEncoding:encoding]];

    [requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];

    NSString* finalString = [[data gzipDeflate] base64EncodedString];
    //an example of finalString at this point: H4sIAAAAAAAAA6WOwQrCMAyGX0VyUqjHOtk7CN5HGekWMdB2knansXc3FcSDKIiHhBC+5P+6BWYJGdrOGbhdp0R1XiCgpwAtnCbPgcBAmqMn0c3WWrvbaNtrWVjNC+ZzffARPsCqIRSRn4EXllz6hJGULcK5YNJzHEehnB8qrgb8o9j8onj8rjigCNObobsD+2WV/kYBAAA=

    NSData* finalData = [finalString dataUsingEncoding:NSUTF8StringEncoding];

    [requestBodyData appendData:finalData];
    [requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
    [requestBodyData appendData:boundaryData];

    [request setHTTPBody:requestBodyData];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSLog(@"%@", [data objectFromJSONData]);
    }];

all seems well at this point, but the result from the (.net)server:

ERROR TYPE: System.IO.InvalidDataException - ERROR MESSAGE: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. -

i realize there isn’t much of a question here just yet, but heres something of one:
what could i be doing at this point that would yield incorrectly compressed data? (lets assume at this time at all is correct server-side given that android is fully functional and the guy who built that side of it is pretty damn experienced)…

  • 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-06-16T03:56:47+00:00Added an answer on June 16, 2026 at 3:56 am

    There are 2 ways to use gzip, as a stream, or as a data file. The compression itself doesn’t require a header, but when saved to a file, it is standard to include the header and footer bytes as defined in the RFC.

    So the problem is one library is looking for the header, while the other library that wrote the data didn’t include it, it’s probably just a raw stream of gzip data. It can be decompressed, but the library needs to know to not look for the headers.

    When saving to a file, there are additional benefits of the file format, mainly due to the CRC check that is included at the end of the file to help verify that the data integrity of the file has not been corrupted.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am using jsonparser to parse data and images obtained from json response. When
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.