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;
		}
	}
}

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?

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.

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");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.