Writing to a GZIPOutputStream in Java

One of the most common tasks when dealing with file compression in Java is writing to a GZIPOutputStream. This class allows you to compress data using the gzip algorithm, which can significantly reduce file sizes.

To get started, you’ll need to import the java.util.zip package, which contains the necessary classes for working with compression in Java. Here’s an example of how to write to a GZIPOutputStream:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class GzipExample {
    public static void main(String[] args) {
        String sourceFile = "path/to/source/file.txt";
        String compressedFile = "path/to/compressed/file.txt.gz";

        try (FileOutputStream fos = new FileOutputStream(compressedFile);
             GZIPOutputStream gzipOS = new GZIPOutputStream(fos)) {

            // Read the source file and write compressed data to the GZIPOutputStream
            byte[] buffer = new byte[1024];
            int len;
            try (FileInputStream fis = new FileInputStream(sourceFile)) {
                while ((len = fis.read(buffer)) > 0) {
                    gzipOS.write(buffer, 0, len);
                }
            }

            System.out.println("File compressed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we first define the paths to the source file and the compressed file. These paths can be adjusted based on your specific requirements.

We then create a FileOutputStream with the path to the compressed file. This stream is used as the underlying output stream for the GZIPOutputStream. Inside the try block, we create a GZIPOutputStream object, passing the FileOutputStream as a parameter.

Next, we read the data from the source file in chunks and write it to the GZIPOutputStream until all data is processed. Finally, we print a success message if the compression process is completed without any exceptions.

Remember to handle any potential IOException that may occur when working with file streams.

That’s it! You now have the basic code to write to a GZIPOutputStream in Java. The resulting file will be compressed using the gzip algorithm, ready for further distribution or storage.

#Java #Compression