This Java tip illustrates a method of compressing a file in the GZIP format. Gunzip is a universal format used by many applications to compress their files.

try {
        // Create the GZIP output stream
        String outFilename = "outfile.gzip";
        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFilename));
    
        // Open the input file
        String inFilename = "infilename";
        FileInputStream in = new FileInputStream(inFilename);
    
        // Transfer bytes from the input file to the GZIP output stream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
    
        // Complete the GZIP file
        out.finish();
        out.close();
    } catch (IOException e) {
    }