1. How to create a zip file for a set of files (Compress):
Step1: Create a set of files (contains images, documents, texts)
We will zip up the following files in this assignment:
1. file1.txt
2. file2.docx
3. file3.jpg (Image/file3.jpg)
4. file4.jpg (Image/file4.jpg)
5. file5.txt( Samiha/Tarin/Jannat/file5.txt)
2. The test class creates a FileOutputStream to 'atest.zip', the name of the zip file to create. It
creates a ZipOutputStream object based on the FileOutputStream. It then calls its addToZipFile()
method to write each file to the zip file.
In addToZipFile(), we create a FileInputStream to read from the specified file. We create a
ZipEntry object for the file and tell the ZipOutputStream that we'll be writing a ZipEntry to it via
the call to putNextEntry(). We read the file contents from the FileInputStream and write the file
contents to the ZipOutputStream.
Step2:
Test.java
3.
4. Step3:
The execution of “test.java” is shown below in the Console view:
So, the atest.zip file has been created.
Step4:
If we examine the contents of atest.zip, we can see that the zip file that we created contains the
expected files.
8. Uncompressing the createdZip file:
Zip compression can take a certain amount of time to perform. On occasion, you may want to
create a zip file to store multiple files in a single file, but you might want to avoid the time
required to compress the files. As an example, if you are zipping a set of large files that have
already been compressed, it would make sense to avoid compression in the big zip file that
you're creating. To demonstrate how to do this:
Step1:
9. Step2:
We used the test class to create a compressed zip file from a set of files. Now, we'll create an
'uncompressed-test.zip' file and add the following line to the test class:
test.java
10.
11. Step3:
The execution of “test.java” is shown below in the Console view:
Step4:
The uncompressed-test.zip file was created. If we examine the contents of uncompressed-
test.zip, we can see that the zip file that we created contains the expected files.
14. file5.txt:
Unzip the contents of a zipped file:
How to extract the contents of a zip file using the java.util.zip package and a little basic
File/Stream Java code. The atest.zip file is the zip file that we will unzip, and ExtractZipContents
is the Java class that will extract the contents of the zip file and write them to the file system.
The ExtractZipContents class is shown below. It creates a ZipFile object based on atest.zip. It
then enumerates over the ZipEntry objects within the zip file. It displays the name, size, and
compressed size of each ZipEntry object. If the name of the ZipEntry ends with a slash, it is a
directory, and this directory gets created along with all other directories above it via
file.mkdirs(). The 'continue' skips the rest of the code in the while loop and lets the while loop go
to the next zip entry.
If the name doesn't end with a slash, then we check to see if the file created from the current
ZipEntry name has a parent file, which would be a directory. If it has a parent, the parent
directory is created along with any other higher directories via the file.mkdirs() call.
Following this, the ExtractZipContents class does some standard Java input stream/output stream
code to read the contents of the current ZipEntry object and write the contents to the file that was
created using the ZipEntry name.
16. Step3:
The execution of our ExtractZipContents class with the atest.zip file is shown in the Console
view below:
We can see, using the java.util.zip package along with some standard Java file and stream code makes
it easy to extract the files within a zip file.