Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;
import java.util.zip.*;

public class Main {
    public static int SIZE_OF_BUFFER = 1024;

    public static void compressFile(File file, File fileCompressed) throws IOException {

        byte[] buffer = new byte[SIZE_OF_BUFFER];
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
        FileInputStream fileInputStream = new FileInputStream(file);
        zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

        int size;
        while ((size = fileInputStream.read(buffer)) > 0)
            zipOutputStream.write(buffer, 0, size);

        zipOutputStream.closeEntry();
        fileInputStream.close();
        zipOutputStream.close();
    }
}