Reading GZIPInputStream line by line

I have a file in .gz format. The java class for reading this file is GZIPInputStream. However, this class does not extend the BufferedReader java class. As a result, I cannot read the file line by line. I need something like this

reader = new MyGZInputStream( some constructor of GZInputStream) reader.readLine()... 

Although I create my own class, which extends the reading class or BufferedReader java and uses GZIPInputStream as one of its variables.

 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import java.util.zip.GZIPInputStream; public class MyGZFilReader extends Reader { private GZIPInputStream gzipInputStream = null; char[] buf = new char[1024]; @Override public void close() throws IOException { gzipInputStream.close(); } public MyGZFilReader(String filename) throws FileNotFoundException, IOException { gzipInputStream = new GZIPInputStream(new FileInputStream(filename)); } @Override public int read(char[] cbuf, int off, int len) throws IOException { // TODO Auto-generated method stub return gzipInputStream.read((byte[])buf, off, len); } } 

But it does not work when I use

 BufferedReader in = new BufferedReader( new MyGZFilReader("F:/gawiki-20090614-stub-meta-history.xml.gz")); System.out.println(in.readLine()); 

Can someone advise how to proceed.

+75
java file-io filereader gzipinputstream
Jul 03 '09 at 18:19
source share
6 answers

The main setup for decorators is as follows:

 InputStream fileStream = new FileInputStream(filename); InputStream gzipStream = new GZIPInputStream(fileStream); Reader decoder = new InputStreamReader(gzipStream, encoding); BufferedReader buffered = new BufferedReader(decoder); 

The key issue in this snippet is the value of encoding . This is the character encoding of the text in the file. These are US-ASCII, UTF-8, SHIFT-JIS, ISO-8859-9, & hellip ;? There are hundreds of possibilities, and the right choice usually cannot be determined from the file itself. It must be defined through some out-of-band channel.

For example, perhaps this is the default platform. However, in a networked environment this is very fragile. The machine that wrote the file may be in a neighboring cell, but has a different encoding by default.

Most network protocols use a header or other metadata to explicitly mark character encodings.

In this case, the file extension shows that the content is XML. For this purpose, XML contains the "encoding" attribute in the XML declaration. In addition, XML must really be processed using an XML parser, not text. Reading XML line by line is like a fragile special case.

It is not possible to explicitly specify the encoding for the second command. Use the default encoding at your peril!

+131
Jul 03 '09 at 18:24
source share
 GZIPInputStream gzip = new GZIPInputStream(new FileInputStream("F:/gawiki-20090614-stub-meta-history.xml.gz")); BufferedReader br = new BufferedReader(new InputStreamReader(gzip)); br.readLine(); 

+41
Jul 03 '09 at 18:23
source share
 BufferedReader in = new BufferedReader(new InputStreamReader( new GZIPInputStream(new FileInputStream("F:/gawiki-20090614-stub-meta-history.xml.gz")))); String content; while ((content = in.readLine()) != null) System.out.println(content); 
+3
Aug 22 2018-12-12T00:
source share

What about:

 GZIPInputStream zipReader = new GZIPInputStream(); InputStreamReader streamReader = new InputStreamReader(zipReader); BufferedReader bufferedReader = new BufferedReader(streamReader); 
0
Jul 03 '09 at 18:23
source share

You can use the following method in the utility class and use it if necessary ...

 public static List<String> readLinesFromGZ(String filePath) { List<String> lines = new ArrayList<>(); File file = new File(filePath); try (GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(file)); BufferedReader br = new BufferedReader(new InputStreamReader(gzip));) { String line = null; while ((line = br.readLine()) != null) { lines.add(line); } } catch (FileNotFoundException e) { e.printStackTrace(System.err); } catch (IOException e) { e.printStackTrace(System.err); } return lines; } 
0
Jul 23 '18 at 12:10
source share

with one line

 try (BufferedReader br = new BufferedReader( new InputStreamReader( new GZIPInputStream( new FileInputStream( "F:/gawiki-20090614-stub-meta-history.xml.gz"))))) {br.readLine();} 
0
Nov 09 '18 at 10:58
source share



All Articles