Java Decompressing Files Example

0

In Java File Compression Example, we have seen how to compress files to a zip file. In this article, we will unzip the zipped file and extract the compressed files.

The compressed ZIP file contains the following three files an XML, a properties file and a text file.

emp.xml:

<?xml version="1.0"?>
<employees>
	<employee id="1">
		<name>Joe</name>
		<age>34</age>
	</employee>
	<employee id="2">
		<name>Sam</name>
		<age>24</age>
	</employee>
	<employee id="3">
		<name>John<!-- author --> S</name>
		<age>44</age>
	</employee>
</employees>

x.properties:

name=javarticles.com

y.txt:

Hello!

Unzip files

In order to decompress a ZIP file, we will rely on ZipInputStream. Its a special input stream object for reading files in the ZIP file format. If you also want to compute checksum as you read through the ZIP file, you need another special input stream object called CheckInputStream that is mean to maintain the checksum as the data is being read.

In the below code, we make use of FileInputStream, CheckedInputStream and ZipInputStream to read the ZIP file. Each input stream has its own role. You can also make use of BufferedInputStream which maintains an internal buffer array.

FileInputStream fis = new FileInputStream("allFiles.zip");
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));

The ZIP file contains zip entries for each file. Once we have the ZIP input stream, you can read the zip entries using the ZipInputStream.getNextEntry() method.

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
...
}

The returned object is of type ZipEntry. We call on ZipEntry.getName() to get the file entry name. For each ZIP entry, we create a new FileOutputStream.

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
    System.out.println("Extracting: " + entry);
    int count;
    byte data[] = new byte[BUFFER];
    // write the files to the disk
    FileOutputStream fos = new FileOutputStream(entry.getName());
...
}

We read the data into a byte array using ZipInputStream which we will use to create new files. The byte array read is used to write to the FileOutputStream, we attach a buffer to improve the performance. In the end, in finally block, we will flush the written files and close the output stream.

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
    System.out.println("Extracting: " + entry);
    int count;
    byte data[] = new byte[BUFFER];
    // write the files to the disk
    FileOutputStream fos = new FileOutputStream(entry.getName());
    dest = new BufferedOutputStream(fos, BUFFER);
    try {
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
    } finally {
        dest.flush();
        dest.close();
    }
}

DecompressFile:

package com.javarticles.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class DecompressFile {
    private static final int BUFFER = 2048;
    public static void main(String[] args) throws IOException {
        DecompressFile decompressFile = new DecompressFile();
        decompressFile.decompress("allFiles.zip");
    }

    public long decompress(String fileName) throws IOException {
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream("allFiles.zip");
        CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
                checksum));
        try {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("Extracting: " + entry);
                int count;
                byte data[] = new byte[BUFFER];
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(entry.getName());
                dest = new BufferedOutputStream(fos, BUFFER);
                try {
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                } finally {
                    dest.flush();
                    dest.close();
                }
            }
            return checksum.getChecksum().getValue();
        } finally {
            zis.close();
        }
    }
}

Output:

Extracting: src/com/javarticles/io/emp.xml
Extracting: src/com/javarticles/io/x.properties
Extracting: src/com/javarticles/io/y.txt

Download the source code

This was an example about decompressing zip file.

You can download the source code here: fileDecompressExample.zip

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at [email protected]

Comments are closed.