Working with ZIP Archives in Java: java.util.zip and Zip4j
\n\nData compression and archiving is one of the key tasks when developing applications in Java. ZIP archives are widely used for packaging files, transferring data over a network, and saving disk space. In this article, we will take a detailed look at two main approaches to working with ZIP archives in Java: the built-in java.util.zip package and the powerful third-party library Zip4j.
Standard java.util.zip Package
\n\nStarting with JDK 1.1, Java has the java.util.zip package, which provides basic classes for working with the ZIP format. Main classes:
- \n
ZipOutputStream— for creating ZIP archives \n ZipInputStream— for reading ZIP archives \n ZipEntry— represents an individual entry (file or folder) inside the archive \n ZipFile— for random access to the contents of the archive \n
Creating a ZIP Archive with java.util.zip
\n\nimport java.io.*;\nimport java.util.zip.*;\n\npublic class ZipCreator {\n public static void main(String[] args) throws IOException {\n String sourceFile = "document.txt";\n String zipName = "archive.zip";\n\n try (FileOutputStream fos = new FileOutputStream(zipName);\n ZipOutputStream zos = new ZipOutputStream(fos)) {\n\n File fileToZip = new File(sourceFile);\n FileInputStream fis = new FileInputStream(fileToZip);\n\n ZipEntry zipEntry = new ZipEntry(fileToZip.getName());\n zos.putNextEntry(zipEntry);\n\n byte[] buffer = new byte[1024];\n int length;\n while ((length = fis.read(buffer)) >= 0) {\n zos.write(buffer, 0, length);\n }\n zos.closeEntry();\n fis.close();\n }\n System.out.println("Archive created: " + zipName);\n }\n}\n\nExtracting a ZIP Archive
\n\nimport java.io.*;\nimport java.util.zip.*;\n\npublic class ZipExtractor {\n public static void main(String[] args) throws IOException {\n String zipFile = "archive.zip";\n String outputDir = "extracted";\n\n new File(outputDir).mkdirs();\n\n try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {\n ZipEntry entry;\n while ((entry = zis.getNextEntry()) != null) {\n File newFile = new File(outputDir, entry.getName());\n new File(newFile.getParent()).mkdirs();\n\n FileOutputStream fos = new FileOutputStream(newFile);\n byte[] buffer = new byte[1024];\n int len;\n while ((len = zis.read(buffer)) > 0) {\n fos.write(buffer, 0, len);\n }\n fos.close();\n zis.closeEntry();\n }\n }\n System.out.println("Extraction completed to folder: " + outputDir);\n }\n}\n\nLimitations of java.util.zip:
\n- \n
- No encryption support (password protection) \n
- Limited Unicode support in file names (before Java 7) \n
- No ZIP64 support for archives > 4 GB (before Java 8) \n
- Complexity when working with nested folders \n
Zip4j Library — Extended Capabilities
\n\nZip4j is a popular open-source third-party library that addresses most of the limitations of the standard package. To use it, add the dependency in Maven:
\n\n<dependency>\n <groupId>net.lingala.zip4j</groupId>\n <artifactId>zip4j</artifactId>\n <version>2.11.5</version>\n</dependency>\n\nCreating an Encrypted ZIP Archive with Zip4j
\n\nimport net.lingala.zip4j.ZipFile;\nimport net.lingala.zip4j.model.ZipParam