source codes to zip the entire folder not just files by files

Discussions

EJB design: source codes to zip the entire folder not just files by files

  1. Anyone know how to zip folder? I don't want to zip files by files? Can show it here? Thanks!

    Threaded Messages (5)

  2. Sorce Code[ Go to top ]

    import java.io.File;
    import java.util.zip.ZipOutputStream;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.util.zip.ZipEntry;
    /**
     * FolderZiper provide a static method to zip a folder.
     *
     * @author pitchoun
     */
    public class FolderZiper {
     
    /**
    * Zip the srcFolder into the destFileZipFile. All the folder subtree of the src folder is added to the destZipFile
    * archive.
    *
    * TODO handle the usecase of srcFolder being en file.
    *
    * @param srcFolder String, the path of the srcFolder
    * @param destZipFile String, the path of the destination zipFile. This file will be created or erased.
    */
    static public void zipFolder(String srcFolder, String destZipFile) {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;
    try {
    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);
    }
    catch (Exception ex){
    ex.printStackTrace();
    System.exit(0);
    }

    addFolderToZip("", srcFolder, zip);
    try {
    zip.flush();
    zip.close();
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    /**
    * Write the content of srcFile in a new ZipEntry, named path+srcFile, of the zip stream. The result
    * is that the srcFile will be in the path folder in the generated archive.
    *
    * @param path String, the relatif path with the root archive.
    * @param srcFile String, the absolute path of the file to add
    * @param zip ZipOutputStram, the stream to use to write the given file.
    */
    static private void addToZip(String path, String srcFile, ZipOutputStream zip) {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
    addFolderToZip(path, srcFile, zip);
    }
    else {
    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    try {
    FileInputStream in = new FileInputStream(srcFile);
    zip.putNextEntry(new ZipEntry(path +"/"+ folder.getName()));
    while ((len = in.read(buf)) > 0) {
    zip.write(buf, 0, len);
    }
    }
    catch (Exception ex){
    ex.printStackTrace();
    }
    }
    }

    /**
    * add the srcFolder to the zip stream.
    *
    * @param path String, the relatif path with the root archive.
    * @param srcFile String, the absolute path of the file to add
    * @param zip ZipOutputStram, the stream to use to write the given file.
    */
    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) {
    File folder = new File(srcFolder);
    String fileListe[] = folder.list();
    try {
    int i = 0;
    while (true) {
    addToZip(path+"/"+ folder.getName(), srcFolder+"/"+fileListe[i], zip);
    i++;
    }
    }
    catch (Exception ex) {
    }
    }
    }
  3. hi, a big thank u to mohit and all.......for the above codes rite, the program cannot be run cos there isn't the hyperlink "run" to run it. think it's becos there is no main class so i was wondering if there are any codes needed for the main class.Can someone help mi with this,can write down the codes here? Thanks!
  4. Source code to call FolderZiper[ Go to top ]

    import java.io.*;

    class UseFolderZiper
    {
     public static void main(String args[])throws IOException
     {
      BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter folder nameto be zipped");
      String fileName=cin.readLine();
      String fileNameUnixStyle=fileName.replace('
    ','/');
      FolderZiper obj=new FolderZiper();
      obj.zipFolder(fileNameUnixStyle,fileName+".zip");
     }
    }

    place FolderZiper.class path in the same folder as of UseFolderZiper.java

    And then compile and execute UseFolderZiper.java

  5. code not copied correctly[ Go to top ]

    Replace this statement

    String fileNameUnixStyle=fileName.replace('
    ','/');

    with

    String fileNameUnixStyle=fileName.replace('two backward slashes','/');

    manually enter 2 backward slashes \ .These were removed while converting to HTML page.

     

     

  6. zipping a directory[ Go to top ]

    Hi..in the code it shows to zip a single folder..but if we want to zip a entire directory which contains many sub folders and sub folders then????i have the code for it in java..but i want in android ....can any one help me???