public void zipFiles(){ FileOutputStream fos = null; ZipOutputStream zipOut = null; FileInputStream fis = null; try { File folder = new File("D:\\Data\\attachment"); File[] attachFiles = folder.listFiles(); fos = new FileOutputStream("C:\\Users\\admin\\Desktop\\tai-lieu\\testing.zip"); zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); for(File file: attachFiles){ fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(file.getName()); System.out.println("Zipping the file: "+file.getName()); zipOut.putNextEntry(ze); byte[] tmp = new byte[1024]; int size = 0; while((size = fis.read(tmp)) != -1){ zipOut.write(tmp, 0, size); } zipOut.flush(); fis.close(); } zipOut.close(); System.out.println("Done... Zipped the files..."); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try{ if(fos != null) fos.close(); } catch(Exception ex){ } } }