Java – Zip Folder Recursively

After searching for a long time, finally i found the best example which could zip a folder recursively in Java.
AVAJAVA Web Tutorials – How do I zip a directory and all its contents?

package com.wordpress.ykyuen;

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

public class ZipDirectory {

  public static void main(String[] args) throws IOException {
    // The folder path to be zipped
    File directoryToZip = new File("C:\\Users\\Administrator\\Desktop\\ykyuen");

    List<File> fileList = new ArrayList<File>();
    System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
    getAllFiles(directoryToZip, fileList);
    System.out.println("---Creating zip file");
    writeZipFile(directoryToZip, fileList);
    System.out.println("---Done");
  }

  public static void getAllFiles(File dir, List<File> fileList) {
    try {
      File[] files = dir.listFiles();
      for (File file : files) {
        fileList.add(file);
        if (file.isDirectory()) {
          System.out.println("directory:" + file.getCanonicalPath());
          getAllFiles(file, fileList);
        } else {
          System.out.println("     file:" + file.getCanonicalPath());
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void writeZipFile(File directoryToZip, List<File> fileList) {

    try {
      // The output filename, you could use full path here or other
      // the .zip file will be found in the project root directory
      FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip");
      ZipOutputStream zos = new ZipOutputStream(fos);

      for (File file : fileList) {
        if (!file.isDirectory()) { // we only zip files, not directories
          addToZip(directoryToZip, file, zos);
        }
      }

      zos.close();
      fos.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the zipEntry's path to be a relative path that is relative
    // to the directory being zipped, so chop off the rest of the path
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
      zos.write(bytes, 0, length);
    }

    zos.closeEntry();
    fis.close();
  }

}

 

Done =)

Reference: AVAJAVA Web Tutorials – How do I zip a directory and all its contents?

One thought on “Java – Zip Folder Recursively”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.