//////////////////////////////////////////////////////////////////////
//jZip.java --
//author - Tom Valesky
//purpose - example of how to use java.util.zip classes; compresses one
//          file 
//////////////////////////////////////////////////////////////////////
import java.util.zip.*;
import java.io.*;

public class jZip
{
        public static void compress_file(String filename, String outfilename)
        {

        try
        {

                byte[] buf = new byte[4096];            //read buffer

                int retval;                             //return value

                //set up input stream
                FileInputStream is = new FileInputStream(filename);

                //set up output stream
                FileOutputStream os = new FileOutputStream(outfilename);
                //wrap output stream in ZipOutputStream
                ZipOutputStream zos = new ZipOutputStream(os);

                //set entry field in zipfile; do this once for each
                //file you're compressing
                zos.putNextEntry(new ZipEntry(filename));

                //read the input file in and write it to the zipfile
                do
                        {
                        //read file in from input stream
                        retval = is.read(buf, 0, 4096);

                        
                        //-1 indicates end of file
                        if (retval != -1)
                        {
                                //write buffer to output stream
                                zos.write(buf, 0, retval);
                        }


                        }while (retval != -1);
                //close this ZipEntry
                zos.closeEntry();

                //close input stream
                is.close();
                //close output stream
                zos.close();
        }
        catch(Exception e)
        {
                System.out.println("Exception occurred: " + e);
        }

        }

        public static void main(String[] argv)
        {
        if (argv.length != 2)
                {
                System.out.println("usage: java jZip <filename> <zipfile name>");
                return;
                }
        compress_file (argv[0], argv[1]);
        }
}
