1

I want to zip a folder. Actually I managed it.But there is a problem. if there is a empty folder in my directory, the empty folder is not created in my zip file For example my directory is:

/folder

/folder/temp.txt

/folder/emptyfolder

when I zip this directory with my code, emptyfolder doesn't exist in my zip file. I find problem.Problem is that files.length == 0 so for loop doesn't work.But I didn't find a solution. How to create the empty folder in my zip file?

  private static final void zip(File directory, File base,
      ZipOutputStream zos) throws IOException {

    File[] files;
    if(directory.isDirectory()) // folder
    {

        files = directory.listFiles(); 

    }else
    {
        files = new File[1]; // file
        files[0] = directory;
    }


    byte[] buffer = new byte[8192];
    int read = 0;

    for (int i = 0, n = files.length; i < n; i++)
    {
      if (files[i].isDirectory())
      {
        zip(files[i], base, zos);
      }
      else
      {

              FileInputStream in = new FileInputStream(files[i]);
              ZipEntry entry = new ZipEntry(files[i].getPath().substring(
                      base.getPath().lastIndexOf("/") + 1));
              zos.putNextEntry(entry);
              while (-1 != (read = in.read(buffer)))
              {
                  zos.write(buffer, 0, read);
              }

              in.close();               

      }
    }
Serkan
  • 91
  • 6

0 Answers0