its supposed to zip folder content recursively, which it does until it encounters a folder with both an empty folder and a non-empty. or so i think.
i used the following structure to zip: http://img291.imageshack.us/img291/6120/84976017.jpg if untitled folder is removed it works. any tips would be much appreciated.

import java.io.*;
import java.util.zip.*;

public class Compress {
	static File rootDir;
	static FileInputStream fis;
	static ZipOutputStream zos;
	static String fsep; //file separator
	
	static void zipContents(File[] file, String dir) { 
		//dir - directory in the zip file
		byte[] buffer = new byte[4096];
		try {
				
			for(int i=0; i<file.length; i++) { //zip files
				if(file[i].isFile()) {
					fis = new FileInputStream(file[i]);
					zos.putNextEntry(new ZipEntry(dir+file[i].getName()));
						//shows how its stored
						//System.out.println(dir+file[i].getName());
					int bytes_read;
					while((bytes_read = fis.read(buffer))!=-1) zos.write(buffer, 0, bytes_read);
				
					fis.close(); 
				}
			} //for
			
			//create empty dir if theres no files inside
			if(file.length==1)  zos.putNextEntry(new ZipEntry(dir+fsep)); // this part is erroneous i think
			
			for(int i=0; i<file.length; i++) { //zip directories
				if(file[i].isDirectory()) {
					File subList[] = file[i].listFiles();
					
					//for dir of varying depth
					File unparsedDir=file[i];
					String parsedDir=fsep+file[i].getName()+ fsep; //last folder
						while(!unparsedDir.getParentFile().getName().equals(rootDir.getName())) {
							unparsedDir = file[i].getParentFile();
							parsedDir =fsep+unparsedDir.getName() + parsedDir;
					}	
					parsedDir=rootDir.getName()+parsedDir; //add input_output as root
						
					zipContents(subList, parsedDir);
				}
			} //for
		
		} catch(IOException ioex) { ioex.printStackTrace(); }			
	}

	public static void main(String args[]) throws IOException, FileNotFoundException {
		fsep = File.separator;
		rootDir = new File(System.getProperty("user.dir")+fsep+"src"+fsep+"input_output");
		File list[] = rootDir.listFiles();
		zos = new ZipOutputStream(new FileOutputStream(rootDir+".zip"));
		zipContents(list, rootDir.getName()+fsep);
		zos.close();
	}
}
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.