Zipping /Unzipp both current directories and subdirectories using zlib java

Hi

Guys any one provide me code for zipping and unzipping directory contains a subdirectories..using zlib java

It is very urgent ..please send me the code..............

consider..

folder name is input for zipping

and zipfile name is input for unzipping..

Please help me out in this problemm

Thanks regards,

javaprabhu

[373 byte] By [javaprabhua] at [2007-11-26 23:44:15]
# 1

http://forum.java.sun.com/thread.jspa?threadID=468452&messageID=2158669

unzip

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.SortedSet;

import java.util.TreeSet;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

* UnZip -- print or unzip a JAR or PKZIP file using java.util.zip. Command-line

* version: extracts files.

*

* @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07

* 17:40:35 ian Exp $

*/

public class UnZip {

/** Constants for mode listing or mode extracting. */

public static final int LIST = 0, EXTRACT = 1;

/** Whether we are extracting or just printing TOC */

protected int mode = LIST;

/** The ZipFile that is used to read an archive */

protected ZipFile zippy;

/** The buffer for reading/writing the ZipFile data */

protected byte[] b;

/**

* Simple main program, construct an UnZipper, process each .ZIP file from

* argv[] through that object.

*/

public static void main(String[] argv) {

UnZip u = new UnZip();

for (int i = 0; i < argv.length; i++) {

if ("-x".equals(argv[i])) {

u.setMode(EXTRACT);

continue;

}

String candidate = argv[i];

// System.err.println("Trying path " + candidate);

if (candidate.endsWith(".zip") || candidate.endsWith(".jar"))

u.unZip(candidate);

else

System.err.println("Not a zip file? " + candidate);

}

System.err.println("All done!");

}

/** Construct an UnZip object. Just allocate the buffer */

UnZip() {

b = new byte[8092];

}

/** Set the Mode (list, extract). */

protected void setMode(int m) {

if (m == LIST || m == EXTRACT)

mode = m;

}

/** Cache of paths we've mkdir()ed. */

protected SortedSet dirsMade;

/** For a given Zip file, process each entry. */

public void unZip(String fileName) {

dirsMade = new TreeSet();

try {

zippy = new ZipFile(fileName);

Enumeration all = zippy.entries();

while (all.hasMoreElements()) {

getFile((ZipEntry) all.nextElement());

}

} catch (IOException err) {

System.err.println("IO Error: " + err);

return;

}

}

protected boolean warnedMkDir = false;

/**

* Process one file from the zip, given its name. Either print the name, or

* create the file on disk.

*/

protected void getFile(ZipEntry e) throws IOException {

String zipName = e.getName();

switch (mode) {

case EXTRACT:

if (zipName.startsWith("/")) {

if (!warnedMkDir)

System.out.println("Ignoring absolute paths");

warnedMkDir = true;

zipName = zipName.substring(1);

}

// if a directory, just return. We mkdir for every file,

// since some widely-used Zip creators don't put out

// any directory entries, or put them in the wrong place.

if (zipName.endsWith("/")) {

return;

}

// Else must be a file; open the file for output

// Get the directory part.

int ix = zipName.lastIndexOf('/');

if (ix > 0) {

String dirName = zipName.substring(0, ix);

if (!dirsMade.contains(dirName)) {

File d = new File(dirName);

// If it already exists as a dir, don't do anything

if (!(d.exists() && d.isDirectory())) {

// Try to create the directory, warn if it fails

System.out.println("Creating Directory: " + dirName);

if (!d.mkdirs()) {

System.err.println("Warning: unable to mkdir "

+ dirName);

}

dirsMade.add(dirName);

}

}

}

System.err.println("Creating " + zipName);

FileOutputStream os = new FileOutputStream(zipName);

InputStream is = zippy.getInputStream(e);

int n = 0;

while ((n = is.read(b)) > 0)

os.write(b, 0, n);

is.close();

os.close();

break;

case LIST:

// Not extracting, just list

if (e.isDirectory()) {

System.out.println("Directory " + zipName);

} else {

System.out.println("File " + zipName);

}

break;

default:

throw new IllegalStateException("mode value (" + mode + ") bad");

}

}

}

java_2006a at 2007-7-11 15:15:33 > top of Java-index,Java Essentials,Java Programming...
# 2

You can search in google

i'm new to this forum. so i dont know how to redirect you.

Here is the code from one of our forum member:

import java.io.*;

import java.util.*;

import java.util.zip.*;

public class unzip

{

public static void main(String args[])

{

if(args.length < 1)

{

System.out.println("Syntax : unzip zip-file destination-dir[optional]");

return;

}

try

{

ZipFile zf = new ZipFile(args[0]);

Enumeration zipEnum = zf.entries();

String dir = new String(".");

if( args.length == 2 )

{

dir = args[1].replace('\\', '/');

}

if(dir.charAt(dir.length()-1) != '/')

dir += "/";

while( zipEnum.hasMoreElements() )

{

ZipEntry item = (ZipEntry) zipEnum.nextElement();

if( item.isDirectory() ) //Directory

{

File newdir = new File( dir + item.getName() );

System.out.print("Creating directory "+newdir+"..");

newdir.mkdir();

System.out.println("Done!");

}

else //File

{

String newfile = dir + item.getName();

System.out.print("Writing "+newfile+"..");

InputStream is = zf.getInputStream(item);

FileOutputStream fos = new FileOutputStream(newfile);

int ch;

while( (ch = is.read()) != -1 )

{

fos.write(ch);

}

is.close();

fos.close();

System.out.println("Done!");

}

}

zf.close();

}

catch(Exception e)

{

System.err.println(e);

}

}

}

code #2

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.SortedSet;

import java.util.TreeSet;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

* UnZip -- print or unzip a JAR or PKZIP file using java.util.zip. Command-line

* version: extracts files.

*

* @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07

* 17:40:35 ian Exp $

*/

public class UnZip {

/** Constants for mode listing or mode extracting. */

public static final int LIST = 0, EXTRACT = 1;

/** Whether we are extracting or just printing TOC */

protected int mode = LIST;

/** The ZipFile that is used to read an archive */

protected ZipFile zippy;

/** The buffer for reading/writing the ZipFile data */

protected byte[] b;

/**

* Simple main program, construct an UnZipper, process each .ZIP file from

* argv[] through that object.

*/

public static void main(String[] argv) {

UnZip u = new UnZip();

for (int i = 0; i < argv.length; i++) {

if ("-x".equals(argv[i])) {

u.setMode(EXTRACT);

continue;

}

String candidate = argv[i];

// System.err.println("Trying path " + candidate);

if (candidate.endsWith(".zip") || candidate.endsWith(".jar"))

u.unZip(candidate);

else

System.err.println("Not a zip file? " + candidate);

}

System.err.println("All done!");

}

/** Construct an UnZip object. Just allocate the buffer */

UnZip() {

b = new byte[8092];

}

/** Set the Mode (list, extract). */

protected void setMode(int m) {

if (m == LIST || m == EXTRACT)

mode = m;

}

/** Cache of paths we've mkdir()ed. */

protected SortedSet dirsMade;

/** For a given Zip file, process each entry. */

public void unZip(String fileName) {

dirsMade = new TreeSet();

try {

zippy = new ZipFile(fileName);

Enumeration all = zippy.entries();

while (all.hasMoreElements()) {

getFile((ZipEntry) all.nextElement());

}

} catch (IOException err) {

System.err.println("IO Error: " + err);

return;

}

}

protected boolean warnedMkDir = false;

/**

* Process one file from the zip, given its name. Either print the name, or

* create the file on disk.

*/

protected void getFile(ZipEntry e) throws IOException {

String zipName = e.getName();

switch (mode) {

case EXTRACT:

if (zipName.startsWith("/")) {

if (!warnedMkDir)

System.out.println("Ignoring absolute paths");

warnedMkDir = true;

zipName = zipName.substring(1);

}

// if a directory, just return. We mkdir for every file,

// since some widely-used Zip creators don't put out

// any directory entries, or put them in the wrong place.

if (zipName.endsWith("/")) {

return;

}

// Else must be a file; open the file for output

// Get the directory part.

int ix = zipName.lastIndexOf('/');

if (ix > 0) {

String dirName = zipName.substring(0, ix);

if (!dirsMade.contains(dirName)) {

File d = new File(dirName);

// If it already exists as a dir, don't do anything

if (!(d.exists() && d.isDirectory())) {

// Try to create the directory, warn if it fails

System.out.println("Creating Directory: " + dirName);

if (!d.mkdirs()) {

System.err.println("Warning: unable to mkdir "

+ dirName);

}

dirsMade.add(dirName);

}

}

}

System.err.println("Creating " + zipName);

FileOutputStream os = new FileOutputStream(zipName);

InputStream is = zippy.getInputStream(e);

int n = 0;

while ((n = is.read(b)) > 0)

os.write(b, 0, n);

is.close();

os.close();

break;

case LIST:

// Not extracting, just list

if (e.isDirectory()) {

System.out.println("Directory " + zipName);

} else {

System.out.println("File " + zipName);

}

break;

default:

throw new IllegalStateException("mode value (" + mode + ") bad");

}

}

}

Leelavathia at 2007-7-11 15:15:33 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi man,

This code is some wat helped me...but thing is that it is not fully satisfying problem...please help me out..

I have a zipfile containing one folder

like this

prabhu\prabhuimg\some image files

I want to unzip all folders..now it is unzipping only current folder prabhu

Pleaseeeeeeeeee help me out in this worst sistuation

thanks and regards,

prabhu

javaprabhua at 2007-7-11 15:15:33 > top of Java-index,Java Essentials,Java Programming...
# 4
#1 doesn't do that but #2 does.
ejpa at 2007-7-11 15:15:33 > top of Java-index,Java Essentials,Java Programming...
# 5

import java.io.*;

import java.util.zip.*;

public class Unzip {

public static void main(String[] args) {

try {

final int BUFFER_LENGTH = 1024;

final String PATH = "C:\\";

final String FILENAME = "Desktop.zip";

System.out.println("Unzipping file...");

ZipInputStream in = new ZipInputStream(new FileInputStream(PATH + FILENAME));

byte[] buf = new byte[BUFFER_LENGTH];

ZipEntry entry;

int len;

while ((entry = in.getNextEntry()) != null) {

int index = entry.getName().lastIndexOf("/") ;

if (index != -1) {

File dir = new File(PATH + entry.getName().substring(0, index));

if (!(dir.exists() && dir.isDirectory())) {

dir.mkdirs();

}

}

OutputStream out = new FileOutputStream(PATH + entry);

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

out.close();

}

in.close();

System.out.println("Unzip complete.");

} catch (Exception e) {

e.printStackTrace();

}

}

}

JavaTigera at 2007-7-11 15:15:33 > top of Java-index,Java Essentials,Java Programming...