Java - Zip files in a directory

Use the following Java method to Zip a set of files in a directory.

public boolean createZipFile(String dataDir, String zipFilePath) throws Exception
{
        boolean status = false;
        int BUFFER = 2048;
        try
        {
                BufferedInputStream origin = null;
                FileOutputStream dest = new FileOutputStream(zipFilePath);
                ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
                byte data[] = new byte[BUFFER];
               
                // get a list of files from current directory
                File f = new File(dataDir);
                String files[] = f.list();

                for (int i = 0; i < files.length; i++)
                {
                        FileInputStream fi = new FileInputStream(dataDir + File.separator + files[i]);
                        origin = new BufferedInputStream(fi, BUFFER);
                        ZipEntry entry = new ZipEntry(files[i]);
                        out.putNextEntry(entry);
                        int count;
                        while ((count = origin.read(data, 0, BUFFER)) != -1)
                        {
                                out.write(data, 0, count);
                        }
                        origin.close();
                }
                out.close();
        }
        catch (Exception e)
        {
                e.printStackTrace();
                throw e;
        }
        return status;
}

Search