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

ZipFile Help

I'm just trying to teach myself how to use java.util.zip, but I'm struggling with one part I think. The code below is what I have so far, I'm just not sure how to actually get it to read in a zip file? Any help would be much appreciated.

import java.io.*;
import java.util.*;
import java.util.zip.*;
public class ZipExample {
	public static final void copyInputStream(InputStream in, OutputStream out)
			throws IOException
			{
		byte[] buffer = new byte[1024];
		int len;
		while((len = in.read(buffer)) >= 0)
			out.write(buffer, 0, len);
		in.close();
		out.close();
			}
	public static final void main(String[] args) {
		Enumeration <?> entries;
		ZipFile zipFile;
		if(args.length != 1) {
			System.err.println("Usage: Unzip zipfile");
			return;
		}
		try {
			zipFile = new ZipFile(args[0]);
			entries = zipFile.entries();
			while(entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry)entries.nextElement();
				if(entry.isDirectory()) {
					// Assume directories are stored super then sub
					System.err.println("Extracting directory: " + entry.getName());
					(new File(entry.getName())).mkdir();
					continue;
				}
				System.err.println("Extracting file: " + entry.getName());
				copyInputStream(zipFile.getInputStream(entry),
						new BufferedOutputStream(new FileOutputStream(entry.getName())));
			}
			zipFile.close();
		} catch (IOException ioe) {
			System.err.println("Unhandled exception:");
			ioe.printStackTrace();
			return;
		}
	}
}
baseballer
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

I'm just trying to teach myself how to use java.util.zip, but I'm struggling with one part I think. The code below is what I have so far, I'm just not sure how to actually get it to read in a zip file? Any help would be much appreciated.

import java.io.*;
import java.util.*;
import java.util.zip.*;
public class ZipExample {
	public static final void copyInputStream(InputStream in, OutputStream out)
			throws IOException
			{
		byte[] buffer = new byte[1024];
		int len;
		while((len = in.read(buffer)) >= 0)
			out.write(buffer, 0, len);
		in.close();
		out.close();
			}
	public static final void main(String[] args) {
		Enumeration <?> entries;
		ZipFile zipFile;
		if(args.length != 1) {
			System.err.println("Usage: Unzip zipfile");
			return;
		}
		try {
			zipFile = new ZipFile(args[0]);
			entries = zipFile.entries();
			while(entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry)entries.nextElement();
				if(entry.isDirectory()) {
					// Assume directories are stored super then sub
					System.err.println("Extracting directory: " + entry.getName());
					(new File(entry.getName())).mkdir();
					continue;
				}
				System.err.println("Extracting file: " + entry.getName());
				copyInputStream(zipFile.getInputStream(entry),
						new BufferedOutputStream(new FileOutputStream(entry.getName())));
			}
			zipFile.close();
		} catch (IOException ioe) {
			System.err.println("Unhandled exception:");
			ioe.printStackTrace();
			return;
		}
	}
}


what do you mean by read in a zip file? does the current code work, if so what is it not doing that you want it to?

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

No I believe it works - I get the error message, what I don't understand completely is where I write in what file it is supposed to read in/if that is even how it works.

baseballer
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
No I believe it works - I get the error message, what I don't understand completely is where I write in what file it is supposed to read in/if that is even how it works.


Sorry i have just been busy with school look at line 23:

zipFile = new ZipFile(args[0]);


the args[0] is the files name you can change this to your need ie:

zipFile = new ZipFile("C:\whatever.zip");
DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: