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 compressFiles(File files[], File fileCompressed) throws IOException {

        byte[] buffer = new byte[SIZE_OF_BUFFER];
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
        for (int i = 0; i < files.length; i++) {
            FileInputStream fileInputStream = new FileInputStream(files[i]);
            zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath()));

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

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

        zipOutputStream.close();
    }
}