Java has direct support for the ZIP file format. Typically, we would be using the following four classes from the java.util.zip package to work with the ZIP file format:
A ZipEntry object represents an entry in an archive file in a ZIP file format.
A zip entry may be compressed or uncompressed.
The ZipEntry class has methods to set and get information about an entry in a ZIP file.
ZipInputStream can read data from a ZIP file for each entry.
ZipOutputStream can write data to a ZIP file for each entry.
ZipFile is a utility class to read the entries from a ZIP file.
The following code shows how to create a ZIP File
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; //from w w w.j a v a 2s . c o m public class Main { public static void main(String[] args) { String zipFileName = "ziptest.zip"; String[] entries = new String[2]; entries[0] = "test1.txt"; entries[1] = "notes" + File.separator + "test2.txt"; zip(zipFileName, entries); } public static void zip(String zipFileName, String[] zipEntries) { try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(zipFileName)))) { // Set the compression level to best compression zos.setLevel(Deflater.BEST_COMPRESSION); for (int i = 0; i < zipEntries.length; i++) { File entryFile = new File(zipEntries[i]); if (!entryFile.exists()) { System.out.println("The entry file " + entryFile.getAbsolutePath() + " does not exist"); System.out.println("Aborted processing."); return; } ZipEntry ze = new ZipEntry(zipEntries[i]); zos.putNextEntry(ze); addEntryContent(zos, zipEntries[i]); zos.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } } public static void addEntryContent(ZipOutputStream zos, String entryFileName) throws IOException, FileNotFoundException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream( entryFileName)); byte[] buffer = new byte[1024]; int count = -1; while ((count = bis.read(buffer)) != -1) { zos.write(buffer, 0, count); } bis.close(); } }
The code above generates the following result.
The following code shows how to read contents of a ZIP File.
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; // w w w .j a v a 2 s .co m public class Main { public static void main(String[] args) { String zipFileName = "ziptest.zip"; String unzipdirectory = "extracted"; unzip(zipFileName, unzipdirectory); } public static void unzip(String zipFileName, String unzipdir) { try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream( new FileInputStream(zipFileName)))) { ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // Extract teh entry's contents extractEntryContent(zis, entry, unzipdir); } } catch (IOException e) { e.printStackTrace(); } } public static void extractEntryContent(ZipInputStream zis, ZipEntry entry, String unzipdir) throws IOException, FileNotFoundException { String entryFileName = entry.getName(); String entryPath = unzipdir + File.separator + entryFileName; createFile(entryPath); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream( entryPath)); byte[] buffer = new byte[1024]; int count = -1; while ((count = zis.read(buffer)) != -1) { bos.write(buffer, 0, count); } bos.close(); } public static void createFile(String filePath) throws IOException { File file = new File(filePath); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } file.createNewFile(); } }
The following code shows how to use the ZipFile class.
The ZipFile class comes in handy when you just want to list the entries in a ZIP file.
import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; // w w w . j av a2s.c o m public class Main { public static void main(String[] args) throws Exception { ZipFile zf = new ZipFile("ziptest.zip"); // Get the enumeration for all zip entries and loop through them Enumeration<? extends ZipEntry> e = zf.entries(); ZipEntry entry = null; while (e.hasMoreElements()) { entry = e.nextElement(); // Get the input stream for the current zip entry InputStream is = zf.getInputStream(entry); /* Read data for the entry using the is object */ // Print the name of the entry System.out.println(entry.getName()); } } }
The following code rewrites the above code using the Stream class and a lambda expression.
import java.io.IOException; import java.io.InputStream; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { public static void main(String[] args) throws Exception { ZipFile zf = new ZipFile("ziptest.zip"); Stream<? extends ZipEntry> entryStream = zf.stream(); entryStream.forEach(entry -> { try { // Get the input stream for the current zip entry InputStream is = zf.getInputStream(entry); System.out.println(entry.getName()); } catch (IOException e) { e.printStackTrace(); } }); } }
The GZIPInputStream and GZIPOutputStream classes are used to work with the GZIP file format.