space in file name

G

Guest

Hello All,
Thank you in advance. I am using java.util.zip to zip several folders
and subfolders. When I try and zip a folder
(c:\inetpub\wwwroot\test\this is a test) it works fine. But when I
unzip it, I get the directory structure (inetpub\wwwroot\test\this is a
test) along with the files. If I remove the spaces
(c:\inetpub\wwwroot\test\this_is_a_test) I get this_is_a_test and the
files I need. Is there a work around for this. What I am hoping to get
is (this is a test) and all of the files.

Thanks again
 
C

Chris Uppal

unwantedspam said:
Thank you in advance. I am using java.util.zip to zip several folders
and subfolders. When I try and zip a folder
(c:\inetpub\wwwroot\test\this is a test) it works fine. But when I
unzip it, I get the directory structure (inetpub\wwwroot\test\this is a
test) along with the files. If I remove the spaces
(c:\inetpub\wwwroot\test\this_is_a_test) I get this_is_a_test and the
files I need. Is there a work around for this. What I am hoping to get
is (this is a test) and all of the files.

I'm not sure what your problem is (I can't understand your explanation). But
it sounds as if you /might/ be making one of three common mistakes when you are
building your ZIP file. If so then it would be worthwhile fixing it or them
and see if the problem goes away.

The first is that it is easy to forget that '\' is not a path separator in ZIP
files. You must /always/ use a forward slash '/', even on Windows.

The second is that you don't normally want to add entries corresponding to the
directories themselves (it's not illegal, but it can confuse some ZIP file
readers). So you just have entries for the actual files and leave the reader
to deduce the directories.

The third is that if you /do/ add entries for directories, then the last
character in the name of each /must/ be '/' (and never '\').

If none of that helps, then it would be worthwhile clarifying exactly what you
are using to read the ZIP file -- it might be a bug in the reader rather than
any problem with the ZIP file itself.

-- chris
 
G

Guest

Chris said:
I'm not sure what your problem is (I can't understand your explanation). But
it sounds as if you /might/ be making one of three common mistakes when you are
building your ZIP file. If so then it would be worthwhile fixing it or them
and see if the problem goes away.

The first is that it is easy to forget that '\' is not a path separator in ZIP
files. You must /always/ use a forward slash '/', even on Windows.

The second is that you don't normally want to add entries corresponding to the
directories themselves (it's not illegal, but it can confuse some ZIP file
readers). So you just have entries for the actual files and leave the reader
to deduce the directories.

The third is that if you /do/ add entries for directories, then the last
character in the name of each /must/ be '/' (and never '\').

If none of that helps, then it would be worthwhile clarifying exactly what you
are using to read the ZIP file -- it might be a bug in the reader rather than
any problem with the ZIP file itself.

-- chris



Chris,
Thank you for your response. I have replaced the "\" with "/" and the
same thing is happening.

I will try to explain this a little better. : )

Basically, say I zip the folder: "c:\inetpub\wwwroot\test\this is a
test\". When I open the zip file it has the exact same folder
structure: "inetpub\wwwroot\test\this is a test" with all of the files
that are "under" the folder "this is a test".

Now, if I zip the folder: "c:\inetpub\wwwroot\test\this_is_a_test\" and
open the zip file it only has "this_is_a_test" with all of the files
that are "under" the folder "this_is_a_test". This is what I want to
happen but with folders with spaces in the name like "This is a test".

I am not adding entries for the directories. I am using
file.getAbsolutePath.

Hopefully a explained this a little better. Is there an escape
character I need to use in "front" of the space?

Thanks again for your help.
 
C

Chris Uppal

unwantedspam said:
Basically, say I zip the folder: "c:\inetpub\wwwroot\test\this is a
test\". When I open the zip file it has the exact same folder
structure: "inetpub\wwwroot\test\this is a test" with all of the files
that are "under" the folder "this is a test".

Now, if I zip the folder: "c:\inetpub\wwwroot\test\this_is_a_test\" and
open the zip file it only has "this_is_a_test" with all of the files
that are "under" the folder "this_is_a_test". This is what I want to
happen but with folders with spaces in the name like "This is a test".

I am not adding entries for the directories. I am using
file.getAbsolutePath.

So you are getting the absolute pathname of the file, and using that, unchanged
except for replacing '\' with '/' as the name to use for the ZipEntry; am I
understanding you correctly ?

Also, if I read you right, in both cases the directory and its contents are
written to the zipfile, but the differences is that in the case where there are
spaces in the directory name, the name that appears in the zipfile has several
components (inetpub/wwwroot/test/this_is_a_test), but if the folder's name has
no spaces, then all the ZipEntries are given shorter names without the
"inetpub/wwwroot/test/" prefix ?

Is there an escape
character I need to use in "front" of the space?

No. There is no need (and in fact no way) to escape the space.

I think this must be a problem somewhere in the code you use to create the
zipfile. I would add some tracing (or use a debugger) to find exactly what
names your code is giving the ZipEntries. I'm pretty sure you'll find the
problem there, i.e. that this is nothing to do with zip files themselves (or
their Java implementation).

-- chirs
 
G

Guest

Chris said:
So you are getting the absolute pathname of the file, and using that, unchanged
except for replacing '\' with '/' as the name to use for the ZipEntry; am I
understanding you correctly ?

Also, if I read you right, in both cases the directory and its contents are
written to the zipfile, but the differences is that in the case where there are
spaces in the directory name, the name that appears in the zipfile has several
components (inetpub/wwwroot/test/this_is_a_test), but if the folder's name has
no spaces, then all the ZipEntries are given shorter names without the
"inetpub/wwwroot/test/" prefix ?



No. There is no need (and in fact no way) to escape the space.

I think this must be a problem somewhere in the code you use to create the
zipfile. I would add some tracing (or use a debugger) to find exactly what
names your code is giving the ZipEntries. I'm pretty sure you'll find the
problem there, i.e. that this is nothing to do with zip files themselves (or
their Java implementation).

-- chirs



Chris,
Thanks again very much for your response. I greatly appreciate your
help.
So you are getting the absolute pathname of the file, and using that, unchanged
except for replacing '\' with '/' as the name to use for the ZipEntry; am I
understanding you correctly ?
Yes, you are correct
Also, if I read you right, in both cases the directory and its contents are
written to the zipfile, but the differences is that in the case where there are
spaces in the directory name, the name that appears in the zipfile has several
components (inetpub/wwwroot/test/this_is_a_test), but if the folder's name has
no spaces, then all the ZipEntries are given shorter names without the
"inetpub/wwwroot/test/" prefix ?
Yes and no. Yes, the directories and all of the contents are being
written to the zip file. No, the name is correct. I am creating a zip
file called test.zip from the directory c:\inetpub\wwwroot\test\this is
a test. When I extract this file to my desktop, I get the directory
structure desktop\inetpub\wwwroot\test\this is a test. This is
incorrect. It should be desktop\this is a test. If I use the directory
structure c:\inetpub\wwwroot\test\this_is_a_test. Everything works
fine. When I extract this file to the desktop, I get the directory
structue desktop\this_is_a_test.

I think this must be a problem somewhere in the code you use to create the
zipfile. I would add some tracing (or use a debugger) to find exactly what
names your code is giving the ZipEntries. I'm pretty sure you'll find the
problem there, i.e. that this is nothing to do with zip files themselves (or
their Java implementation).
I will try this. Thanks for the suggestion.

Again, thank you very much.

If you have any other suggestions I would greatly appreciate it.
 
C

Chris Uppal

unwantedspam wrote:

[me:]
Yes, you are correct

Then I think that's probably half your problem there. ZIP files are not meant
to contain "absolute" paths (think what would happen if a naive client
extracted 'C:\Windows\system32\somethingVital.dll' without checking, for
instance). Unless you are creating the ZIP file for very special purposes
(i.e. not the be read by a general reader) then each filename should be of the
form:
dir1/dir2/filename
where "dir1" does NOT start with '/'.
When I extract this file to my desktop, I get the directory
structure desktop\inetpub\wwwroot\test\this is a test. This is
incorrect.

And I think that's the other half of your problem. The ZIP file reader which
is built into Windows XP (and later) is fairly deeply broken. For one thing it
(stupidly) attempts to interpret filenames with '\' in them (which probably
mislead you from the start), for another thing, it simply doesn't work with
entries including the ':' or ';' character in the filename. I'm sure it has
other problems too. This is why I kept asking what reader you were using. If
you use the unzip utility I mentioned, then you'll find out what is /actually/
in the ZIP file, not what MS's broken reader /thinks/ is in there.

I can't, by the way, reproduce exactly the symptoms you are seeing, so there
may be other problems still to be resolved, but I'd bet that the combo of
Windows and the ':' character is causing at least some of the problems.

-- chris
 

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

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top