0

I am trying to improve performance for my application by downloading google drive files in compressed format. I am using this as reference.

https://developers.google.com/drive/api/v2/performance#gzip

I tried various things in the header of the HttpwebRequest I am sending , but couldnt achieve success. Can anyone please help me in this regard. This is the code I am using.

    public void DownloadFile(string url, string filename)
    {
        try
        {
            var tstart = DateTime.Now;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Timeout = 60 * 1000; // Timeout
            request.Headers.Add("Authorization", "Bearer" + " " + AuthenticationKey);                
            request.Headers.Add("Accept-Encoding", "gzip,deflate");
            request.UserAgent = "MyApplication/11.14 (gzip)";                

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string content = response.GetResponseHeader("content-disposition");
            Console.WriteLine(content);

            System.IO.Stream received = response.GetResponseStream();
            using (System.IO.FileStream file = new System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                received.CopyTo(file);
            }

            var tend = DateTime.Now;
            Console.WriteLine("time taken to download '{0}' is {1} seconds", filename, (tend - tstart).TotalSeconds);
        }
        catch (WebException e)
        {
            Console.WriteLine("Exception thrown - {0}", e.Message);
        }
    }
  • what is your error? – Linda Lawton - DaImTo Jul 20 '18 at 06:41
  • @DalmTo There is no error , but I was expecting a compressed file here instead of regular file hoping for some improvement in download speed. As you replied with the answer, i guess file data is not compressed. Do you have any idea if something like this would be supported in future ? Are there any other better ways to improve download speed ? – Saurabh Agrawal Jul 20 '18 at 09:44
  • Not when downloading the file itself. It downloads at the speed it downloads at this will depend upon your connection, the time of day, and the server you manage to connect to at googles datacenter. – Linda Lawton - DaImTo Jul 20 '18 at 10:03

1 Answers1

0

Using gzip

An easy and convenient way to reduce the bandwidth needed for each request is to enable gzip compression. Although this requires additional CPU time to uncompress the results, the trade-off with network costs usually makes it very worthwhile.

In order to receive a gzip-encoded response you must do two things: Set an Accept-Encoding header, and modify your user agent to contain the string gzip. Here is an example of properly formed HTTP headers for enabling gzip compression:

Accept-Encoding: gzip
User-Agent: my program (gzip)

Gzip compress the actual response from the API. Not the actual file you are downloading. For example file.export returns a file.resource json object this object would be compressed. Not the actual data of the file you will need to download. Files are downloaded in their corresponding type manage downloads While google may be able to convert a google doc file to a ms word file when you download it. It is not going to covert a google doc file to a zip file so that you can download it zipped.

Community
  • 1
  • 1
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • @DalmTo Thanks for the response . I replied on above question. It would be good if can mention it on the documentation to make it more clear. :) – Saurabh Agrawal Jul 20 '18 at 09:46
  • 1
    Well it is clear as its not mentioned in the documentation that it would zip the file itself only the response object. Google doesn't normally document what they don't support if they did there would be no end to the documentation. – Linda Lawton - DaImTo Jul 20 '18 at 09:58