• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem zipping a war file

 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic