1 String sourceFile = "test1.txt";
2 FileOutputStream fos = new FileOutputStream("compressed.zip");
3 ZipOutputStream zipOut = new ZipOutputStream(fos);
4 File fileToZip = new File(sourceFile);
5 FileInputStream fis = new FileInputStream(fileToZip);
6 ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
7 zipOut.putNextEntry(zipEntry);
8 byte[] bytes = new byte[1024];
9 int length;
10 while((length = fis.read(bytes)) >= 0) {
11 zipOut.write(bytes, 0, length);
12 }
13 zipOut.close();
14 fis.close();
15 fos.close();
16