java.util.Zip adding file but strip directory

M

Michael Trosen

I am trying to add a new file to a zip, so I do the normal creation of
a temp zip, add all the old zip contents to the new zip file, then add
my new file. However, when I add it, it also adds the directory
structure leading up to that new file. I want to just save the file,
and get rid of the directory structure.

Ie: file to add: /tmp/myfile.txt
When added to the zip, it has the following
file1.txt
file2.txt
/tmp/myfile.txt

I want it to be:
file1.txt
file2.txt
myfile.txt

Any suggestions?

Thanks!
Michael
 
L

Liz

Michael Trosen said:
I am trying to add a new file to a zip, so I do the normal creation of
a temp zip, add all the old zip contents to the new zip file, then add
my new file. However, when I add it, it also adds the directory
structure leading up to that new file. I want to just save the file,
and get rid of the directory structure.

Ie: file to add: /tmp/myfile.txt
When added to the zip, it has the following
file1.txt
file2.txt
/tmp/myfile.txt

I want it to be:
file1.txt
file2.txt
myfile.txt

Any suggestions?

Thanks!
Michael

You are giving the full path of the file. Try just giving the filename.
 
T

Thomas Weidenfeller

Michael said:
I am trying to add a new file to a zip, so I do the normal creation of
a temp zip, add all the old zip contents to the new zip file, then add
my new file. However, when I add it, it also adds the directory
structure leading up to that new file.
[...]
Any suggestions?


Since you, in your very own code, create the ZipEntry object, it is
entirely in your hands to name the file entry and call it whatever
pleases you. So, fix your code.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top