How to create a zip file



This code example shows how to create a zip file.
What we do is to create an input stream from the file we want to compress, and while we read from it we write the contents to an output stream.
This output stream is of type ZipOutputStream which takes an FileOutputStream as parameter.
Next we have to add a zip entry to the output stream before we start writing to it.
We will also clean up by closing the zip entry and both the input stream and output stream when we are done.







Search for more Java info on this site here:
Custom Search


2 thoughts on “How to create a zip file

    • I just tried it myself and it works fine. Be sure to give the right path to the file if it’s not located in the same folder as the program.

Any thoughts or questions?