How do I zip a directory and all its contents?
Author: Deron Eriksson
Description: This Java tutorial describes how to zip all the contents of a directory.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page: < 1 2

(Continued from page 1)

This project's directory structure is shown below. The ZipDirectory class will zip all the contents of the 'stuff' folder.

'testing' project

Execution of ZipDirectory results in the following output:

---Getting references to all files in: C:\projects\workspace\testing\stuff
directory:C:\projects\workspace\testing\stuff\f1
directory:C:\projects\workspace\testing\stuff\f1\f2
directory:C:\projects\workspace\testing\stuff\f1\f2\f3
     file:C:\projects\workspace\testing\stuff\f1\f2\f3\file5.txt
     file:C:\projects\workspace\testing\stuff\file1.txt
     file:C:\projects\workspace\testing\stuff\file2.txt
directory:C:\projects\workspace\testing\stuff\folder
     file:C:\projects\workspace\testing\stuff\folder\file3.txt
     file:C:\projects\workspace\testing\stuff\folder\file4.txt
---Creating zip file
Writing 'f1\f2\f3\file5.txt' to zip file
Writing 'file1.txt' to zip file
Writing 'file2.txt' to zip file
Writing 'folder\file3.txt' to zip file
Writing 'folder\file4.txt' to zip file
---Done

The stuff.zip file is now present at the root level of the project.

stuff.zip created

If we examine stuff.zip, we can see that it contains the expected contents.

stuff.zip contents displayed in WinZip
Page: < 1 2