码迷,mamicode.com
首页 > 编程语言 > 详细

Java determine uncompressed size of gzipped file

时间:2014-05-06 18:25:15      阅读:551      评论:0      收藏:0      [点我收藏+]

标签:style   java   ext   color   int   http   

If you want to determine the uncompressed size of a gzip file from within a program, you can extract to original file size from the gzip file. This size is stored in the last 4 bytes of the file. This will only provide the correct value if the compressed file was smaller than 4 Gb.

To extract this information and convert it to something useful in your Java program.

RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(raf.length() - 4);
int b4 = raf.read();
int b3 = raf.read();
int b2 = raf.read();
int b1 = raf.read();
int val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
raf.close();

The original size of the compressed file will now be available in the val variable in bytes.

http://www.abeel.be/wiki/Java_determine_uncompressed_size_of_gzipped_file

 

 

Java determine uncompressed size of gzipped file,布布扣,bubuko.com

Java determine uncompressed size of gzipped file

标签:style   java   ext   color   int   http   

原文地址:http://www.cnblogs.com/lizhongchen/p/3712068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!