Problem zipping the file
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Guys,
I m getting null poniter exception where i pass the location of the file in the args[0]
if i run like java Zip . it works fine.
if i give the location ex:java zip c:\santhosh\test it gives me a error saying null pointer exception.
The code as follows
import java.io.*;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("C:\\Documents and Settings\\vhr\\Desktop\\aditya\\myfigs.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(argv[0]);
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();
}
}
}
====
any suggestions plz?
Thanks in advance
I m getting null poniter exception where i pass the location of the file in the args[0]
if i run like java Zip . it works fine.
if i give the location ex:java zip c:\santhosh\test it gives me a error saying null pointer exception.
The code as follows
import java.io.*;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("C:\\Documents and Settings\\vhr\\Desktop\\aditya\\myfigs.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(argv[0]);
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();
}
}
}
====
any suggestions plz?
Thanks in advance
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Which line of the code is throwing that exception?
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
it throws the exception is 19 lines
i try it on my computer
i try look out the problem
but not
i try it on my computer
i try look out the problem
but not
zhangChina lei
Greenhorn
Posts: 7
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i know why you come along this exception:
chang this code in your program:
==============================
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(argv[0] + "\\" + files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
=============================
but in your code,you point the argv[0] dirctory,you must sure there is no
anther dirctory in it
this program is only useful all files under the argv[0] dirctory,not
useful mixed files and other dirctorys
chang this code in your program:
==============================
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(argv[0] + "\\" + files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
=============================
but in your code,you point the argv[0] dirctory,you must sure there is no
anther dirctory in it
this program is only useful all files under the argv[0] dirctory,not
useful mixed files and other dirctorys
| I'm doing laundry! Look how clean this tiny ad is: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






