Find the size of the file inside a GZIP file

前端 未结 4 930
长情又很酷
长情又很酷 2020-12-06 02:58

Is there a way to find out the size of the original file which is inside a GZIP file in java?

As in, I have a file a.txt of 15 MB which has been GZipped to a.gz of s

4条回答
  •  没有蜡笔的小新
    2020-12-06 03:55

    public class ReadStream {
    
        public static void main(String[] args) {
            try {
                RandomAccessFile raf = new RandomAccessFile(new File("D:/temp/temp.gz"), "r");
                try {
                    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;
    
                    System.out.println(val);
    
                    raf.close();
                } catch (IOException ex) {
                    Logger.getLogger(ReadStream.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ReadStream.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

提交回复
热议问题