How to make a gzip file in Java?
To create a GZIP file, you can use GZIPOutputStream class provided by java.util.zip package. Here is an example,
String outFilename = "GZIPTest.txt.gz";
String inFilename = "Test.txt";
BufferedWriter bufferedWriter = null;
BufferedReader bufferedReader = null;
try {
//Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(
new OutputStreamWriter(
new GZIPOutputStream(new FileOutputStream(outFilename))
));
//Construct the BufferedReader object
bufferedReader = new BufferedReader(new FileReader(inFilename));
String line = null;
// from the input file to the GZIP output file
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
//Close the BufferedWrter
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Close the BufferedReader
if (bufferedReader != null ){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- What are class variables in Java?
- What are local variables in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- How to Use Updatable ResultSet in JDBC?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?