File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Problem zipping the file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem zipping the file" Watch "Problem zipping the file" New topic
Author

Problem zipping the file

Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
Which line of the code is throwing that exception?


Ping & DNS - my free Android networking tools app
zhangChina lei
Greenhorn

Joined: Nov 17, 2005
Posts: 7
it throws the exception is 19 lines
i try it on my computer
i try look out the problem
but not
zhangChina lei
Greenhorn

Joined: Nov 17, 2005
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem zipping the file