The following example shows creating a ZIP file out of user given empty directories.
The method zipOut.putNextEntry() adds a new file/directory entry to the ZIP file.
Remove the drive prefix before making an entry to build the ZIP file in proper format.
If a ZIP Entry name ends with "/" then its treated as directory entry.
So to create an empty directory in ZIP file suffix the entry name with "/".
Check the source of ZipEntry.
ZipEntry.java
public boolean isDirectory()
{
return this.name.endsWith("/");
}
File child = null;
String entryName; int index = 0;
try {
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
for (int i = 0 ; i < emptyDirs.length ; i ++) {
child = new File(emptyDirs[i]);
entryName = child.getAbsolutePath();
/**
* Remove the drive prefix,
*
* C:\NEW2\ABC to NEW2\ABC
* C:\NEW2\CCCC\AAA\IPC.LOG to NEW2\CCCC\AAA\IPC.LOG
*
* If we don't remove the prefix, a directory
* named C: is getting created in the ZIP file.
* To avoid this remove the directory prefix.
*/
index = entryName.indexOf(File.separator); if (index > 0) {
entryName = entryName.substring(index + 1);
}