Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        String[] filenames = new String[] { "filename1", "filename2" };
        byte[] buf = new byte[1024];
        String outFilename = "outfile.zip";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

        for (int i = 0; i < filenames.length; i++) {
            FileInputStream in = new FileInputStream(filenames[i]);
            out.putNextEntry(new ZipEntry(filenames[i]));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
    }
}