Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unzip all the files from a folder and place them another folder

715138Feb 27 2012 — edited Feb 27 2012
Hi All,

I have a need where in a folder there are several .zip files and through my java code I have to unzip all of them and place them to a new directory.For a single file to be unzipped I have the below code but for more than on zip file in a folder and to unzip all of them (the folder or directory is not zipped, only the file inside them are zipped and I have to unzip thoses files and place in new directory or folder) I am not getting how to move ahead.Help me.

Code for For a single file to be unzipped .
package com.myprojcet;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class Main {

	/**
	 * Extracts a zip file
	 */
	public void extractZipFile() {

		try {
			String zipFileName = "C:\\soft\\test\\Hello.zip";
			String extractedFileName = "C:\\soft\\test2\\Hello.txt";

			//Create input and output streams
			ZipInputStream inStream = new ZipInputStream(new FileInputStream(zipFileName));
			OutputStream outStream = new FileOutputStream(extractedFileName);

			ZipEntry entry;
			byte[] buffer = new byte[1024];
			int nrBytesRead;

			//Get next zip entry and start reading data
			if ((entry = inStream.getNextEntry()) != null) {
				while ((nrBytesRead = inStream.read(buffer)) > 0) {
					outStream.write(buffer, 0, nrBytesRead);
				}
			}

			//Finish off by closing the streams
			outStream.close();
			inStream.close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}


	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		new Main().extractZipFile();
	}

}
Thanks
Sumit

Edited by: EJP on 27/02/2012 19:40: {noformat}
{noformat} tags. Please use them. Without them your code is incomprehensible.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2012
Added on Feb 27 2012
5 comments
976 views