Announcement :

Try our newly launched website www.Technicalbaba.com Token Number - 1techbaba1

Wednesday, 20 November 2013

Java program to zip any folder

In our previous post we show how can you show your jsp / web application to this world with out any hosting or domain name , In this post we are showing how can you zip or unzip any folder using this java program with out any API.











Java  Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SHASHANK
 */
public class FolderZiper {
  public static void main(String[] a) throws Exception {
    zipFolder("d:\\aswing", "d:\\aswing.zip");
  }

  static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
  }

  static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
      throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
      addFolderToZip(path, srcFile, zip);
    } else {
      byte[] buf = new byte[1024];
      int len;
      FileInputStream in = new FileInputStream(srcFile);
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
      while ((len = in.read(buf)) > 0) {
        zip.write(buf, 0, len);
      }
    }
  }

  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
      throws Exception {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
      if (path.equals("")) {
        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
      } else {
        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
      }
    }
  }
}






Share it Please

Unknown

He is a blogger/website designer and developer.he is founder of www.technicalbaba.com and also loves web programming.

0 comments:

Post a Comment

We welcome's you to post your comments here , Our editorial team will review and reply to your valuable comment.Thank you to connect with us.














1. HTC offers bloggers to write a post for us , you can use your links too.


2. HTC offers ad networks to publish their ads in very low cost.


Copyright @ 2013 Ability 2 Crack. Designed by Templateism | Re-designed By Shashank MIshra