Creating and Extracting Zip Archives in Java using ZipInputStream and ZipOutputStream

Zip archives are a popular way to bundle and compress multiple files and directories into a single compressed file. In Java, the ZipInputStream and ZipOutputStream classes from the java.util.zip package provide a flexible and convenient way to create and extract zip archives. This blog post will guide you through the process of creating and extracting zip archives using these classes. 

Creating Zip Archives using ZipOutputStream

The ZipOutputStream class allows you to create a new zip archive and add files or directories to it. To create a zip archive using ZipOutputStream, follow these steps: 

  1. Create an instance of FileOutputStream to write the zip archive. 
  2. Create an instance of ZipOutputStream with the FileOutputStream as its parameter. Use the putNextEntry method to add each file or directory to the zip archive. 
  3. Read the data from the source file or directory and write it to the ZipOutputStream
  4. Close the zip entry using the closeEntry method. 
  5. Close the streams to ensure proper resource cleanup. 

Here's an example code snippet to create a zip archive:

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZipArchiveExample {
    public static void main(String[] args) {
        String sourceFolder = "source_folder";
        String zipFile = "archive.zip";
        
        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zipOS = new ZipOutputStream(fos)) {
            
            File folder = new File(sourceFolder);
            for (File file : folder.listFiles()) {
                if (file.isFile()) {
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zipOS.putNextEntry(zipEntry);
                    
                    FileInputStream fis = new FileInputStream(file);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = fis.read(buffer)) != -1) {
                        zipOS.write(buffer, 0, bytesRead);
                    }
                    fis.close();
                    zipOS.closeEntry();
                }
            }
            
            System.out.println("Zip archive created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Extracting Zip Archives using ZipInputStream

The ZipInputStream class allows you to extract files and directories from a zip archive. To extract a zip archive using ZipInputStream, follow these steps: 

  1. Create an instance of FileInputStream to read the zip archive. 
  2. Create an instance of ZipInputStream with the FileInputStream as its parameter. 
  3. Iterate through the zip entries using the getNextEntry method. 
  4. Create an instance of FileOutputStream to write the extracted file. 
  5. Read the data from the ZipInputStream and write it to the FileOutputStream
  6. Close the ZipInputStream entry and the FileOutputStream
  7. Repeat the process for all entries. 
  8. Close the streams to ensure proper resource cleanup. 

Here's an example code snippet to extract files from a zip archive:

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ExtractZipArchiveExample {
    public static void main(String[] args) {
        String zipFile = "archive.zip";
        String targetFolder = "extracted_folder";
        
        try (FileInputStream fis = new FileInputStream(zipFile);
             ZipInputStream zipIS = new ZipInputStream(fis)) {
            
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            File folder = new File(targetFolder);
            folder.mkdirs();
            
            ZipEntry zipEntry;
            while ((zipEntry = zipIS.getNextEntry()) != null) {
                File outputFile = new File(folder, zipEntry.getName());
                FileOutputStream fos = new FileOutputStream(outputFile);
                
                while ((bytesRead = zipIS.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                }
                
                fos.close();
                zipIS.closeEntry();
            }
            
            System.out.println("Zip archive extracted successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Conclusion

Using the ZipInputStream and ZipOutputStream classes, you can easily create and extract zip archives in Java. These classes provide a versatile way to bundle files and directories into compressed archives and then extract them as needed.

Comments