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

Edited 6 Years Ago by youngwolf0: Accidental Double post, sorry folks

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.

Edited 6 Years Ago by ~s.o.s~: n/a

Comments
TrueZip! Pure java.
This article has been dead for over six months. Start a new discussion instead.