How to unzip (.zip files) archived or compressed files in ZIP formats using Java?
We use winrar or winzip to unzip files in windows. I once wondered if I could write a java program to unpack .zip files and java.util.zip package was my answer. This core package of java is used to unzip (unpack or open) compressed files in ZIP formats.
Table of Contents [hide]
Introduction
How to unzip (.zip files) archived or compressed files in ZIP formats using Java?
Java uses one of its core package java.util.zip to implement the functionality. The core classes used from the package java.util.zip are
- ZipFile - is used to read entries from a .zip file
- ZipEntry - represents an entry of the .zip file
The other basic classes required to read and write each entry are
- InputStream, BufferedInputStream, FileOutputStream, BufferedOutputStream etc
Algorithm
- Open a ZIP file for reading the specified File object.
- Get an enumeration of the ZIP file entries
- For each ZIP entry
-
If ZIP entry is a directory
- Create a directory with the ZIP entry's name
- else if ZIP entry is a file
- Create a file with the ZIP entry's name & write contents of the ZIP entry to that file
-
If ZIP entry is a directory
Implementation
// Unzip a .zip file
public void unzip(File inputFile) throws IOException
{
ZipFile zipFile = new ZipFile(inputFile);
Enumeration oEnum = zipFile.entries();
while( oEnum.hasMoreElements() )
{
ZipEntry zipEntry = (ZipEntry) oEnum.nextElement();
// write each entry to the correct location where the .zip file is extracted
storeEntry(zipFile, zipEntry);
}
zipFile.close();
}
/**
* Write a zip entry's contents to a new file created with its name and
* stored in the location of the entry as in the .zip file.
* As there can very well be many entries, we have created a separate method
* to save each entry and the unzip method just uses this method
*/
private void storeEntry(ZipFile zipFile, ZipEntry zipEntry)
{
File file = new File(zipEntry.getName());
if( zipEntry.isDirectory() )
{
file .mkdirs();
}
else
{
InputStream inputStream = zipFile.getInputStream(zipEntry);
write(inputStream, file);
}
file.deleteOnExit();
}
/**
* Read an input stream and write contents to a file
*/
public void write(InputStream inputStream, File fileToWrite)
{
BufferedInputStream buffInputStream = new BufferedInputStream( inputStream );
file.deleteOnExit();
File dir = new File( file.getParent() );
dir.mkdirs();
dir.deleteOnExit();
FileOutputStream fos = new FileOutputStream( file );
BufferedOutputStream bos = new BufferedOutputStream( fos );
// write bytes
int byteData;
while( ( byteData = buffInputStream.read() ) != EOF )
{
bos.write( (byte) byteData);
}
// close all the open streams
bos.close();
fos.close();
buffInputStream.close();
inputStream.close();
}
public void unzip(File inputFile) throws IOException
{
ZipFile zipFile = new ZipFile(inputFile);
Enumeration oEnum = zipFile.entries();
while( oEnum.hasMoreElements() )
{
ZipEntry zipEntry = (ZipEntry) oEnum.nextElement();
// write each entry to the correct location where the .zip file is extracted
storeEntry(zipFile, zipEntry);
}
zipFile.close();
}
/**
* Write a zip entry's contents to a new file created with its name and
* stored in the location of the entry as in the .zip file.
* As there can very well be many entries, we have created a separate method
* to save each entry and the unzip method just uses this method
*/
private void storeEntry(ZipFile zipFile, ZipEntry zipEntry)
{
File file = new File(zipEntry.getName());
if( zipEntry.isDirectory() )
{
file .mkdirs();
}
else
{
InputStream inputStream = zipFile.getInputStream(zipEntry);
write(inputStream, file);
}
file.deleteOnExit();
}
/**
* Read an input stream and write contents to a file
*/
public void write(InputStream inputStream, File fileToWrite)
{
BufferedInputStream buffInputStream = new BufferedInputStream( inputStream );
file.deleteOnExit();
File dir = new File( file.getParent() );
dir.mkdirs();
dir.deleteOnExit();
FileOutputStream fos = new FileOutputStream( file );
BufferedOutputStream bos = new BufferedOutputStream( fos );
// write bytes
int byteData;
while( ( byteData = buffInputStream.read() ) != EOF )
{
bos.write( (byte) byteData);
}
// close all the open streams
bos.close();
fos.close();
buffInputStream.close();
inputStream.close();
}
Tips
- You can write all the code in a single method and make the method recursive.








Comments
bnatey
توبيكات
توبيكات توبيكات
توبيكات اسلاميه
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه
توبيكات كريسماس
توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك
توبيكات
توبيكات
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات نكت
توبيكات جوال
توبيكات سعوديه
توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات
توبيكات
توبيكات فلاش
توبيكات روعه
توبيكات رائعه
منتديات
دردشة
العاب
العاب بنات
شات
توبيكات مصريه
توبيكات قطريه
دليل مواقع
تحميل صور
مركز تحميل
برامج
كأس العالم
كأس العالم 2010
العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات
Post new comment