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