Compressing a file using zip or gzip
nBytesRead ignored!


        int nBytesRead;
        while ((nBytesRead = bis.read(buffer, 0, buffer.length)) &gt;= 0) {
            os.write(buffer);
        }



You forgot to use the nBytesRead variable when writing to the other stream, putting garbage data in the target file.

        int nBytesRead;
        while ((nBytesRead = bis.read(buffer, 0, buffer.length)) &gt;= 0) {
            os.write(buffer, 0, nBytesRead);
        }


zipDirectory() throws a NPE

Your code throws a NullPointerException if the directory doesn't exists. (Caused by dir.listFiles(); returning null, failing the for loop after)

You should check if dir.exists is true, before continuing with the method, and throw a FileNotFoundException if it is false.

Possible IndexOutOfBoundsException


String zipFile = file.getCanonicalPath().substring(dir.getCanonicalPath()
                        .length() + 1);



While this line usually works correctly, it fails on unix systems if the file is symlinked to a directory with less characters, this will throw a IndexOutOfBoundsException