code: private static void traverseAndWrite(File file, ZipOutputStream zos, StringBuilder pathBuilder, boolean isFirst, byte[] buffer)
              throws IOException {
  
          if (!isFirst) {
              pathBuilder.append(file.getName());
              if (!file.isFile()) {
                  pathBuilder.append('/');
              }
          }
  
          if (file.isFile()) {
              final String path = pathBuilder.toString();
              final ZipEntry zipEntry = new ZipEntry(path);
              zipEntry.setTime(file.lastModified());
              zos.putNextEntry(zipEntry);
  
              InputStream input = new BufferedInputStream(new FileInputStream(file));
              try {
                  int read;
                  do {
                      read = input.read(buffer);
                      zos.write(buffer, 0, Math.max(read, 0));
                  } while (read == buffer.length);
              } finally {
                  close(input);
              }
          } else {
              final File[] files = file.listFiles();
              if (files != null) {
                  int length = pathBuilder.length();
                  for (File innerFile : files) {
                      traverseAndWrite(innerFile, zos, pathBuilder, false, buffer);
                      pathBuilder.setLength(length);
                  }
              }
          }
      }
docs: /* --- Private methods ---