Zipping folders and sub folders in Java

Posted on 2008-01-24
Medium Priority
Last Modified: 2013-12-01
The attached code zips up all the files present in C:\abc\ss into C:\Abc.zip at the top level. When I open up the zip file using WinZip and extract the file into another folder temp, the subfolders "abc" and "ss" are lost. All the files are getting extracted to the top level only i.e. C:\temp. ( and not C:\temp\abc\ss).

How can I change this code to make sure that the sub folders also get zipped during the process?

	     File inFolder=new File("C:\\abc\\ss");
	     File outFolder=new File("C:\\Abc.zip");
	     ZipOutputStream out = new ZipOutputStream(new 
	BufferedOutputStream(new FileOutputStream(outFolder)));
	     BufferedInputStream in = null;
	     byte[] data    = new byte[1000];
	     String files[] = inFolder.list();
	     for (int i=0; i<files.length; i++)
	      in = new BufferedInputStream(new FileInputStream
	(inFolder.getPath() + "/" + files[i]), 1000);                  
	out.putNextEntry(new ZipEntry(files[i])); 
	      int count;
	      while((count = in.read(data,0,1000)) != -1)
	           out.write(data, 0, count);
	      catch(Exception e)

Open in new window

Question by:pdurbha
  • 2
LVL 11

Accepted Solution

spoxox earned 1000 total points
ID: 20739070
To retain directory information for each zipped file, chage the name of the ZipEntry object to reflect the path...

Line 14: out.putNextEntry(inFolder.getPath() + "/" + new ZipEntry(files[i]));
LVL 11

Expert Comment

ID: 20739307
To add subdirectories and their contents, use recursion - something like the snippet. I haven't thought about whether File objects are the right way to go, etc. Just presenting a concept.

Somebody else tried the same thing you're doing:
File f = new File(fName);
if (f.isDirectory())
 addFile(f); // you already have this part
public static void doDirectory(File f) {
if (f.isDirectory())

Open in new window


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
I originally wrote this article to compare SARDU and YUMI, but have now added Easy2Boot, since that is the one I currently use and find the easiest to create and alter.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question