Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
     * TODO check faster and more stable implementations.
     *
     * @param source_filepath
     * @param destinaton_zip_filepath
     * @throws IOException
     */
    public static void gzipFile(final String source_filepath, final String destinaton_zip_filepath)
            throws IOException {
        byte[] buffer = new byte[4096];
        try (FileOutputStream fileOutputStream = new FileOutputStream(destinaton_zip_filepath)) {
            try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream)) {
                try (FileInputStream fileInput = new FileInputStream(source_filepath)) {
                    int bytes_read;
                    while ((bytes_read = fileInput.read(buffer)) > 0) {
                        gzipOutputStream.write(buffer, 0, bytes_read);
                    }
                }
                gzipOutputStream.finish();
            }
        } catch (IOException ex) {
            throw ex;
        }
    }
}