Java provides the ‘java.util.zip’ package to compress and decompress the data. To get started, we will need to import the necessary classes from the java.util.zip
package. These classes allow us to work with zip files and compress data.
In the below example we are using ZipOutputStream and ZipEntry Class. Both are essential classes in Java’s java.util.zip package for working with ZIP files. They play crucial roles in creating and manipulating ZIP archives.
ZipOutputStream:
- Purpose:
ZipOutputStream
is a class that provides the functionality to write data to a ZIP file in a compressed format. - Usage: You use
ZipOutputStream
to create a new ZIP file or update an existing one by adding entries (files or directories) and their compressed data. - Key Methods:
putNextEntry(ZipEntry e)
: Starts a new ZIP entry (file or directory) within the archive.write(byte[] buffer, int offset, int length)
: Writes compressed data to the current ZIP entry.closeEntry()
: Closes the current ZIP entry.close()
: Closes theZipOutputStream
.
ZipEntry:
- Purpose:
ZipEntry
represents an entry (file or directory) within a ZIP file. It holds information about the entry, such as its name, size, and compression settings. - Usage: You create a
ZipEntry
object for each file or directory you want to include in the ZIP archive. - Key Methods and Properties:
setName(String name)
: Sets the name of the ZIP entry.getSize()
: Returns the uncompressed size of the entry.setCompressedSize(long csize)
: Sets the compressed size of the entry.setMethod(int method)
: Sets the compression method (typically DEFLATE).
Together, ZipOutputStream
and ZipEntry
allow you to create, update, and manage ZIP files in your Java applications. You can add files, specify their compression settings, and write compressed data to the ZIP archive using these classes
import java.io.*; import java.util.zip.*; public class ZipFilesExample { public static void main(String[] args) { String[] sourceFiles = {"c:\\file1.txt", "c:\\file2.txt"}; String zipFileName = "c:\\documents\\output.zip"; try { FileOutputStream fos = new FileOutputStream(zipFileName); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String sourceFile : sourceFiles) { File fileToZip = new File(sourceFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); zipOut.closeEntry(); } zipOut.close(); System.out.println("Files have been zipped successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Let’s break down the code step by step:
1. We define an array of source files (sourceFiles
) and specify the name and location of the output zip file (zipFileName
).
2. We start by creating an output stream (fos
) to write to the zip file. The ZipOutputStream
(zipOut
) is responsible for handling the zip entries.
3. We iterate through each source file in the sourceFiles
array. For each file, we create a File
object (fileToZip
) and a FileInputStream
(fis
). Then, we create a ZipEntry
for the file and add it to the ZipOutputStream
.
4. Inside the loop, we read the file’s content in chunks and write it to the zip stream. Once the file is fully processed, we close the input stream (fis
) and the zip entry.
5. Finally, we close the zip output stream (zipOut
) and print a success message.