获取uncom pressed gzip文件的大小,而COM pressed文件的大小可从服务器 [英] Get size of uncompressed gzip file while size of compressed file is available from server

查看:250
本文介绍了获取uncom pressed gzip文件的大小,而COM pressed文件的大小可从服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GZIPInputStream 下载PDF文件。我想显示在UI按钮的文件的下载进度。但是,我没有得到文件的实际大小,我所得到的原因是COM pressed大小对此我无法显示正确的下载进度。此下载进度超过 100 作为实际的文件大小比文件的COM pressed大小。

从服务器文件的标题内容:继信息,我从服务器接收,从我使用内容长度这是给COM pressed文件大小

  1.Connection
2.Content-编码
3.Content长
4.Content型
5.保持活
6.Server
7.Date

下面是我的code。有没有什么办法让文件的原始大小?

 长文件长度= HTT presponse.getEntity()getContentLength(); //
GZIPInputStream输入=新GZIPInputStream(新的BufferedInputStream(
        HTT presponse.getEntity()的getContent()));
FileOutputStream中输出=新的FileOutputStream(destinationFilePath);字节的数据[] =新的字节[1024];
总长= 0;
浮动比例= 0;
诠释计数;
currentDownloadingPercentage = 0;
而((计数= input.read(数据))!= - 1){
    总+ =计数;
    output.write(数据0,计);    //发布进度....
    百分比=(浮点)总/(浮点)文件长度;
    百分比* = 100;
    如果((int)的百分比>(INT)currentDownloadingPercentage){
        currentDownloadingPercentage =百分比;
        捆绑resultData =新包();
        resultData.putBoolean(DOWNLOAD_FAILED,FALSE);
        resultData.putInt(DOWNLOAD_PROGRESS,(INT)百分比);
        receiver.send(进程ID,resultData);
        resultData = NULL;
    }
}


解决方案

此的问题讨论结果似乎没有办法避免HttpURLConnection.getInputStream()自动返回GZIPInputStream,一旦你让HttpURLConnection的接受gzip的COM pression,你将无法准确计算下载进度,唯一的一个我们能做的只是禁用gzip的可接受编码:

  HttpURLConnection.setRequestProperty(接受编码,身份);

另一种选择是使用 AndroidHttpClient ,我对测试这一点,连我们present接受gzip编码是这样的:

  HttpUriRequest.addHeader(接受编码,gzip的);

的InputStream实例,通过的Htt presponse.getEntity()返回。的getContent() EofSensorInputStream ,一个原始的InputStream就是我们想要的,不是吗 ŧGZIPInputStream,使我们可以通过自己包装为GZIPInputStream,我们可以使用TeeInputStream和CountingOutputStream才能完成计算下载进度。

 的Htt presponse响应= ...;
HttpEntity实体= response.getEntity();
长档案大小= entity.getContentLength();InputStream的插件= entity.getContent(); // EofSensorInputStream实例
CountingOutputStream coStrem =新CountingOutputStream(新ByteArrayOutputStream(100));
GZIPInputStream inStrem =新GZIPInputStream(新TeeInputStream(INS,coStrem,真实));字节[]缓冲区=新的字节[6 * 1024]; // 6K缓冲
INT抵消;而((偏移= inStrem.read(缓冲液))!= - 1){
    tmpFileRaf.write(缓冲液,0,偏移);
    postDownloadProgress(档案大小,coStrem.getByteCount());
}

我想这就是我们可以用这个问题呢,我试着拿起<一个href=\"http://iamkoch.com/2013/11/how-to-get-libcore-sources-for-httpurlconnectionimpl-java-and-other-classes-missing-from-the-base-android-sdks/\"相对=nofollow>安卓libcore 与我的项目源,以便我们可以自定义的 HttpURLConnectionImpl ,然后燮preSS其返回GZIPInputStream,但很多失误使得麻烦,我放弃这方面的努力。

在此帖子,杰西·威尔逊建议我们Android的最好选择客户端是HttpURLConnection的,所以我在寻找如何始终解决这个问题,我希望我能很快得到的方式。

I am using GZIPInputStream to download PDF file. I want to show the download progress of the file on a UI button. But, I am not getting the actual size of the file, what I am getting is compressed size due to which I am unable to show the correct download progress. This download progress is exceeding 100 as the actual file size is greater than the compressed size of file.

Header content of file from server: Following info I receive from server, from which I am using content-length which is giving compressed file size.

1.Connection
2.Content-Encoding
3.Content-length
4.Content-Type
5.Keep-Alive
6.Server
7.Date

Here is my code. Is there any way to get original size of file?

long fileLength =  httpResponse.getEntity().getContentLength();//
GZIPInputStream input = new GZIPInputStream(new BufferedInputStream(
        httpResponse.getEntity().getContent()));
FileOutputStream output = new FileOutputStream(destinationFilePath);

byte data[] = new byte[1024];
long total = 0;
float percentage = 0;
int count;
currentDownloadingPercentage=0;
while ((count = input.read(data)) != -1) {
    total += count;
    output.write(data, 0, count);

    // publishing the progress....
    percentage = (float)total/(float)fileLength;
    percentage *= 100;
    if ((int)percentage > (int)currentDownloadingPercentage) {
        currentDownloadingPercentage = percentage;
        Bundle resultData = new Bundle();
        resultData.putBoolean(DOWNLOAD_FAILED, false);
        resultData.putInt(DOWNLOAD_PROGRESS ,(int)percentage);
        receiver.send(processID, resultData);
        resultData = null;  
    }
}

解决方案

This issue discuss result seems not way to avoid HttpURLConnection.getInputStream() automatically returned GZIPInputStream, once you let HttpURLConnection accept gzip compression, you won't calculate download progress accurately, the only one we can do just disable gzip as acceptable encoding :

HttpURLConnection.setRequestProperty("Accept-Encoding", "identity");

Another choice is use AndroidHttpClient, I had tested about this, even we present accept gzip encoding like this :

HttpUriRequest.addHeader("Accept-Encoding", "gzip");

the InputStream instance that return by HttpResponse.getEntity().getContent() will be EofSensorInputStream, an original InputStream is what we wanted, isn't GZIPInputStream, that make us possible to wrap it to GZIPInputStream by myself, we can use TeeInputStream and CountingOutputStream to finish calculating download progress.

HttpResponse response = ...;
HttpEntity entity = response.getEntity();
long fileSize = entity.getContentLength();

InputStream ins = entity.getContent(); // instance of EofSensorInputStream
CountingOutputStream coStrem = new CountingOutputStream(new ByteArrayOutputStream(100));
GZIPInputStream inStrem = new GZIPInputStream(new TeeInputStream(ins, coStrem, true));

byte[] buffer = new byte[6 * 1024]; // 6K buffer
int offset;

while ((offset = inStrem.read(buffer)) != -1) {
    tmpFileRaf.write(buffer, 0, offset);
    postDownloadProgress(fileSize, coStrem.getByteCount());
}

I think that's all we can do with this problem, I tried pick up android libcore source with my project so we can customize HttpURLConnectionImpl then suppress it return GZIPInputStream, but many errors makes trouble, I discard this effort.

In this post, Jesse Wilson suggested we the best choice client of Android is HttpURLConnection, so I'm looking for how to solve this problem always, I hope I can get a way soon.

这篇关于获取uncom pressed gzip文件的大小,而COM pressed文件的大小可从服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆