code: private static void compressDirectory(String relativePath, File srcPath, ZipOutputStream zip) throws IOException
     {
        if (srcPath.isDirectory())
        {
           zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName() + "/"));
           zip.closeEntry();
  
           String files[] = srcPath.list();
           for (int i = 0; i < files.length; i++)
           {
              compressDirectory(relativePath + "/" + srcPath.getName(), new File(srcPath, files[i]), zip);
           }
        }
        else
        {
           InputStream in = new FileInputStream(srcPath);
           try
           {
              zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName()));
  
              transfer(in, zip);
  
              zip.closeEntry();
           }
           finally
           {
              if (in != null)
              {
                 in.close();
              }
           }
        }
     }
docs: Compress files and directories.