Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Chris_E · May 16, 2016 at 06:33 PM · wwwdownloadbytesdecompress

Difference in downloaded www.byte.length on iOS vs PC

I have a gzip file that I'm pulling from the web. On PC everything works great, but on iOS it comes out wrong, the file that's decompressed is JSON and won't parse. (I tried caching the file downloaded onto PC, then running it through the same function passing TextAsset.bytes instead of the WWW.bytes and it unzips and parses correctly on iOS.) The www.byte.length on PC is always 74616 across two different PCs, every time. On iOS the size is 74496. I can't think of anything that could cause this change. I've confirmed the exact same form data is being sent on both platforms, so the return should be the exact same.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IMD · Aug 31, 2016 at 02:31 PM 0
Share

Any progress on this one Chris_E? I get the same thing between $$anonymous$$acOS and PC, although the bytes.length from the same url is almost twice as long on $$anonymous$$acOS.... o_O

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IMD · Aug 31, 2016 at 05:29 PM

Quite an epic battle :)

It's been quite the dragon. It seems that Unity on Mac with WWW will automatically decompresses the bytes in a WWW.bytes array on download, where on Windows Unity, the bytes are not automatically decompressed.

I have now successfully made a solution which required me to check if the bytes were compressed and if the bytesDownloaded is not the same as the returned content length, then to assume they have not be decompressed and so manually decompress.

To do the decompression I downloaded the very nice gzip compression library DotNetZipLib and use the Ionic.Zlib.dll in the plugins folder. The native .net decompression librarys have been disabled in Unity for various reasons. This library can be downloaded from codeplex here.

The code I wrote to check for compression and return the actual raw bytes is :

 protected byte[] GetRawBytes(WWW w) {
 
             string encoding = "";
             if (w.responseHeaders.TryGetValue("Content-Encoding", out encoding)) {
                 string length = "";
                 if (w.responseHeaders.TryGetValue("Content-Length", out length)) {
                     if (w.bytesDownloaded.ToString() != length) {
                         byte[] decompressedBytes = CompressionIO.Decompress_Gzip(w.bytes);
                         return decompressedBytes;
                     }
                 }
             }
 
             return w.bytes;
 
         }

The CompressionIO class is:

 using System;
 using System.IO;
 //using System.IO.Compression;
 using Ionic.Zlib;
 
 namespace MantleEngine.IO {
 
 
     public class CompressionIO  {
         
             
 
             public static byte[] Decompress_Gzip(byte[] gzip)
             {
                 // Create a GZIP stream with decompression mode.
                 // ... Then create a buffer and write into while reading from the GZIP stream.
                 using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
                 {
                     const int size = 4096;
                     byte[] buffer = new byte[size];
                     using (MemoryStream memory = new MemoryStream())
                     {
                         int count = 0;
                         do
                         {
                             count = stream.Read(buffer, 0, size);
                             if (count > 0)
                             {
                                 memory.Write(buffer, 0, count);
                             }
                         }
                         while (count > 0);
                         return memory.ToArray();
                     }
                 }
             }
 
             static void testexample()
             {
                 // Open a compressed file on disk.
                 // ... Then decompress it with the method below.
                 // ... Then write the length of each array.
                 byte[] file = File.ReadAllBytes("C:\\perlgzips\\~stat.gz");
                 byte[] decompressed = Decompress_Gzip(file);
                 Console.WriteLine(file.Length);
                 Console.WriteLine(decompressed.Length);
             }
 
 
 
         }
 
 
     }


I hope this helps someone and saves them similar pains.

Cheers,

Isaac

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Downloading image from the server crash the game in Android Device ? 1 Answer

Is it possible to download a youtube video from unity 3 Answers

Why does nothing happen after yield return www 0 Answers

WWW Downloaded file 1 Answer

download a file 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges