Creating a zip file from the contents of a directory

hi, I am having a problem as the title suggests with a zip fil creation...

using the basic example zip.java i wished to edit it so it doesnt zip a file fro the current directory but rather a directory i inputted.

It is able to read the first file then throws out the following error with the code below it:

java.io.FileNotFoundException: test.jpg (The system cannot find the file specified)

import java.io.*;

import java.util.zip.*;

publicclass Zip{

staticfinalint BUFFER = 2048;

publicstaticvoid main (String argv[]){

try

{

BufferedInputStream origin =null;

FileOutputStream dest =new FileOutputStream("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/test.zip");

CheckedOutputStream checksum =new CheckedOutputStream(dest,new Adler32());

ZipOutputStream out =new

ZipOutputStream(new BufferedOutputStream(checksum));

//out.setMethod(ZipOutputStream.DEFLATED);

byte data[] =newbyte[BUFFER];

// get a list of files from current directory

File f =new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");

f.listFiles();

String files[] = f.list();

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

{

System.out.println("Adding: "+files[i]);

FileInputStream fi =new FileInputStream(files[i]);

origin =new BufferedInputStream(fi, BUFFER);

ZipEntry entry =new ZipEntry(files[i]);

out.putNextEntry(entry);

int count;

while((count = origin.read(data, 0,

BUFFER)) != -1)

{

out.write(data, 0, count);

}

origin.close();

}

out.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

After investigation i am lead to believe this is because the method returns an array of file and directory names only but not their path and the unqualified names would have therefore be defaulted to the current working directory. Something i understand if this is the case.

So instead i used the File.listFiles() method to return an array

of File objects, instead of an array of Strings as shown in the snippet of the changed code below (the changed code highlighted)...but arrived at another error on the second section of highlighted code meaning i cant compile. I cant understand why this is so!

The error is: "cannot find symbol, Symbol: Contructor ZipEntry (Java.IO.file), location: class.java.util.zip.ZipEntry"

File f =new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");

**************File g[] = f.listFiles();***************

**************String files[] = f.list();**************

for (int i=0; i><g.length; i++)

{

System.out.println("Adding: "+g[i]);

FileInputStream fi =new FileInputStream(g[i]);

origin =new BufferedInputStream(fi, BUFFER);

**************ZipEntry entry =new ZipEntry(g[i]);************

out.putNextEntry(entry);

int count;

while((count = origin.read(data, 0,

BUFFER)) != -1)

{

out.write(data, 0, count);

}

origin.close();

}

out.close();

}

Any help and thoughts most appreciated. Thank u in advance>

[5179 byte] By [philengland1984a] at [2007-10-2 15:48:16]
# 1
Try replacing**************ZipEntry entry = new ZipEntry(g);************with ZipEntry entry = new ZipEntry(files)and check if it works...
ordinary_guya at 2007-7-13 15:49:52 > top of Java-index,Other Topics,Algorithms...
# 2
// *************ZipEntry entry = new ZipEntry(g[i]);************ZipEntry entry = new ZipEntry(files[i]);
ordinary_guya at 2007-7-13 15:49:52 > top of Java-index,Other Topics,Algorithms...
# 3

I'll admit i took 1 look at that reply an thought "thats stupid that wont work"...

then a second look an though "actually, i should work i cant believe i didnt think of that"

Anyways i tried it and it did work

How are the duke dollars awarded, cos u should have them ordinary_guy

cheers!

philengland1984a at 2007-7-13 15:49:52 > top of Java-index,Other Topics,Algorithms...
# 4
cheers !
ordinary_guya at 2007-7-13 15:49:52 > top of Java-index,Other Topics,Algorithms...
# 5
If i want to ZIp a file which i am retriving in a variable XYZ inside a class, how should i do it, can anyone help me by giving the lines of the code.Thanks in AdvanceMessage was edited by: vensMessage was edited by: vens
vensa at 2007-7-13 15:49:52 > top of Java-index,Other Topics,Algorithms...