How to Compress Files in ZIP Format in Java

Compressing files into a ZIP format is a common task in many applications, whether it’s for reducing file size, packaging files for distribution, or simply organizing multiple files into one archive. Java provides robust support for working with ZIP files through the java.util.zip package. This blog post will guide you through the process of compressing files into a ZIP format in Java.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Compressing Files into ZIP Format
  4. Complete Example
  5. Conclusion

Introduction

The java.util.zip package in Java provides classes for reading and writing standard ZIP and GZIP file formats. By using classes like ZipOutputStream and ZipEntry, you can easily create ZIP files in Java. This tutorial will demonstrate how to compress multiple files into a single ZIP file.

Prerequisites

Before you can compress files into a ZIP format, you need to ensure you have the necessary files and directories set up. In this example, we will compress two text files, file1.txt and file2.txt, into a single compressed.zip file.

Compressing Files into ZIP Format

To compress files into a ZIP format, you need to use the ZipOutputStream class. This class is used to write files to a ZIP archive.

Example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressFilesToZip {
    public static void main(String[] args) {
        String[] srcFiles = { "file1.txt", "file2.txt" };
        String zipFile = "compressed.zip";

        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            for (String srcFile : srcFiles) {
                try (FileInputStream fis = new FileInputStream(srcFile)) {
                    ZipEntry zipEntry = new ZipEntry(srcFile);
                    zos.putNextEntry(zipEntry);

                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) >= 0) {
                        zos.write(buffer, 0, length);
                    }
                    zos.closeEntry();
                }
            }
            System.out.println("Files have been compressed into " + zipFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • FileOutputStream is used to write to the compressed.zip file.
  • ZipOutputStream is created to write ZIP file entries.
  • Each source file is read using a FileInputStream, and a corresponding ZipEntry is created in the ZIP file.
  • The file contents are read into a buffer and written to the ZipOutputStream.
  • The closeEntry method is called to complete the writing of each file entry.

Complete Example

Here is a complete example including reading multiple files and compressing them into a ZIP file.

Files to Compress

Ensure you have two text files, file1.txt and file2.txt, with some content in the same directory as your Java program.

CompressFilesToZip.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressFilesToZip {
    public static void main(String[] args) {
        String[] srcFiles = { "file1.txt", "file2.txt" };
        String zipFile = "compressed.zip";

        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            for (String srcFile : srcFiles) {
                try (FileInputStream fis = new FileInputStream(srcFile)) {
                    ZipEntry zipEntry = new ZipEntry(srcFile);
                    zos.putNextEntry(zipEntry);

                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) >= 0) {
                        zos.write(buffer, 0, length);
                    }
                    zos.closeEntry();
                }
            }
            System.out.println("Files have been compressed into " + zipFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • The srcFiles array contains the names of the files to be compressed.
  • FileOutputStream and ZipOutputStream are used to create the ZIP file.
  • Each file is read and written to the ZIP file entry using a buffer to optimize the read and write operations.
  • After writing all files, the ZIP file is closed and saved.

Conclusion

Compressing files into a ZIP format in Java can be easily achieved using the java.util.zip package. By using classes like ZipOutputStream and ZipEntry, you can create ZIP files that contain multiple files. This tutorial demonstrated how to compress multiple text files into a single ZIP file.

Feel free to experiment with the code examples provided in this tutorial to gain a deeper understanding of how to compress files in Java. Happy coding!

Comments