Zipping folders and sub folders in Java

Hi,
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?

Thanks
try
 {
	     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);
	          }
	      out.closeEntry();
	      }
	      out.flush();
	      out.close();
	      }
	      catch(Exception e)
	         {
	              e.printStackTrace();
	          } 
	}

Open in new window

LVL 1
pdurbhaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

spoxoxCommented:
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]));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spoxoxCommented:
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:
http://panmental.de/public/programming_projects/HowTos/useCondorWithSubdirectories.php
File f = new File(fName);
if (f.isDirectory())
 doDirectory(f);
else
 addFile(f); // you already have this part
 
public static void doDirectory(File f) {
if (f.isDirectory())
 doDirectory(f);
else
 addFile(f); 
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Utilities

From novice to tech pro — start learning today.