This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem zipping a war file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem zipping a war file" Watch "Problem zipping a war file" New topic
Author

Problem zipping a war file

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I am creating a zip file from a directory that contains a War file; my code looks like this:
private void zipFiles(File cpFile) {
if (cpFile.isDirectory()) {
if(cpFile.getName().equalsIgnoreCase(".metadata")
||cpFile.getName().equalsIgnoreCase("Libraries")
||cpFile.getName().equalsIgnoreCase("RepositoryBuild")
||cpFile.getName().equalsIgnoreCase("images")){
return;
}
File [] fList = cpFile.listFiles() ;
for (int i=0; i< fList.length; i++){
zipFiles(fList[i]) ;
}
} else {
try {
System.out.println("Zipping "+cpFile);
size += cpFile.length();
//String strAbsPath = cpFile.getAbsolutePath();
numOfFiles++;
String strAbsPath = cpFile.getPath();
String strZipEntryName = strAbsPath.substring(this.strSource.length()+1, strAbsPath.length());
byte[] b = new byte[ (int)(cpFile.length()) ];
ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
this.cpZipOutputStream.putNextEntry(cpZipEntry );
this.cpZipOutputStream.write(b, 0, (int)cpFile.length());
this.cpZipOutputStream.closeEntry() ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
When i open the zip file with Zip, everything looks correct. The problem is that when I try to install the war file in tomcat I get the error message:
java.io.FileNotFoundException: C:\apache-tomcat-6.0.16\webapps\C:\... the full file path.
I don't understand why the ZipEntries are being created with the full file path.

If you can direct me to a good zip program i would appreciate it.

I thank you for your help.


Your help will be greatly appreciated,
Alejandro Barrero
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

WinZip (non-free), 7Zip and TugZip (both open source) are quite good ZIP programs. You can also use the JAR tool provided with the JDK.

About your problem - your adding the entries with their absolute file names, whereas you only want the name relative to your base. You could use the File.toString() method, but that still might not work if the file is already absolute or has a wrong base.
[ March 27, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thanks for your reply. Perhaps I didn't explained the situation correctly. I cannot use a utility like WinZip; I need to create the zip file with code because it is m y program performing the actions. By the way, if I zip the war directory with a utility it deploys fine in tomcat.

Please explain why you say that "your adding the entries with their absolute file names". I am creating the ZipEntry with the relative path name of the files. Is there anything else that I can do?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

Ah, you're right, I missed that part of your code. Please put code between [ code ] and [/ code ] blocks next time (omit the spaces). That will improve readability.

What is printed if you print strZipEntryName to System.out every time? Are these correct?
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you Rob. I didn't know about [ code ] and [/ code ]. The ZipEntry name is correct. For some ungodly reason the problem disappeared (I swear i am running the same code). But now i have a new problem. When I deploy the war file with tomcat's administrator, I am getting the error message:

java.io.FileNotFoundException: C:\apache-tomcat-6.0.16\webapps\A Generic Table in Java Swing to Display Data in Business Objects_files\colorschemamapping.xml

Then tomcat doesn't create the unzipped directory.

However, if I examine the war file with WinZip the directory and the file are there at the top. Also, if I create the war file with WinZip everything is fine.

Something strange is happening with java.zip.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem zipping a war file