aspose file tools
The moose likes Java in General and the fly likes opening .gz file and extracting the .txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "opening .gz file and extracting the .txt file" Watch "opening .gz file and extracting the .txt file" New topic
Author

opening .gz file and extracting the .txt file

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 164
Hi,

I have a .gz file and when i try extracting it, it has a .txt file and i want to open the .gz file and extract this .txt file and place it in another folder. Please advise how i can achieve this. I tried a few open source programs but didn't really work.

Thanks,
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2535
    
  10

Unlike a .zip file, a .gz is just a single compressed file, though often that file is a tar archive. Let's assume your file is just a simple compressed text file. You don't even have to search around for an open source solution, because standard Java already includes a GZIPInputStream class. You can wrap your FileInputStream in that, and then wrap the result in a BufferedReader or whatever gives you the interface you want. Read the contents of the file and write it out in plain text where you want it to go.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You forgot InputStreamReader; BufferedReader takes a Reader, not an InputStream. So the entire chain is this:
Jacob, don't be intimidated by the nesting; you can use temporary variables if you want:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: opening .gz file and extracting the .txt file
 
Similar Threads
Decompress a zip file in java
Invalid archive directory
java.io.IOException: Not in GZIP format
Observing heap space error when viewing 90MB file from UI after 4 times.
Display a file in Notepad