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 files from a folder which is updating regularly using multithreading

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

I have acode which unzip all the files from a folder .This code picks up all the zipped files at a time and then unzip it and write them to another folder but now my requirement is changed ,suppose the source folder where all the zipped files are present is refreshed or updated with new zipped files regularly then how can I implement in my code multithreading to get several files by threads and send it for unzipping.

Please suggest with some example or edit my code.

package com.myprojcet;
import java.io.File;
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"; //Source folder for zipped files
//Create input and output streams
File SourceDir = new File(zipFileName);
File[] zipFiles = SourceDir.listFiles();

ZipInputStream inStream = null;
OutputStream outStream = null;

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

//Get next zip entry and start reading data

for(int i=0; i < zipFiles.length; i++) {
inStream= new ZipInputStream(new FileInputStream(zipFiles));
while((entry = inStream.getNextEntry()) != null) {
outStream=new FileOutputStream("C:\\soft\\test2\\"+entry.toString()); //destination folder for unzipped file
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: user8687839 on Feb 27, 2012 11:00 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2012
Added on Feb 28 2012
12 comments
390 views