how to gzip unzip in java (eclipse)

11 views
Skip to first unread message

Sarvesh Kumar Singh

unread,
Aug 6, 2013, 1:42:19 AM8/6/13
to newidea_or...@googlegroups.com
Hi,

 how to gzip unzip in java (eclipse)


Sarvesh Kumar singh

Neo

unread,
Aug 7, 2013, 1:16:33 AM8/7/13
to newidea_or...@googlegroups.com
Java has very good inbuilt foundation classes for this... 

This is an example.


public class FolderZiper {
  public static void main(String[] a) throws Exception {
    zipFolder("c:\\a", "c:\\a.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);
      }
    }
  }
}


--
--
You received this message because you are subscribed to the Google
Groups "Technical Discussion" group.
To post to this group, send email to
newidea_or...@googlegroups.com
To unsubscribe from this group, send email to
newidea_or_techn...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/newidea_or_technical?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Technical Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to newidea_or_techn...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Shailendra shail

unread,
Aug 7, 2013, 1:34:44 AM8/7/13
to Technical Discussion

Thanks neo. Very good answer. I would love to use java.nio package for ziping unziping added in jdk-7. You can find it on net as well. I can put one example here but I don't have much access of internet. I think nishant will have some POC on it.

Neo

unread,
Aug 7, 2013, 2:32:18 AM8/7/13
to newidea_or...@googlegroups.com
Don't have right now... but will try to throw something in the evening... 
Reply all
Reply to author
Forward
0 new messages