zip a file using spring boot

Solutions on MaxInterview for zip a file using spring boot by the best coders in the world

showing results for - "zip a file using spring boot"
Bruno
28 Oct 2020
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}