How to Create a Zipped File with Oracle JDK

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(args[0]));

    for (int i = 1; i < args.length; i++) {
      final InputStream in = new FileInputStream(args[i]);

      out.putNextEntry(new ZipEntry(args[i]));

      int next = in.read();

      while (next != -1) {
        out.write(next);

        next = in.read();
      }

      in.close();

      out.closeEntry();
    }

    out.close();
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s