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