How to read content of the Zipped file without extracting in java

前端 未结 3 871
傲寒
傲寒 2020-12-18 07:49

I have file with names like ex.zip. In this example, the Zip file contains only one file with the same name(ie. `ex.txt\'), which is quite large. I don\'t want

3条回答
  •  甜味超标
    2020-12-18 08:27

    I think that in your case the fact that a zipfile is a container that can hold many files (and thus forces you to navigate to the right contained file each time you open it) seriously complicates things, as you state that each zipfile only contains one textfile. Maybe it's a lot easier to just gzip the text file (gzip is not a container, just a compressed version of your data). And it's very simple to use:

    GZIPInputStream gis = new GZIPInputStream(new FileInputStream("file.txt.gz"));
    // and a BufferedReader on top to comfortably read the file
    BufferedReader in = new BufferedReader(new InputStreamReader(gis) );
    

    Producing them is equally simple:

    GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream("file.txt.gz"));
    

提交回复
热议问题