Trying to figure out how to use the following program to zip up files
in another directory other than current "." Using current works fine,
I substitute it with a path "c:\\stuff" I get the following run-time
error:
Adding: file1.txt
java.io.FileNotFoundException: file1.txt (The system cannot find the
file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:103)
at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
at Zip.main(Zip.java:25)
It finds the first file but then fails. Here is the code:
import java.util.*;
import java.io.*;
import java.util.zip.*;
public class Zip {
static int BUFFER = 4096;
static String dir = "c:\\stuff";
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("c:\\blah.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.STORED);
// get a list of files from current directory
File f = new File(dir);
String[] files = f.list();
byte data[] = new byte[BUFFER];
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 = fi.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
I am a Windows (XP) platform. Thanks for the help.