java.util.zip Question

I am writing a JSP page to zip a text file and let user to download the zipped file.  My program creates /data/output/abc.zip but when I open the zipped file, I don't see my original file, abc.txt.  I was told I need to use a relative path for my original file but I am out of luck.

Can you help?

Thanks,
Sammy
String file_to_be_zipped = "/data/output/abc.txt;

// program successfully creates /data/output/abc.txt
// now trying to create /data/output/abc.zip file

String zip_file_name = "/data/output/abc.zip;
ZipOutputStream out = new ZipOutputStream(
                         new FileOutputStream(zip_file_name));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
byte[] data = new byte[1024];               
FileInputStream in = new FileInputStream(file_to_be_zipped);
int count;
out.putNextEntry(new ZipEntry(file_to_be_zipped));
while((count = in.read(data,0,1024)) != -1) {      
  out.write(data, 0, count);
}
out.closeEntry();
in.close();
out.close();

Open in new window

alpswonAsked:
Who is Participating?
 
objectsCommented:
see the link I posted above. Includes a few solutions to your problem
0
 
objectsCommented:
your code looks ok to me

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
alpswonAuthor Commented:
All examples that I have seen create a zip file from files or subdirectories in  the current directory where the program is running, not a directory outside of the current path.  My code is running on $JBOSS_HOIME/server/default/deploy/myapp/process_zip/ and my data files exists in /data/output/ directory. I think this gives the problem.
0
 
for_yanCommented:

It has some quotes missing in the names of the file,
so it obviously not exactly this file you used.

Otherwise, it seems OK, I just created zip file with it
and opened it successfully:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MakeZip {

    public static void main(String [] args){
    String file_to_be_zipped = "/temp/test/out.txt";

// program successfully creates /data/output/abc.txt
// now trying to create /data/output/abc.zip file

String zip_file_name = "/temp/test/abc.zip";
                          try{
ZipOutputStream out = new ZipOutputStream(
                         new FileOutputStream(zip_file_name));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
byte[] data = new byte[1024];
FileInputStream in = new FileInputStream(file_to_be_zipped);
int count;
out.putNextEntry(new ZipEntry(file_to_be_zipped));

while((count = in.read(data,0,1024)) != -1) {
  out.write(data, 0, count);
}
out.closeEntry();
in.close();
out.close();




}   catch(Exception ex) {
System.out.println(ex.toString());

    }
}
}

Open in new window

0
 
for_yanCommented:
No I just used your code and it ran perfectly creating
file in the indpendnet directory
0
 
for_yanCommented:
And created  also in the third directory different form the
file form where I took the contents.

Are you sure yyou used exactly this code - as it lacks some obvious quotes after
filenames, this code is probably different.
Try to use my code - it works perfectly for me.
0
 
objectsCommented:
>  I think this gives the problem.

that won't give a problem. Your code appears to be fine.
Make sure your checking the right zip, and the code you posted is what you actually run
delete all class and zip, rebuild project and rerun

0
 
for_yanCommented:
You'll also need to catch exception - make sure
you print it and printStackTrace - maybe it is nit finiding the source file?

Exception will tell you
0
 
alpswonAuthor Commented:
for yan,

Have you tried to open the zipped file from windows? I don't think you can open the file.

I was able to unzip from unix but it created subdirectory the original file was restored under /temp/test/temp/test/out.txt

How can i get rid of creation of the sub dir.  I want to unzip just the original file.
0
 
for_yanCommented:

Yes, I opened it and even read the text file which I zipped inside

and for all three time when I ran this program
0
 
for_yanCommented:
and I'm doing it all on windows
0
 
for_yanCommented:
This is the zip file I created
abc.zip
0
 
for_yanCommented:
Try to run it and look for Exception, and also you could post your actual
code with correct quotes and try ... catch loop, etc.
0
 
objectsCommented:
> String file_to_be_zipped = "/data/output/abc.txt;

that controls the path, don't specify a path if you don't need one
0
 
alpswonAuthor Commented:
When I opened your file from windows, it's empty.  I don't see abc.txt  Also, when I extracted the file, I got the following error from Windows.

"Windows has blocked access to these files to help to protect your computer"

This is the exact problem I have been having.
0
 
for_yanCommented:


In here
new ZipEntry(file_to_be_zipped));

use just
new ZipEntry("abc.txt"));

You'll of course need the path whne reading the file
itself
0
 
for_yanCommented:
That's the problem with your zip I believe
0
 
for_yanCommented:
It is not abc.txt it is aactuoally out.txt
and it opens fine with my zip
0
 
alpswonAuthor Commented:
I tried to open on Windows 7 and Windows XP and both failed
0
 
for_yanCommented:
Post me ths file back - maybe somthing happend in transmission
0
 
for_yanCommented:
Maybe you have some security seettuings on your systems - are those corporate machines?
0
 
alpswonAuthor Commented:
Here is the file I got from you
 abc.zip
0
 
alpswonAuthor Commented:
Windows 7 is my personal PC and Windows XP is a corp machine
0
 
objectsCommented:
> This is the exact problem I have been having.

Thats nothing to do with your code
0
 
for_yanCommented:
I opened the file I got back from you - it is identical with what
I sent - contians file out.txt which I can open and read
0
 
for_yanCommented:
It is something with your WinZip

0
 
objectsCommented:
0
 
alpswonAuthor Commented:
I don't use winzip.  I am using the Windows default zip.
0
 
for_yanCommented:
Try winzip, it is anyway useful stuff
0
 
alpswonAuthor Commented:
Thank you both!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.