We can use the
ZipOutputStream object for writing files in the zip file format. To create a zip file from multiple files, we perform the following:
FileOutputStreamobject for the given zip name.
ZipOutputStreamobject using the
writemethods to add that file to the
See main.java. file three
three.txt, in the current directory, which you can see on the sidebar of the above code snippet.
outputZipFileName, with a value,
educative.zip, which represents the final zip file to be created.
filesToBeWritten, that contains the path of the files to be zipped.
printZipFilesmethod to print the zip files present in the current directory.
cmethod. There is no zip file present, so nothing will be printed.
foswith the name
educative.zip. This will create a file output stream to write to the file. With the
fosobject, we create a
zos, which will have methods to write files to the zip.
forloop for the
Fileobject from the file name.
fis, which can be used to read the file as a stream.
ZipOutputStreamto begin writing a new zip file entry. This method will position the stream to the start of the entry data.
Once all the files are written to the zip, we call the
printZipFiles method. Now we have a zip file with the name
educative.zip. This file name will be printed on the console.
View all Courses