GZIPInputStream
을 사용하여 PDF 파일을 다운로드하고 있습니다. 파일의 다운로드 진행 상황을 UI 단추에 표시하고 싶습니다. 하지만, 파일의 실제 크기를 얻지 못하고 있는데, 압축 크기 때문에 올바른 다운로드 진행 상태를 표시 할 수 없습니다. 실제 파일 크기가 압축 된 파일 크기보다 커서이 다운로드 진행률은 100
을 초과합니다.압축 된 파일의 크기를 서버에서 가져올 수있는 비 압축 gzip 파일의 크기를 확인하십시오.
서버에서 보낸 파일의 헤더 내용 : 다음 정보 내가 압축 된 파일 크기를 제공하는 content-length
을 사용중인 서버로부터 수신했습니다.
1.Connection
2.Content-Encoding
3.Content-length
4.Content-Type
5.Keep-Alive
6.Server
7.Date
여기 내 코드입니다. 파일의 원래 크기를 얻는 방법이 있습니까?
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;
}
}
진행률 부분에서 잘 작동하는 InputStream을 사용하여 시도했지만 스트림이 압축 파일을 생성했습니다. 이 파일의 압축을 풀 때 오프라인 모드에서 평소보다 많은 시간이 걸렸습니다. 파일을 위해 바이트 단위로 압축을 풀기 위해 완전히 다른 라운드가 진행되었습니다. 다음은 압축 된 바이트 만 읽는 코드입니다.InputStream input = 새로운 BufferedInputStream (httpResponse.getEntity(). getContent()); –
압축 된 바이트를 계산하는 방법으로 답변을 업데이트했습니다. –
Ryan, 안드로이드에서 어떻게해야합니까? android의 TeeInputStream에 대한 대안? –