954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Extracting .zip files takes far to long

I'm fairley new to programming in general being a fresh IT graduate.

Some software I am developing needs to be able to compress a folder in to a .zip file and later extract it while maintaining its file structure.

I am able to do this but extracting the file takes far to long, a file that takes a few seconds for winzip to extract takes far longer in my app.

I've tried google and have failed to find the reason for this. My code is as follows:

public void unzipFolder( String startingZip, String destinationFolder ) {
		try {
			ZipFile zipFile = new ZipFile(startingZip);
			Enumeration< ? extends ZipEntry> zipCount = zipFile.entries();
			String dir = destinationFolder;
		
			while( zipCount.hasMoreElements() ) {
				ZipEntry item = (ZipEntry) zipCount.nextElement();
			
				if (item.isDirectory()) {
					File newdir = new File(dir + File.separator + item.getName());
					newdir.mkdir();
				} else {
					String newfilePath = dir + File.separator + item.getName();
					File newFile = new File(newfilePath);
					if (!newFile.getParentFile().exists()) {
						newFile.getParentFile().mkdirs();
					}
					InputStream is = zipFile.getInputStream(item);
					FileOutputStream fos = new FileOutputStream(newfilePath);
					int ch;
					while( (ch = is.read()) != -1 ) {
						fos.write(ch);
					}
					is.close();
					fos.close();
					}
			}
			zipFile.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}


Any help would be greatly appreciated ;p

youngwolf0
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

You need to implement some sort of buffered reading/writing; the way your code is currently written(single character I/O) it makes way too many I/O calls which can be a real performance killer.

A better option IMO is to use the libraries out there which ease the task of zipping/unzipping; some well known ones are 7-Zip-JBinding and TrueZip .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You