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>

