SlideShare a Scribd company logo
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)
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
file compression/zip file.report
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.
file1.txt properties:
file2.docx properties:
file3.jpg properties:
file4.jpg properties:
file5.txt properties:
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:
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
file compression/zip file.report
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.
file1.txt properties:
file2.docx properties:
file3.jpg properties:
file4.jpg:
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.
Step2: ExtractZipContents.java
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.

More Related Content

PDF
Sticky bit
PDF
Terminalcommandsubuntu1 170123133631 (1)
PDF
26 io -ii file handling
PPTX
RandomAccessFile Quick Start
PPT
PPT
4 exercises for part 1
PPTX
Introduction to linux day3
PDF
Restaurant management system
Sticky bit
Terminalcommandsubuntu1 170123133631 (1)
26 io -ii file handling
RandomAccessFile Quick Start
4 exercises for part 1
Introduction to linux day3
Restaurant management system

More from Taslima Yasmin Tarin (14)

PPTX
RSA algorithem
PPTX
Mobile Satellite Communication
PPTX
Application of DSP in Biomedical science
PPTX
Real Life Application of Digital Electronics
PPTX
GPS (Global Positioning System) application
DOCX
Artificial intelligence chatbot Report
PPTX
AI chatbot for massenger
PPTX
file compression ,zip file
DOCX
PPTX
farakka barrage
PPT
PPTX
Operation Amplifire
RSA algorithem
Mobile Satellite Communication
Application of DSP in Biomedical science
Real Life Application of Digital Electronics
GPS (Global Positioning System) application
Artificial intelligence chatbot Report
AI chatbot for massenger
file compression ,zip file
farakka barrage
Operation Amplifire
Ad

Recently uploaded (20)

PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Foundation of Data Science unit number two notes
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
1_Introduction to advance data techniques.pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
Lecture1 pattern recognition............
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
climate analysis of Dhaka ,Banglades.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
ISS -ESG Data flows What is ESG and HowHow
Foundation of Data Science unit number two notes
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
1_Introduction to advance data techniques.pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
.pdf is not working space design for the following data for the following dat...
Qualitative Qantitative and Mixed Methods.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Clinical guidelines as a resource for EBP(1).pdf
Reliability_Chapter_ presentation 1221.5784
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Introduction to Knowledge Engineering Part 1
IB Computer Science - Internal Assessment.pptx
Lecture1 pattern recognition............
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Ad

file compression/zip file.report

  • 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
  • 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
  • 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.