jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem zipping the file" Watch "Problem zipping the file" New topic

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
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
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
} catch(Exception e) {

any suggestions plz?
Thanks in advance
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Which line of the code is throwing that exception?
zhangChina lei

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

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
It's not a secret anymore!