Private static void zipFile(String filePath) Note that the directory path is given from the command line.
* This Java program demonstrates how to compress a file in ZIP format. Compress a Single File ExampleThe following program compresses a file whose path is passed from the command line: import java.io.* setLevel(int level): sets the compression level ranging from 0 to 9 (the default).setMethod(int method): there are 2 methods: DEFLATED (the default) which compresses the data and STORED which doesn’t compress the data (archive only).You can also set the compression method and compression level using the following ZipOutputStream’s methods: Write all bytes read to the output stream using the write(byte bytes, int offset, int length) method.Read all bytes from the original file by using the Files.readAllBytes(Path) method.The ZipEntry class represents an entry of a compressed file in the ZIP file. Put a ZipEntry object by calling the putNextEntry(ZipEntry) method on the ZipOutputStream.The ZipOutputStream class implements an output stream filter for writing in the ZIP file format. The zip file does not store absolute paths. The resulting zip file contains the paths of filenames relative to the current folder. zip recursively compresses the content in folders. Open a ZipOutputStream that wraps an OutputStream like FileOutputStream. zip(zipfilename,filenames) compresses the contents of filenames into the zip file zipfilename.Steps to Compress a File in JavaHere are the steps to compress a file using Java code: With this feature, you can compress/decompress data on the fly in your Java programs. You know, Java has great support for writing and reading ZIP files via the easy-to-use API. In this tutorial, you will learn how to compress files in ZIP format using the package.