tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Zip Utilities > Zip Empty Directory

Zip Empty Directory 

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 Name  :  
com/bethecoder/tutorials/utils/zip/ZipEmptyDir.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.utils.zip;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipEmptyDir {

  /**
   @param args
   */
  public static void main(String[] args) {

    String zipFilePath = "C:\\empty_dirs.zip";
    String [] emptyDirs = {
        
        "C:\\EMPTY\\EMPTY4\\EMPTY5",
        "C:\\EMPTY2\\EMPTY6",
        "C:\\EMPTY3",
        "C:\\EMPTY9"
    };
    
    File child = null;
    String entryName;
    int index = 0;
    
    try {
      ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
      
      for (int i = ; 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);
              }

              System.out.println("Adding : " + child.getAbsolutePath());

            /**
             * Just add only empty directories
             */
            if (child.isDirectory() && child.list().length == 0) {
                /**
                 * Add ZIP directory entry
                 */
              zipOut.putNextEntry(new ZipEntry(entryName + "/"));
              zipOut.closeEntry();
            }
      }
  
      /**
       * Close Zip stream
       */
      zipOut.close();
      System.out.println("Done..");
      
    catch (FileNotFoundException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  
}
   

It gives the following output,
Adding : C:\EMPTY\EMPTY4\EMPTY5
Adding : C:\EMPTY2\EMPTY6
Adding : C:\EMPTY3
Adding : C:\EMPTY9
Done..


                    Print

   


Books Recommendation

 
 
bl  br