?
Solved

java.util.zip Question

Posted on 2011-04-20
31
Medium Priority
?
297 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:alpswon
  • 16
  • 8
  • 7
31 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35438117
0
 
LVL 92

Expert Comment

by:objects
ID: 35438125
your code looks ok to me

0
 

Author Comment

by:alpswon
ID: 35438158
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 35438159

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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438163
No I just used your code and it ran perfectly creating
file in the indpendnet directory
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438185
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
 
LVL 92

Expert Comment

by:objects
ID: 35438186
>  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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438193
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
 

Author Comment

by:alpswon
ID: 35438197
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438200

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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438202
and I'm doing it all on windows
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438210
This is the zip file I created
abc.zip
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438216
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
 
LVL 92

Expert Comment

by:objects
ID: 35438218
> 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
 

Author Comment

by:alpswon
ID: 35438225
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438227


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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438229
That's the problem with your zip I believe
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438231
It is not abc.txt it is aactuoally out.txt
and it opens fine with my zip
0
 

Author Comment

by:alpswon
ID: 35438233
I tried to open on Windows 7 and Windows XP and both failed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438237
Post me ths file back - maybe somthing happend in transmission
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438241
Maybe you have some security seettuings on your systems - are those corporate machines?
0
 

Author Comment

by:alpswon
ID: 35438242
Here is the file I got from you
 abc.zip
0
 

Author Comment

by:alpswon
ID: 35438243
Windows 7 is my personal PC and Windows XP is a corp machine
0
 
LVL 92

Expert Comment

by:objects
ID: 35438245
> This is the exact problem I have been having.

Thats nothing to do with your code
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35438251
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35438253
It is something with your WinZip

0
 
LVL 92

Expert Comment

by:objects
ID: 35438254
0
 

Author Comment

by:alpswon
ID: 35438255
I don't use winzip.  I am using the Windows default zip.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 248 total points
ID: 35438260
Try winzip, it is anyway useful stuff
0
 
LVL 92

Accepted Solution

by:
objects earned 252 total points
ID: 35438261
see the link I posted above. Includes a few solutions to your problem
0
 

Author Closing Comment

by:alpswon
ID: 35438282
Thank you both!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question