Download file from Google Drive via rest api using gzip compression

白昼怎懂夜的黑 提交于 2019-12-23 03:49:13

问题


I'm using the following code to download a publicly shared file from Google drive. It works fine.

InputStream input = null;
OutputStream output = null;

HttpURLConnection httpsURLConnectionToGoogleDrive = (HttpURLConnection) new URL(downloadUrl).openConnection();
httpsURLConnectionToGoogleDrive.connect();

long fileLength = httpsURLConnectionToGoogleDrive.getContentLength();
input = httpsURLConnectionToGoogleDrive.getInputStream();

byte data[] = new byte[MediaHttpUploader.MINIMUM_CHUNK_SIZE];
int count;
while ((count = input.read(data)) != -1) {
    // allow canceling with back button
    if (isCancelled()) {
        input.close();
        return null;
    }
    total += count;
    log("Received Up To : "+ total + " ("+ count +") ");
    // publishing the progress....
    if (fileLength > 0) { // only if total length is known
        publishProgress((int) (total * 100 / fileLength));
    }
    output.write(data, 0, count);
}

Now, I want to add gzip compression to save bandwidth. From this reference I added the following code :-

httpsURLConnectionToGoogleDrive.setRequestProperty("Accept-Encoding", "gzip");
httpsURLConnectionToGoogleDrive.setRequestProperty("User-Agent", "my program (gzip)");

But I could't get it to work. I'm stuck here. What to do?


回答1:


The Drive API already manages the requests as gzip as default, so there is no extra effort from your side. Only if you were to create you're own library, then you should specify the headers for gzip.



来源:https://stackoverflow.com/questions/32004933/download-file-from-google-drive-via-rest-api-using-gzip-compression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!